Tester2009

GUI exercise

Nov 26th, 2015
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.04 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 guiexercise2.buttonlistener;
  7.  
  8. import java.awt.Color;
  9. import java.awt.FlowLayout;
  10. import java.awt.event.ActionEvent;
  11. import java.awt.event.ActionListener;
  12. import javax.swing.ButtonGroup;
  13. import javax.swing.JButton;
  14. import javax.swing.JFrame;
  15. import javax.swing.JLabel;
  16. import javax.swing.JPanel;
  17. import javax.swing.JRadioButton;
  18.  
  19. /**
  20.  *
  21.  * @author hakase
  22.  */
  23.  
  24. class butang extends JFrame implements ActionListener {
  25.     JButton butangMerah, butangHijau;
  26.    
  27.     public butang(String tajuk) {
  28.         super(tajuk);
  29.        
  30.         butangMerah = new JButton("MERAH");
  31.         butangHijau = new JButton("HIJAU");
  32.        
  33.         butangMerah.setActionCommand("meRAh");
  34.         butangHijau.setActionCommand("hiJAu");
  35.        
  36.         butangMerah.addActionListener(this);
  37.         butangHijau.addActionListener(this);
  38.        
  39.         setLayout(new FlowLayout());
  40.         add(butangMerah);
  41.         add(butangHijau);
  42.     }
  43.  
  44.     public void actionPerformed(ActionEvent UUUUE) {
  45.         if (UUUUE.getActionCommand().equals("meRAh"))
  46.             getContentPane().setBackground(Color.RED);
  47.         else
  48.             getContentPane().setBackground(Color.GREEN);
  49.         repaint();
  50.     }
  51. }   //  end of butang class
  52.  
  53. class butangRadio extends JPanel implements ActionListener {
  54.     private JRadioButton radioSatu, radioDua, radioTiga;
  55.     private JLabel labelSatu, labelDua, labelTiga;
  56.    
  57.     public butangRadio() {
  58.         super.setLayout(new FlowLayout());
  59.         radioSatu = new JRadioButton("Label SATU");
  60.         radioDua = new JRadioButton("Label DUA");
  61.         radioTiga = new JRadioButton("Label TIGA");
  62.        
  63.         labelSatu = new JLabel("Tunjuk Label SATU");
  64.         labelDua = new JLabel("Tunjuk Label DUA");
  65.         labelTiga = new JLabel("Tunjuk Label TIGA");
  66.        
  67.         labelSatu.setVisible(false);
  68.         labelDua.setVisible(false);
  69.         labelTiga.setVisible(false);
  70.        
  71.         radioSatu.setActionCommand("rdio1");
  72.         radioDua.setActionCommand("rdio2");
  73.         radioTiga.setActionCommand("rdio3");
  74.        
  75.         radioSatu.addActionListener(this);
  76.         radioDua.addActionListener(this);
  77.         radioTiga.addActionListener(this);
  78.        
  79.         ButtonGroup butangDen = new ButtonGroup();
  80.         butangDen.add(radioSatu);
  81.         butangDen.add(radioDua);
  82.         butangDen.add(radioTiga);
  83.        
  84.         add(radioSatu);
  85.         add(radioDua);
  86.         add(radioTiga);
  87.        
  88.         add(labelSatu);
  89.         add(labelDua);
  90.         add(labelTiga);
  91.     }
  92.     public void actionPerformed(ActionEvent eS) {
  93.         if("rdio1".equals(eS.getActionCommand())) {
  94.             labelSatu.setVisible(true);
  95.             labelDua.setVisible(false);
  96.             labelTiga.setVisible(false);
  97.         }
  98.         if("rdio2".equals(eS.getActionCommand())) {
  99.             labelSatu.setVisible(false);
  100.             labelDua.setVisible(true);
  101.             labelTiga.setVisible(false);
  102.         }
  103.         if("rdio3".equals(eS.getActionCommand())) {
  104.             labelSatu.setVisible(false);
  105.             labelDua.setVisible(false);
  106.             labelTiga.setVisible(true);
  107.         }
  108.     }
  109. }   //  end of butangRadio class
  110.  
  111. public class GUIExercise2Buttonlistener {
  112.     /**
  113.      * @param args the command line arguments
  114.      */
  115.     public static void main(String[] args) {
  116.         // TODO code application logic here
  117.         /*
  118.        
  119.         butang uji = new butang("TEKAN.LA");
  120.         uji.setSize(300, 150);
  121.         uji.setVisible(true);
  122.         uji.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  123.        
  124.         */
  125.        
  126.         JFrame kerangka = new JFrame("Guna butang radio");
  127.         kerangka.setSize(180, 300); // pixel X, pixel Y
  128.         kerangka.setVisible(true);
  129.         kerangka.add(new butangRadio());
  130.         kerangka.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  131.     }
  132. }
Add Comment
Please, Sign In to add comment