Advertisement
stevennathaniel

Penerapan AbstractButton & Enumeration pada jRadioButton

Jan 28th, 2015
333
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.97 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 latihan13;
  7.  
  8. import java.awt.BorderLayout;
  9.  
  10. import java.awt.GridLayout;
  11.  
  12. import java.util.Enumeration;
  13.  
  14. import javax.swing.AbstractButton;
  15.  
  16. import javax.swing.BorderFactory;
  17.  
  18. import javax.swing.ButtonGroup;
  19.  
  20. import javax.swing.JCheckBox;
  21.  
  22. import javax.swing.JCheckBoxMenuItem;
  23.  
  24. import javax.swing.JFrame;
  25.  
  26. import javax.swing.JPanel;
  27.  
  28. import javax.swing.JRadioButton;
  29.  
  30. import javax.swing.JRadioButtonMenuItem;
  31.  
  32. import javax.swing.JToggleButton;
  33.  
  34. import javax.swing.border.Border;
  35.  
  36. /**
  37.  *
  38.  * @author steven
  39.  */
  40. public class RadioButton1 extends javax.swing.JFrame {
  41.  
  42.     /**
  43.      * Creates new form RadioButton1
  44.      */
  45.     public RadioButton1() {
  46.         initComponents();
  47.        
  48.        
  49.         AbstractButton abstract1 = jToggleButton1;
  50.        
  51.         buttonGroup1.add(abstract1);
  52.        
  53.        
  54.         AbstractButton abstract2 = jRadioButton1;
  55.        
  56.         buttonGroup1.add(abstract2);
  57.        
  58.        
  59.         AbstractButton abstract3 = jCheckBox1;
  60.        
  61.         buttonGroup1.add(abstract3);
  62.        
  63.        
  64.        
  65.         buttonGroup1.setSelected(abstract1.getModel(), true);
  66.        
  67.         Enumeration elements = buttonGroup1.getElements();
  68.        
  69.         while(elements.hasMoreElements()){
  70.            
  71.            
  72.             AbstractButton tombol = (AbstractButton)elements.nextElement();
  73.            
  74.             if(tombol.isSelected()){
  75.                
  76.                
  77.                 System.out.println("Maka juaranya adalah : " + tombol.getText());
  78.             }
  79.         }
  80.        
  81.        
  82.        
  83.        
  84.        
  85.        
  86.     }
  87.  
  88.     /**
  89.      * This method is called from within the constructor to initialize the form.
  90.      * WARNING: Do NOT modify this code. The content of this method is always
  91.      * regenerated by the Form Editor.
  92.      */
  93.     @SuppressWarnings("unchecked")
  94.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  95.     private void initComponents() {
  96.  
  97.         buttonGroup1 = new javax.swing.ButtonGroup();
  98.         jToggleButton1 = new javax.swing.JToggleButton();
  99.         jRadioButton1 = new javax.swing.JRadioButton();
  100.         jCheckBox1 = new javax.swing.JCheckBox();
  101.  
  102.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  103.  
  104.         jToggleButton1.setText("jToggleButton1");
  105.  
  106.         jRadioButton1.setText("jRadioButton1");
  107.  
  108.         jCheckBox1.setText("jCheckBox1");
  109.  
  110.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  111.         getContentPane().setLayout(layout);
  112.         layout.setHorizontalGroup(
  113.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  114.             .addGroup(layout.createSequentialGroup()
  115.                 .addContainerGap()
  116.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  117.                     .addComponent(jToggleButton1)
  118.                     .addComponent(jRadioButton1)
  119.                     .addComponent(jCheckBox1))
  120.                 .addContainerGap(268, Short.MAX_VALUE))
  121.         );
  122.         layout.setVerticalGroup(
  123.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  124.             .addGroup(layout.createSequentialGroup()
  125.                 .addContainerGap()
  126.                 .addComponent(jToggleButton1)
  127.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  128.                 .addComponent(jRadioButton1)
  129.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  130.                 .addComponent(jCheckBox1)
  131.                 .addContainerGap(198, Short.MAX_VALUE))
  132.         );
  133.  
  134.         pack();
  135.     }// </editor-fold>                        
  136.  
  137.     /**
  138.      * @param args the command line arguments
  139.      */
  140.     public static void main(String args[]) {
  141.         /* Set the Nimbus look and feel */
  142.         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  143.         /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  144.          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  145.          */
  146.         try {
  147.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  148.                 if ("Nimbus".equals(info.getName())) {
  149.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
  150.                     break;
  151.                 }
  152.             }
  153.         } catch (ClassNotFoundException ex) {
  154.             java.util.logging.Logger.getLogger(RadioButton1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  155.         } catch (InstantiationException ex) {
  156.             java.util.logging.Logger.getLogger(RadioButton1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  157.         } catch (IllegalAccessException ex) {
  158.             java.util.logging.Logger.getLogger(RadioButton1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  159.         } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  160.             java.util.logging.Logger.getLogger(RadioButton1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  161.         }
  162.         //</editor-fold>
  163.  
  164.         /* Create and display the form */
  165.         java.awt.EventQueue.invokeLater(new Runnable() {
  166.             public void run() {
  167.                 new RadioButton1().setVisible(true);
  168.             }
  169.         });
  170.     }
  171.  
  172.     // Variables declaration - do not modify                    
  173.     private javax.swing.ButtonGroup buttonGroup1;
  174.     private javax.swing.JCheckBox jCheckBox1;
  175.     private javax.swing.JRadioButton jRadioButton1;
  176.     private javax.swing.JToggleButton jToggleButton1;
  177.     // End of variables declaration                  
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement