Advertisement
Guest User

aula 09 - Combobox

a guest
Apr 16th, 2014
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 KB | None | 0 0
  1. package br.com.aula09;
  2.  
  3. import java.awt.Color;
  4. import java.awt.FlowLayout;
  5. import java.awt.event.ItemEvent;
  6. import java.awt.event.ItemListener;
  7.  
  8. import javax.swing.Icon;
  9. import javax.swing.ImageIcon;
  10. import javax.swing.JComboBox;
  11. import javax.swing.JFrame;
  12. import javax.swing.JLabel;
  13. //tem que falar que a classe vai implementar item listener adiciona metodo
  14. public class excombo extends JFrame implements ItemListener{
  15.  
  16.  
  17. JComboBox a = new JComboBox();// arrays do lado de fora para a classe toda
  18. JLabel rotulo = new JLabel();
  19.  
  20. String[] nomes = {"moto","palio","carro","kombi"};
  21. String[] fotos = {"T:\\LTPII 16.04\\Carros\\moto.jpg",
  22. "T:\\LTPII 16.04\\Carros\\palio.jpg",
  23. "T:\\LTPII 16.04\\Carros\\carro.jpg",
  24. "T:\\LTPII 16.04\\Carros\\kombi.jpg"};
  25.  
  26. Icon [] icones = {new ImageIcon(fotos[0]),
  27. new ImageIcon(fotos[1]),
  28. new ImageIcon(fotos[2]),
  29. new ImageIcon(fotos[3]),
  30. };
  31.  
  32.  
  33. public excombo() {
  34. //super("Exemplo Combobox");
  35. setTitle("Exemplo Combobox");
  36. setSize(300,400);
  37. setVisible(true);
  38.  
  39. setLayout(new FlowLayout());
  40. a = new JComboBox(nomes);
  41. a.addItemListener(this);
  42.  
  43. //label que mostra a figura
  44. rotulo = new JLabel(icones[0]);
  45.  
  46. //setBackground(Color.black);
  47. //item listener - checa combobox/checkbox/radiobutton
  48.  
  49.  
  50. //faz combob e rotulo aparecer
  51. add(a); add (rotulo);
  52.  
  53.  
  54. }
  55.  
  56. public static void main(String[] args) {
  57.  
  58. new excombo();//chamando metodo construtor pra rodar
  59.  
  60. }
  61. //metodo adicionado pelo itemlistener no comeco
  62. @Override
  63. public void itemStateChanged(ItemEvent e) {
  64. // TODO Auto-generated method stub
  65. Object source = e.getSource();
  66.  
  67. //captura a selecao e joga array no rotulo
  68. if (e.getStateChange()==ItemEvent.SELECTED)
  69. rotulo.setIcon(icones[a.getSelectedIndex()]);
  70.  
  71. }
  72.  
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement