Advertisement
stevennathaniel

Membuat Button Group & Memberikan Nilai pada Radio Button

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