Advertisement
Jecao

Mudar de cor

Dec 8th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.93 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package kkkkkkkkk;
  7.  
  8. import java.awt.Color;
  9.  
  10. /**
  11.  *
  12.  * @author Jeh s2 Roh
  13.  */
  14. public class NewJFrame extends javax.swing.JFrame {
  15.  
  16.     /**
  17.      * Creates new form NewJFrame
  18.      */
  19.     public NewJFrame() {
  20.         initComponents();
  21.     }
  22.  
  23.     /**
  24.      * This method is called from within the constructor to initialize the form.
  25.      * WARNING: Do NOT modify this code. The content of this method is always
  26.      * regenerated by the Form Editor.
  27.      */
  28.     @SuppressWarnings("unchecked")
  29.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  30.     private void initComponents() {
  31.  
  32.         jPanel1 = new javax.swing.JPanel();
  33.         jPanel2 = new javax.swing.JPanel();
  34.         Lista = new javax.swing.JComboBox<>();
  35.  
  36.         javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
  37.         jPanel1.setLayout(jPanel1Layout);
  38.         jPanel1Layout.setHorizontalGroup(
  39.             jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  40.             .addGap(0, 100, Short.MAX_VALUE)
  41.         );
  42.         jPanel1Layout.setVerticalGroup(
  43.             jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  44.             .addGap(0, 100, Short.MAX_VALUE)
  45.         );
  46.  
  47.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  48.  
  49.         Lista.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Amarelo", "Azul", "Vermelho", "Rosa" }));
  50.         Lista.setToolTipText("");
  51.         Lista.addPopupMenuListener(new javax.swing.event.PopupMenuListener() {
  52.             public void popupMenuCanceled(javax.swing.event.PopupMenuEvent evt) {
  53.             }
  54.             public void popupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent evt) {
  55.                 ListaPopupMenuWillBecomeInvisible(evt);
  56.             }
  57.             public void popupMenuWillBecomeVisible(javax.swing.event.PopupMenuEvent evt) {
  58.             }
  59.         });
  60.         Lista.addActionListener(new java.awt.event.ActionListener() {
  61.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  62.                 ListaActionPerformed(evt);
  63.             }
  64.         });
  65.  
  66.         javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
  67.         jPanel2.setLayout(jPanel2Layout);
  68.         jPanel2Layout.setHorizontalGroup(
  69.             jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  70.             .addGroup(jPanel2Layout.createSequentialGroup()
  71.                 .addGap(78, 78, 78)
  72.                 .addComponent(Lista, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  73.                 .addContainerGap(88, Short.MAX_VALUE))
  74.         );
  75.         jPanel2Layout.setVerticalGroup(
  76.             jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  77.             .addGroup(jPanel2Layout.createSequentialGroup()
  78.                 .addGap(74, 74, 74)
  79.                 .addComponent(Lista, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  80.                 .addContainerGap(85, Short.MAX_VALUE))
  81.         );
  82.  
  83.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  84.         getContentPane().setLayout(layout);
  85.         layout.setHorizontalGroup(
  86.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  87.             .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  88.         );
  89.         layout.setVerticalGroup(
  90.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  91.             .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  92.         );
  93.  
  94.         pack();
  95.     }// </editor-fold>                        
  96.  
  97.     private void ListaActionPerformed(java.awt.event.ActionEvent evt) {                                      
  98.         // TODO add your handling code here:
  99.     }                                    
  100.  
  101.     private void ListaPopupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent evt) {                                                  
  102.        if(Lista.getSelectedItem() == Lista.getItemAt(0)){
  103.            jPanel2.setBackground(Color.yellow);
  104.        }
  105.        if(Lista.getSelectedItem() == Lista.getItemAt(1)){
  106.            jPanel2.setBackground(Color.blue);
  107.        }
  108.        if(Lista.getSelectedItem() == Lista.getItemAt(2)){
  109.            jPanel2.setBackground(Color.red);
  110.        }
  111.        if(Lista.getSelectedItem() == Lista.getItemAt(3)){
  112.            jPanel2.setBackground(Color.pink);
  113.        }
  114.     }                                                  
  115.  
  116.     /**
  117.      * @param args the command line arguments
  118.      */
  119.     public static void main(String args[]) {
  120.         /* Set the Nimbus look and feel */
  121.         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  122.         /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  123.          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  124.          */
  125.         try {
  126.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  127.                 if ("Nimbus".equals(info.getName())) {
  128.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
  129.                     break;
  130.                 }
  131.             }
  132.         } catch (ClassNotFoundException ex) {
  133.             java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  134.         } catch (InstantiationException ex) {
  135.             java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  136.         } catch (IllegalAccessException ex) {
  137.             java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  138.         } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  139.             java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  140.         }
  141.         //</editor-fold>
  142.  
  143.         /* Create and display the form */
  144.         java.awt.EventQueue.invokeLater(new Runnable() {
  145.             public void run() {
  146.                 new NewJFrame().setVisible(true);
  147.             }
  148.         });
  149.     }
  150.  
  151.     // Variables declaration - do not modify                    
  152.     private javax.swing.JComboBox<String> Lista;
  153.     private javax.swing.JPanel jPanel1;
  154.     private javax.swing.JPanel jPanel2;
  155.     // End of variables declaration                  
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement