Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package br.com.aula09;
- import java.awt.Color;
- import java.awt.FlowLayout;
- import java.awt.event.ItemEvent;
- import java.awt.event.ItemListener;
- import javax.swing.Icon;
- import javax.swing.ImageIcon;
- import javax.swing.JComboBox;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- //tem que falar que a classe vai implementar item listener adiciona metodo
- public class excombo extends JFrame implements ItemListener{
- JComboBox a = new JComboBox();// arrays do lado de fora para a classe toda
- JLabel rotulo = new JLabel();
- String[] nomes = {"moto","palio","carro","kombi"};
- String[] fotos = {"T:\\LTPII 16.04\\Carros\\moto.jpg",
- "T:\\LTPII 16.04\\Carros\\palio.jpg",
- "T:\\LTPII 16.04\\Carros\\carro.jpg",
- "T:\\LTPII 16.04\\Carros\\kombi.jpg"};
- Icon [] icones = {new ImageIcon(fotos[0]),
- new ImageIcon(fotos[1]),
- new ImageIcon(fotos[2]),
- new ImageIcon(fotos[3]),
- };
- public excombo() {
- //super("Exemplo Combobox");
- setTitle("Exemplo Combobox");
- setSize(300,400);
- setVisible(true);
- setLayout(new FlowLayout());
- a = new JComboBox(nomes);
- a.addItemListener(this);
- //label que mostra a figura
- rotulo = new JLabel(icones[0]);
- //setBackground(Color.black);
- //item listener - checa combobox/checkbox/radiobutton
- //faz combob e rotulo aparecer
- add(a); add (rotulo);
- }
- public static void main(String[] args) {
- new excombo();//chamando metodo construtor pra rodar
- }
- //metodo adicionado pelo itemlistener no comeco
- @Override
- public void itemStateChanged(ItemEvent e) {
- // TODO Auto-generated method stub
- Object source = e.getSource();
- //captura a selecao e joga array no rotulo
- if (e.getStateChange()==ItemEvent.SELECTED)
- rotulo.setIcon(icones[a.getSelectedIndex()]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement