Advertisement
Guest User

Untitled

a guest
Mar 10th, 2017
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.27 KB | None | 0 0
  1. /* package whatever; // don't place package name! */
  2.  
  3. import javax.swing.JComboBox;
  4. import javax.swing.JFrame;
  5. import javax.swing.JPanel;
  6.  
  7. public class Exemplo extends JFrame
  8. {
  9.     private String t;
  10.     public Exemplo()
  11.     {
  12.         setTitle("Exemplo combo");
  13.         setSize(500, 500);    
  14.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  15.         setLocationRelativeTo(null);          
  16.  
  17.  
  18.         JPanel jp = new JPanel();
  19.         jp.setLayout(null);
  20.         add(jp);        
  21.         JComboBox combo = new JComboBox();
  22.         combo.addItem("Opção 1");        
  23.         combo.addItem("Opção 2");
  24.         combo.addItemListener(new ItemListener(){
  25.             @Override
  26.             public void itemStateChanged(ItemEvent event) {
  27.                 if (event.getStateChange() == ItemEvent.SELECTED) {
  28.                     String value = event.getItem().toString();
  29.                     // faz algo conforme a opção selecionada
  30.                         if (value.equals("Opção 1")) {
  31.                     // faz algo se selecionou a opção 1
  32.           }
  33.        }
  34.     }
  35.         });
  36.         jp.add(combo);
  37.         combo.setBounds(180, 200, 125, 30);      
  38.     }
  39.  
  40.     public static void main(String[] args)
  41.     {
  42.         Exemplo ex = new Exemplo();
  43.         ex.setVisible(true);
  44.     }  
  45.  
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement