A apresentação está carregando. Por favor, espere

A apresentação está carregando. Por favor, espere

Introdução PLB-II (Java) – Prof. Alberto  Mais componentes da GUI.

Apresentações semelhantes


Apresentação em tema: "Introdução PLB-II (Java) – Prof. Alberto  Mais componentes da GUI."— Transcrição da apresentação:

1 Introdução PLB-II (Java) – Prof. Alberto  Mais componentes da GUI

2 Introdução PLB-II (Java) – Prof. Alberto JTextField: caixa de texto (TextField, em awt); JPasswordField: caixa de texto para senha (caracter default: '*', sendo alterável por setEchoChar('caracter') ). class Compo01 extends Frame implements ActionListener { private JLabel l1=new JLabel("Digite seu nome:"); private JTextField campo1 = new JTextField("nome",20); private JLabel l2=new JLabel("Digite sua senha:"); private JPasswordField campo2 = new JPasswordField(5); private JPanel p1=new JPanel(new GridLayout(4,1)); Compo01(String tit, int larg, int alt, int col, int lin) { super(tit); setSize(larg,alt); setLocation(col,lin); JTextField e JPasswordField

3 Introdução PLB-II (Java) – Prof. Alberto campo1.addActionListener(this); campo2.addActionListener(this); p1.add(l1); p1.add(campo1); p1.add(l2); p1.add(campo2); add(p1); } public void actionPerformed(ActionEvent e) { if (e.getSource() == campo1) campo2.requestFocus(); if (e.getSource() == campo2) System.exit(0); } JTextField e JPasswordField

4 Introdução PLB-II (Java) – Prof. Alberto TextArea: caixa de texto de várias linhas, memo (também JTextArea – swing); class Compo02 extends Frame { private Label l1=new Label("Digite o texto:"); private TextArea caixa1 = new TextArea(5,20); Compo02(String titulo, int largura, int altura, int colinic, int lininic) { super(titulo); setSize(largura,altura); setLocation(colinic,lininic); setLayout(new FlowLayout( FlowLayout.LEFT)); add(l1); add(caixa1); } TextArea

5 Introdução PLB-II (Java) – Prof. Alberto Como JTextArea não apresenta as barras de rolagem, é necessário criar e instanciar o componente JScrollPane com o JTextArea declarado: class Compo02a extends Frame { private JLabel l1=new JLabel("Digite o texto:"); private JTextArea caixa1 = new JTextArea(5,20); private JScrollPane j = new JScrollPane(caixa1); Compo02a(String titulo, int largura, int altura, int colinic, int lininic) { super(titulo); setSize(largura,altura); setLocation(colinic,lininic); setLayout(new FlowLayout(FlowLayout. LEFT)); add(l1); add(j); } O método append() permite inserir uma string na área de texto. JTextArea

6 Introdução PLB-II (Java) – Prof. Alberto List: caixa de listagem (listbox) class Compo03 extends Frame { private Label l1=new Label("Opções:"); private List lst1= new List(5); Compo03(String titulo, int largura, int altura, int colinic, int lininic) { super(titulo); setSize(largura,altura); setLocation(colinic,lininic); setLayout(new FlowLayout(FlowLayout.LEFT)); add(l1); add(lst1); lst1.add("maçã"); lst1.add("banana"); lst1.add("uva"); lst1.add("pera"); lst1.add("mamão"); lst1.add("melancia"); } List

7 Introdução PLB-II (Java) – Prof. Alberto class Compo03a extends Frame implements ActionListener, ItemListener { private String S[] = {"maçã","banana","uva","pera", "mamão","melancia"}; private Label l1 = new Label("Opções:"); private List lst1 = new List(5); private Label l2 = new Label("fruta"); private Label l3 = new Label("selecionada"); Compo03a(String titulo, int largura, int altura, int colinic, int lininic) { super(titulo); setSize(largura,altura); setLocation(colinic,lininic); setLayout(new FlowLayout(FlowLayout.LEFT)); add(l1); add(lst1); add(l2); add(l3); lst1.addActionListener(this); lst1.addItemListener(this); for(int x=0;x<=5;x++) lst1.add(S[x]); } List e Jlist – exemplos

8 Introdução PLB-II (Java) – Prof. Alberto List e JList - Exemplo public void actionPerformed(ActionEvent e) { if (e.getSource() == lst1) l2.setText(lst1.getSelectedItem()); } public void itemStateChanged(ItemEvent e) { if (e.getSource() == lst1) { if (lst1.getSelectedIndex()>-1) l3.setText(lst1.getSelectedItem()); } }

9 Introdução PLB-II (Java) – Prof. Alberto Choice: semelhante a um ComboBox class Compo03b extends Frame implements ItemListener { private String S[] = {"maçã","banana","uva","pera", "mamão","melancia"}; private Label l1 = new Label("Opções:"); private Choice lst1 = new Choice(); private Label l3 = new Label("selecionada"); Compo03b(String titulo, int largura, int altura, int colinic, int lininic) { super(titulo); setSize(largura,altura); setLocation(colinic,lininic); setLayout(new FlowLayout(FlowLayout.LEFT)); add(l1); add(lst1); add(l3); lst1.addItemListener(this); Choice

10 Introdução PLB-II (Java) – Prof. Alberto for(int x=0;x<=5;x++) lst1.add(S[x]); } public void itemStateChanged(ItemEvent e) { if (e.getSource() == lst1) { if (lst1.getSelectedIndex()>-1) l3.setText(lst1.getSelectedItem()); } Choice

11 Introdução PLB-II (Java) – Prof. Alberto class Compo04 extends Frame { private Label l1 = new Label("Opções:"); private Checkbox ck1 = new Checkbox("Opção 1",false); private Checkbox ck2 = new Checkbox("Opção 2",false); private Checkbox ck3 = new Checkbox("Opção 3",true); Compo04(String titulo, int largura, int altura, int colinic, int lininic) { super(titulo); setSize(largura,altura); setLocation(colinic,lininic); setLayout(new FlowLayout(FlowLayout.LEFT)); add(l1); add(ck1); add(ck2); add(ck3); } Checkbox

12 Introdução PLB-II (Java) – Prof. Alberto Alguns métodos de Checkbox: getLabel(): recupera o texto associado ao Checkbox; getState(): recupera o estado (falso/verdadeiro) do Checkbox; setLabel(string): seta o rótulo do Checkbox; setState(boolean): seta o estado do Checkbox (falso/verdadeiro). Checkbox

13 Introdução PLB-II (Java) – Prof. Alberto Não há componente semelhante ao RadioButton. Pode-se colocar vários checkbox em um CheckboxGroup: assim, eles funcionarão semelhantemente a um RadioButton: class Compo05 extends Frame { private Label l1 = new Label("Opções:"); private CheckboxGroup cbg = new CheckboxGroup(); private Checkbox ck1= new Checkbox("Opção 1",cbg,true); private Checkbox ck2= new Checkbox("Opção 2",cbg,false); private Checkbox ck3= new Checkbox("Opção 3",cbg,false); Compo05(String tit, int lar, int alt, int col, int lin) { super(tit); setSize(lar,alt); setLocation(col,lin); setLayout(new FlowLayout(FlowLayout.LEFT)); add(l1); add(ck1); add(ck2); add(ck3); } RadioButton

14 Introdução PLB-II (Java) – Prof. Alberto Não há componente exclusivo para apresentação de imagens. Podemos utilizar o ImageIcon e trabalhá-lo como um JScrollPane: class Compo06 extends Frame { private ImageIcon i = new ImageIcon("fig.gif"); private JScrollPane j = new JScrollPane (new JLabel (i)); Compo06(String tit, int lar, int alt, int col, int lin) { super(tit); setSize(lar,alt); setLocation(col,lin); setLayout(new FlowLayout(FlowLayout.LEFT)); add(j); } Imagens

15 Introdução PLB-II (Java) – Prof. Alberto Ou podemos trabalhar com a classe Image e o objeto Toolkit: class Compo06 extends Frame implements ActionListener { private TextField tf = new TextField(); private Image img; Compo06(String tit, int larg, int alt, int colinic, int lininic) { super(tit); setSize(larg,alt); setLocation(colinic,lininic); tf.addActionListener(this); add(tf,BorderLayout.SOUTH); } Imagens

16 Introdução PLB-II (Java) – Prof. Alberto public void actionPerformed(ActionEvent e) { if (img != null) img.flush(); img = Toolkit.getDefaultToolkit().getImage(tf.getText()); repaint(); } public void paint(Graphics g) { if (img != null) { Insets i = getInsets(); g.drawImage(img,i.left,i.top,this); } getImage obtém um arquivo gráfico e flush libera recursos do objeto Imagens


Carregar ppt "Introdução PLB-II (Java) – Prof. Alberto  Mais componentes da GUI."

Apresentações semelhantes


Anúncios Google