Elaborando Interfaces Prof Andr Aparecido da Silva Disponvel
Elaborando Interfaces Prof. André Aparecido da Silva Disponível em: http: //www. oxnar. com. br/2017/3 ati Aulas 16, 17, e 18 1
Pensando nesta tela 2
As Etiquetas ou rótulos JLabel 3
As caixas de textos JText. Field 4
Pensando nesta tela JButton bt_Somar, bt_Diminuir, bt_Dividir, bt_Multiplicar, bt_Limpar; JButtom 5
Pensando nesta tela Mais a frente no código os botões serão adicionados aos painéis e instanciados. Painel_Leste 1. add(bt_Somar = new JButton("+")); Painel_Leste 1. add(bt_Diminuir = new JButton("-")); Painel_Leste. add(Painel_Leste 2 = new JPanel(new Flow. Layout(Flow. Layout. CENTER))); Painel_Leste 2. add(bt_Dividir = new JButton("/")); Painel_Leste 2. add(bt_Multiplicar = new JButton("*")); Painel_Leste. add(Painel_Leste 3 = new JPanel(new Flow. Layout(Flow. Layout. CENTER))); Painel_Leste 3. add(bt_Limpar = new JButton("Limpar")); 6
O código – os botões import javax. swing. *; import javax. swing. event. *; import java. awt. event. *; public class Calculadora. Simples extends JFrame implements Action. Listener { JPanel Painel_Norte, Painel_Sul, Painel_Centro, Painel_Leste, Painel_Oeste; JPanel Painel_Oeste 1, Painel_Oeste 2, Painel_Oeste 3; JPanel Painel_Leste 1, Painel_Leste 2, Painel_Leste 3; JPanel Painel_Centro 1, Painel_Centro 2, Painel_Centro 3; JButton bt_Somar, bt_Diminuir, bt_Dividir, bt_Multiplicar, bt_Limpar; JLabel lbl_Resultado; JText. Field txt_Valor 1, txt_Valor 2; 7
Note que. . . Os objetos são criados aqui mas não são instanciados. public class Calculadora. Simples extends JFrame implements Action. Listener { JPanel Painel_Norte, Painel_Sul, Painel_Centro, Painel_Leste, Painel_Oeste; JPanel Painel_Oeste 1, Painel_Oeste 2, Painel_Oeste 3; JPanel Painel_Leste 1, Painel_Leste 2, Painel_Leste 3; JPanel Painel_Centro 1, Painel_Centro 2, Painel_Centro 3; JButton bt_Somar, bt_Diminuir, bt_Dividir, bt_Multiplicar, bt_Limpar; JLabel lbl_Resultado; JText. Field txt_Valor 1, txt_Valor 2; Por que foi feito assim? 8
O código import javax. swing. event. *; import javax. swing. *; import java. awt. event. *; public class Calculadora. Simples extends JFrame implements Action. Listener { JPanel Painel_Norte, Painel_Sul, Painel_Centro, Painel_Leste, Painel_Oeste; JPanel Painel_Oeste 1, Painel_Oeste 2, Painel_Oeste 3; JPanel Painel_Leste 1, Painel_Leste 2, Painel_Leste 3; JPanel Painel_Centro 1, Painel_Centro 2, Painel_Centro 3; JButton bt_Somar, bt_Diminuir, bt_Dividir, bt_Multiplicar, bt_Limpar; JLabel lbl_Resultado; JText. Field txt_Valor 1, txt_Valor 2; 9
O código import javax. swing. event. *; import javax. swing. *; import java. awt. event. *; public class Calculadora. Simples extends JFrame implements Action. Listener { JPanel Painel_Norte, Painel_Sul, Painel_Centro, Painel_Leste, Painel_Oeste; JPanel Painel_Oeste 1, Painel_Oeste 2, Painel_Oeste 3; JPanel Painel_Leste 1, Painel_Leste 2, Painel_Leste 3; JPanel Painel_Centro 1, Painel_Centro 2, Painel_Centro 3; JButton bt_Somar, bt_Diminuir, bt_Dividir, bt_Multiplicar, bt_Limpar; JLabel lbl_Resultado; JText. Field txt_Valor 1, txt_Valor 2; 10
O código import javax. swing. *; import javax. swing. event. *; import java. awt. event. *; public class Calculadora. Simples extends JFrame implements Action. Listener { JPanel Painel_Norte, Painel_Sul, Painel_Centro, Painel_Leste, Painel_Oeste; JPanel Painel_Oeste 1, Painel_Oeste 2, Painel_Oeste 3; JPanel Painel_Leste 1, Painel_Leste 2, Painel_Leste 3; JPanel Painel_Centro 1, Painel_Centro 2, Painel_Centro 3; JButton bt_Somar, bt_Diminuir, bt_Dividir, bt_Multiplicar, bt_Limpar; JLabel lbl_Resultado; JText. Field txt_Valor 1, txt_Valor 2; 11
O código public class Calculadora. Simples extends JFrame implements Action. Listener { JPanel Painel_Norte, Painel_Sul, Painel_Centro, Painel_Leste, Painel_Oeste; JPanel Painel_Oeste 1, Painel_Oeste 2, Painel_Oeste 3; JPanel Painel_Leste 1, Painel_Leste 2, Painel_Leste 3; JPanel Painel_Centro 1, Painel_Centro 2, Painel_Centro 3; JButton bt_Somar, bt_Diminuir, bt_Dividir, bt_Multiplicar, bt_Limpar; JLabel lbl_Resultado; JText. Field txt_Valor 1, txt_Valor 2; Também são JLabel mas não serão declaradas neste momento 12
O código public class Calculadora. Simples extends JFrame implements Action. Listener { JPanel Painel_Norte, Painel_Sul, Painel_Centro, Painel_Leste, Painel_Oeste; JPanel Painel_Oeste 1, Painel_Oeste 2, Painel_Oeste 3; JPanel Painel_Leste 1, Painel_Leste 2, Painel_Leste 3; JPanel Painel_Centro 1, Painel_Centro 2, Painel_Centro 3; JButton bt_Somar, bt_Diminuir, bt_Dividir, bt_Multiplicar, bt_Limpar; JLabel lbl_Resultado; JText. Field txt_Valor 1, txt_Valor 2; Por que tantos Paineis? 13
Aplicação dos Layout 14
public Calculadora. Simples() { set. Layout(new Border. Layout()); get. Content. Pane(). add("East", Painel_Leste = new JPanel(new Grid. Layout(3, 1)) ); Painel_Leste. add(Painel_Leste 1 = new JPanel( new Flow. Layout(Flow. Layout. CENTER))); Painel_Leste 1. add(bt_Somar = new JButton("+")); Painel_Leste 1. add(bt_Diminuir = new JButton("-")); Painel_Leste. add(Painel_Leste 2 = new JPanel(new Flow. Layout(Flow. Layout. CENTER))); Painel_Leste 2. add(bt_Dividir = new JButton("/")); Painel_Leste 2. add(bt_Multiplicar = new JButton("*")); Layout dos Paineis (JPanel) Layout do programa (JFrame) 15
Layouts usados na linguagem Java • Border. Layout() • Grid. Layout(Linhas, Colunas) • Flow. Layout() 16
Border. Layout 17
Flow. Layout() set. Layout(new Flow. Layout()); set. Layout(new Flow. Layout(Flow. Layout. CENTER)); set. Layout(new Flow. Layout(Flow. Layout. LEFT)); set. Layout(new Flow. Layout(Flow. Layout. RIFHT)); 18
Grid. Layout(Linhas, Colunas) Exemplo no nosso programa: get. Content. Pane(). add("West", Painel_Oeste = new JPanel(new Grid. Layout(3, 1))); 19
Para que serve o método get. Content. Pane() ? O get. Content. Pane() serve para retornar o painel principal da JPanel. 20
Aplicando o Border. Layout na nossa tela get. Content. Pane(). add("West", Painel_Oeste = new JPanel(new Grid. Layout(3, 1))); Painel_Oeste. add(Painel_Oeste 1 = new JPanel (new Flow. Layout(Flow. Layout. RIGHT))); Painel_Oeste 1. add(new JLabel("Valor 1: ")); Painel_Oeste. add(Painel_Oeste 2 = new JPanel (new Flow. Layout(Flow. Layout. RIGHT))); Painel_Oeste 2. add(new JLabel("Valor 2: ")); Painel_Oeste. add(Painel_Oeste 3 = new JPanel (new Flow. Layout(Flow. Layout. RIGHT))); Painel_Oeste 3. add(new JLabel("Resultado: ")); 21
- Slides: 21