Elaborando Interfaces

Slides:



Advertisements
Apresentações semelhantes
APLICAÇÕES DE LINGUAGEM DE PROGRAMAÇÃO ORIENTADA A OBJETOS COMPONENTES GUI – PARTE I Prof. Thiago Pereira Rique
Advertisements

1 Interface Gráfica em Java Profa. Dra. Angélica Félix de Castro.
Eventos Marco Antonio, Arquiteto de Software – TJDF Novembro/2005
Criação de telas com Swing Arquiteto de Software – TJDF
APLICAÇÕES DE LINGUAGEM DE PROGRAMAÇÃO ORIENTADA A OBJETOS COMPONENTES GUI – PARTE III Prof. Thiago Pereira Rique
14.17 Tratamento de Eventos de Teclado
Linguagens de Programação Orientadas a Objetos
Graphical User Interface (GUI) Prof(o): Alexandre Parra.
Linguagem de Programação II
Capítulo 15 Swing – Parte 2.
Tutorial I: Criando a interface de uma aplicação em Java
Aula de reposição Prof. Grace e Ângela
Componentes GUI GUI – Grafical User Interface – São construídas a partir de componentes GUI chamados de controles ou widgets (windows gadgets – engenhocas.
Programação Visual em Java
Linguagem de Programação III
Interface Grafica - Swing Professor Rafael Monteiro
Interface Gráfica (material da Profa. Luciana Nedel - www. inf. pucrs
POO.
Jpanel e Diagramadores
POO - II Prof.: Jean Carlo Mendes
POO Interface Gráfica - Swing
Java Kickstart, day 2 Semelhanças com linguagem C.
Introdução PLB-II (Java) – Prof. Alberto  Mais componentes da GUI.
GUI – Graphic User Interface Prof. M.Sc. Ronnison Reges Vidal.
Programação Prof.: Bruno Rafael de Oliveira Rodrigues.
POO - II PROF.: JEAN CARLO MENDES
UNIVERSIDADE FEDERAL DE SANTA CATARINA CTC - CENTRO TECNOLÓGICO INE - DEPARTAMENTO DE INFORMÁTICA E ESTATÍSTICA DISCIPLINA DE COMPUTAÇÃO COM OBJETOS DISTRIBUÍDOS.
Roberto C. S. Pacheco Professor
Programação Orientada a Objetos Prof. Rafael Silva.
Java Módulo II Projeto Final.
Principais Classes Swing AWT  As classes e interfaces localizadas nos pacotes:  java.awt  javax.swing  são recursos para o desenvolvimento de GUIs.
PROGRAMAÇÃO ORIENTADA A OBJETOS EM C++ Professor: Dilvan Moreira.
Programação em Java JPanel e Gerenciadores de Layout Prof. Luiz Antonio Rodrigues Prof. Luiz Antonio Unioeste - Cascavel Interfaces Gráficas Jpanel e Diagramadores.
Programação em Java RMI - Remote Method Invocation Prof. Luiz Antonio Rodrigues Prof. Luiz Antonio Unioeste - Cascavel Jpanel e Diagramadores.
Programação em Java JButton e Tratamento de Eventos Prof. Luiz Antonio Rodrigues Prof. Luiz Antonio Unioeste - Cascavel Jpanel e Diagramadores.
Programação em Java Threads Prof. Luiz Antonio Rodrigues Prof. Luiz Antonio Unioeste - Cascavel Jpanel e Diagramadores.
GUI – GRAPHIC USER INTERFACE
3.1 Classes e Objetos Em um programa orientado a objetos normalmente existem vários objetos de um mesmo tipo. Por exemplo, um programa de controle de.
Java: Interfaces Alcides Calsavara.
Criação e manipulação de objetos
CREATED BY RAFAEL S. T. VIEIRA
Programação Orientada a Objetos
Interface gráficas em Java
Elaborando as Interfaces dos trabalhos das aulas 28, 29 e 30.
Elaborando as Interfaces Aulas 37, 38 e 39.
Unidade VII – Interfaces Gráficas
Elaborando as Interfaces dos trabalhos das aulas 22, 23 e 24.
Elaborando as Interfaces dos trabalhos das aulas 25, 26 e 27.
Programação Orientada a Objetos*
Elaborando as Interfaces Aulas 31, 32 e 33
Programação Orientada a Objetos*
Elaborando as Interfaces Aulas 43, 44 e 45
Elaborando Interfaces
Rosemary Silveira Filgueiras Melo
Rosemary Silveira Filgueiras Melo
3.1.7 Variáveis de Classe e Instância
Programação Orientada a Objetos
AULA JCOMBOBOX, JCHOICE
Formatando trabalhos no Writer
Rosemary Silveira Filgueiras Melo
Exercício sobre Interface Java
Leitura do teclado e Interface gráfica
Tipos Primitivos de Dados
RESULUÇÃO DOS EXERCÍCIOS,
CALCULADORA CARACTERE E VARIÁVEIS COMPOSTAS OU VETORES EM JAVA
EXERCÍCIOS DA AULA PASSADA
Aplicação com Várias Janelas
Interface Java: JFrame, JPanel, JButton, JLabel, JMenuBar, Jmenu, JMenuItem, addSeparator() e a biblioteca Java.Math Prof. Esp. André Aparecido da Silva.
RESULUÇÃO DOS EXERCÍCIOS E
JAVA - BÁSICO COL. ESTADUAL MARIA AGUIAR TEIXEIRA
Transcrição da apresentação:

Elaborando Interfaces Prof. André Aparecido da Silva Disponível em: http://www.oxnar.com.br/2017/3ati Aulas 16, 17, e 18

Todos os componentes JLabel JTextField JButton Além do próprio JFrame

Todos os componentes

As Etiquetas ou rótulos JLabel

As Etiquetas ou rótulos - Declaração Referência Indireta Referência direta

O que muda quando a referência ao objeto é direta ou indireta?

As caixas de textos JTextField

Botões da tela JButton bt_Somar, bt_Diminuir, bt_Dividir, bt_Multiplicar, bt_Limpar; JButtom

Pensando nesta tela Mais a frente no código os botões serão adicionados aos painéis e instanciados. Painel_Leste1.add(bt_Somar = new JButton("+")); Painel_Leste1.add(bt_Diminuir = new JButton("-")); Painel_Leste.add(Painel_Leste2 = new JPanel(new FlowLayout(FlowLayout.CENTER))); Painel_Leste2.add(bt_Dividir = new JButton("/")); Painel_Leste2.add(bt_Multiplicar = new JButton("*")); Painel_Leste.add(Painel_Leste3 = new JPanel(new FlowLayout(FlowLayout.CENTER))); Painel_Leste3.add(bt_Limpar = new JButton("Limpar"));

O código – os botões import javax.swing.*; import javax.swing.event.*; import java.awt.*; import java.awt.event.*; public class CalculadoraSimples extends JFrame implements ActionListener { JPanel Painel_Norte, Painel_Sul, Painel_Centro, Painel_Leste, Painel_Oeste; JPanel Painel_Oeste1,Painel_Oeste2, Painel_Oeste3; JPanel Painel_Leste1, Painel_Leste2, Painel_Leste3; JPanel Painel_Centro1, Painel_Centro2, Painel_Centro3; JButton bt_Somar, bt_Diminuir, bt_Dividir, bt_Multiplicar,bt_Limpar; JLabel lbl_Resultado; JTextField txt_Valor1, txt_Valor2;

Note que... Por que foi feito assim? Os objetos são criados aqui mas não são instanciados. public class CalculadoraSimples extends JFrame implements ActionListener { JPanel Painel_Norte, Painel_Sul, Painel_Centro, Painel_Leste, Painel_Oeste; JPanel Painel_Oeste1,Painel_Oeste2, Painel_Oeste3; JPanel Painel_Leste1, Painel_Leste2, Painel_Leste3; JPanel Painel_Centro1, Painel_Centro2, Painel_Centro3; JButton bt_Somar, bt_Diminuir, bt_Dividir, bt_Multiplicar,bt_Limpar; JLabel lbl_Resultado; JTextField txt_Valor1, txt_Valor2; Por que foi feito assim?

O código import javax.swing.event.*; import javax.swing.*; import java.awt.*; import java.awt.event.*; public class CalculadoraSimples extends JFrame implements ActionListener { JPanel Painel_Norte, Painel_Sul, Painel_Centro, Painel_Leste, Painel_Oeste; JPanel Painel_Oeste1,Painel_Oeste2, Painel_Oeste3; JPanel Painel_Leste1, Painel_Leste2, Painel_Leste3; JPanel Painel_Centro1, Painel_Centro2, Painel_Centro3; JButton bt_Somar, bt_Diminuir, bt_Dividir, bt_Multiplicar,bt_Limpar; JLabel lbl_Resultado; JTextField txt_Valor1, txt_Valor2;

Principais métodos de um JFrame

O código import javax.swing.event.*; import javax.swing.*; import java.awt.*; import java.awt.event.*; public class CalculadoraSimples extends JFrame implements ActionListener { JPanel Painel_Norte, Painel_Sul, Painel_Centro, Painel_Leste, Painel_Oeste; JPanel Painel_Oeste1,Painel_Oeste2, Painel_Oeste3; JPanel Painel_Leste1, Painel_Leste2, Painel_Leste3; JPanel Painel_Centro1, Painel_Centro2, Painel_Centro3; JButton bt_Somar, bt_Diminuir, bt_Dividir, bt_Multiplicar,bt_Limpar; JLabel lbl_Resultado; JTextField txt_Valor1, txt_Valor2;

O código import javax.swing.event.*; import javax.swing.*; import java.awt.*; import java.awt.event.*; public class CalculadoraSimples extends JFrame implements ActionListener { JPanel Painel_Norte, Painel_Sul, Painel_Centro, Painel_Leste, Painel_Oeste; JPanel Painel_Oeste1,Painel_Oeste2, Painel_Oeste3; JPanel Painel_Leste1, Painel_Leste2, Painel_Leste3; JPanel Painel_Centro1, Painel_Centro2, Painel_Centro3;

O código import javax.swing.*; import javax.swing.event.*; import java.awt.*; import java.awt.event.*; public class CalculadoraSimples extends JFrame implements ActionListener { JPanel Painel_Norte, Painel_Sul, Painel_Centro, Painel_Leste, Painel_Oeste; JPanel Painel_Oeste1,Painel_Oeste2, Painel_Oeste3; JPanel Painel_Leste1, Painel_Leste2, Painel_Leste3; JPanel Painel_Centro1, Painel_Centro2, Painel_Centro3; JButton bt_Somar, bt_Diminuir, bt_Dividir, bt_Multiplicar,bt_Limpar; JLabel lbl_Resultado; JTextField txt_Valor1, txt_Valor2;

O código public class CalculadoraSimples extends JFrame implements ActionListener { JPanel Painel_Norte, Painel_Sul, Painel_Centro, Painel_Leste, Painel_Oeste; JPanel Painel_Oeste1,Painel_Oeste2, Painel_Oeste3; JPanel Painel_Leste1, Painel_Leste2, Painel_Leste3; JPanel Painel_Centro1, Painel_Centro2, Painel_Centro3; JButton bt_Somar, bt_Diminuir, bt_Dividir, bt_Multiplicar,bt_Limpar; JLabel lbl_Resultado; JTextField txt_Valor1, txt_Valor2; Também são JLabel mas não serão declaradas neste momento

O código Por que tantos Paineis? public class CalculadoraSimples extends JFrame implements ActionListener { JPanel Painel_Norte, Painel_Sul, Painel_Centro, Painel_Leste, Painel_Oeste; JPanel Painel_Oeste1,Painel_Oeste2, Painel_Oeste3; JPanel Painel_Leste1, Painel_Leste2, Painel_Leste3; JPanel Painel_Centro1, Painel_Centro2, Painel_Centro3; JButton bt_Somar, bt_Diminuir, bt_Dividir, bt_Multiplicar,bt_Limpar; JLabel lbl_Resultado; JTextField txt_Valor1, txt_Valor2; Por que tantos Paineis?

Aplicação dos Layout

public CalculadoraSimples() { setLayout(new BorderLayout()); getContentPane().add("East", Painel_Leste = new JPanel(new GridLayout(3,1)) ); Painel_Leste.add(Painel_Leste1 = new JPanel( new FlowLayout(FlowLayout.CENTER))); Painel_Leste1.add(bt_Somar = new JButton("+")); Painel_Leste1.add(bt_Diminuir = new JButton("-")); Painel_Leste.add(Painel_Leste2 = new JPanel(new FlowLayout(FlowLayout.CENTER))); Painel_Leste2.add(bt_Dividir = new JButton("/")); Painel_Leste2.add(bt_Multiplicar = new JButton("*")); Layout dos Paineis (JPanel) Layout do programa (JFrame)

Layouts usados nesta aplicação BorderLayout() GridLayout(Linhas, Colunas) FlowLayout()

Layout Managers Um gerenciador de layout é um objeto que determina a forma como os componentes são dispostos em um recipiente Existem vários gerenciadores de layout predefinidos definidos na biblioteca de classes padrão Java: Flow Layout Border Layout Card Layout Grid Layout GridBag Layout Definido no AWT Box Layout Overlay Layout Definido no Swing

BorderLayout

FlowLayout() setLayout(new FlowLayout()); setLayout(new FlowLayout(FlowLayout.CENTER)); setLayout(new FlowLayout(FlowLayout.LEFT)); setLayout(new FlowLayout(FlowLayout.RIFHT));

GridLayout(Linhas, Colunas) Exemplo no nosso programa: getContentPane().add("West", Painel_Oeste = new JPanel(new GridLayout(3, 1)));

Para que ser serve o método getContentPane() ? O getContentPane() serve para retornar o painel principal da JPanel.

Aplicando o BorderLayout na nossa tela Leste BorderLayout Center BorderLayout West getContentPane().add("West", Painel_Oeste = new JPanel(new GridLayout(3, 1))); Painel_Oeste.add(Painel_Oeste1 = new JPanel (new FlowLayout(FlowLayout.RIGHT))); Painel_Oeste1.add(new JLabel("Valor 1:")); Painel_Oeste.add(Painel_Oeste2 = new JPanel (new FlowLayout(FlowLayout.RIGHT))); Painel_Oeste2.add(new JLabel("Valor 2:")); Painel_Oeste.add(Painel_Oeste3 = new JPanel (new FlowLayout(FlowLayout.RIGHT))); Painel_Oeste3.add(new JLabel("Resultado:"));

Tornando os botões sensíveis aos eventos bt_Somar.addActionListener(this); bt_Diminuir.addActionListener(this); bt_Dividir.addActionListener(this); bt_Multiplicar.addActionListener(this); bt_Limpar.addActionListener(this);

Finalizando o método construtor Habilita para que quando clicado no X do canto da tela o programa seja finalizado setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); setResizable(false); setTitle("CALCULADORA SIMPLES"); pack(); setVisible(true); Desabilita o redimensionamento da tela Define o tamanho da tela Torna a tela visível (JFrame)

Tratamento de eventos public void actionPerformed (ActionEvent e) { String Caixa1 = txt_Valor1.getText(); double Resultado; double Valor1 = Double.parseDouble((Caixa1)); String Caixa2 = txt_Valor2.getText(); double Valor2 = Double.parseDouble(Caixa2); if(e.getSource()==bt_Somar) {lbl_Resultado.setText(""+(Valor1+Valor2));} if(e.getSource()==bt_Diminuir) {lbl_Resultado.setText(""+ (Valor1 - Valor2));} if(e.getSource()==bt_Multiplicar) {lbl_Resultado.setText(""+(Valor1 * Valor2));} if(e.getSource()==bt_Dividir) {lbl_Resultado.setText(""+(Valor1/Valor2));}

Tratamento de eventos Por que é feita esta conversão? public void actionPerformed (ActionEvent e) { String Caixa1 = txt_Valor1.getText(); double Resultado; double Valor1 = Double.parseDouble((Caixa1)); String Caixa2 = txt_Valor2.getText(); double Valor2 = Double.parseDouble(Caixa2); if(e.getSource()==bt_Somar) {lbl_Resultado.setText(""+(Valor1+Valor2));} if(e.getSource()==bt_Diminuir) {lbl_Resultado.setText(""+ (Valor1 - Valor2));} if(e.getSource()==bt_Multiplicar) {lbl_Resultado.setText(""+(Valor1 * Valor2));} if(e.getSource()==bt_Dividir) {lbl_Resultado.setText(""+(Valor1/Valor2));} Por que é feita esta conversão?

Finalizando o tratamento de eventos if(e.getSource()==bt_Limpar) { //Resultado = Valor1 + Valor2; lbl_Resultado.setText("Aguardando digitação de valores"); if(txt_Valor1.equals("")) {/*NAO FAÇA NADA*/} else {txt_Valor1.setText("");} if(txt_Valor2.getText().equals("")) {txt_Valor2.setText(" ");} }

O parâmetro ActionEvent e public void actionPerformed (ActionEvent e) { String Caixa1 = txt_Valor1.getText(); double Resultado; double Valor1 = Double.parseDouble((Caixa1)); String Caixa2 = txt_Valor2.getText(); double Valor2 = Double.parseDouble(Caixa2); if(e.getSource()==bt_Somar) lbl_Resultado.setText(""+(Valor1+Valor2)); } Retorna qual o objeto clicado

Opções do ActionEvent e e.getSource()  Retorna o botão que foi clicado. e.getActionCommand()  Retorna o que esta “escrito” no botão clicado.

Só falta agora colocar o método main para execução public static void main (String args []) { new CalculadoraSimples(); } Aqui também foi utilizada a referencia indireta ao objeto. Como eu sei que esta referência é indireta?

Executando agora tudo deve funciona!

Executando agora tudo deve funciona!

Vamos melhorar nossa calculadora

Adicionando novos botões e funcionalidades Vamos colocar os botões: Raiz Quadrada; Potência Fatorial; Logaritmo; Sair;

Atividade prática da semana Elabore a tela abaixo: (Valor 2,0 pontos)

Algumas dicas Math.pow(Numero, Potencia); é o método usado para calcular exponenciais no Java Para sair do sistema você pode usar o método System.exit(0);