Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package guiexercise2.buttonlistener;
- import java.awt.Color;
- import java.awt.FlowLayout;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.ButtonGroup;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JPanel;
- import javax.swing.JRadioButton;
- /**
- *
- * @author hakase
- */
- class butang extends JFrame implements ActionListener {
- JButton butangMerah, butangHijau;
- public butang(String tajuk) {
- super(tajuk);
- butangMerah = new JButton("MERAH");
- butangHijau = new JButton("HIJAU");
- butangMerah.setActionCommand("meRAh");
- butangHijau.setActionCommand("hiJAu");
- butangMerah.addActionListener(this);
- butangHijau.addActionListener(this);
- setLayout(new FlowLayout());
- add(butangMerah);
- add(butangHijau);
- }
- public void actionPerformed(ActionEvent UUUUE) {
- if (UUUUE.getActionCommand().equals("meRAh"))
- getContentPane().setBackground(Color.RED);
- else
- getContentPane().setBackground(Color.GREEN);
- repaint();
- }
- } // end of butang class
- class butangRadio extends JPanel implements ActionListener {
- private JRadioButton radioSatu, radioDua, radioTiga;
- private JLabel labelSatu, labelDua, labelTiga;
- public butangRadio() {
- super.setLayout(new FlowLayout());
- radioSatu = new JRadioButton("Label SATU");
- radioDua = new JRadioButton("Label DUA");
- radioTiga = new JRadioButton("Label TIGA");
- labelSatu = new JLabel("Tunjuk Label SATU");
- labelDua = new JLabel("Tunjuk Label DUA");
- labelTiga = new JLabel("Tunjuk Label TIGA");
- labelSatu.setVisible(false);
- labelDua.setVisible(false);
- labelTiga.setVisible(false);
- radioSatu.setActionCommand("rdio1");
- radioDua.setActionCommand("rdio2");
- radioTiga.setActionCommand("rdio3");
- radioSatu.addActionListener(this);
- radioDua.addActionListener(this);
- radioTiga.addActionListener(this);
- ButtonGroup butangDen = new ButtonGroup();
- butangDen.add(radioSatu);
- butangDen.add(radioDua);
- butangDen.add(radioTiga);
- add(radioSatu);
- add(radioDua);
- add(radioTiga);
- add(labelSatu);
- add(labelDua);
- add(labelTiga);
- }
- public void actionPerformed(ActionEvent eS) {
- if("rdio1".equals(eS.getActionCommand())) {
- labelSatu.setVisible(true);
- labelDua.setVisible(false);
- labelTiga.setVisible(false);
- }
- if("rdio2".equals(eS.getActionCommand())) {
- labelSatu.setVisible(false);
- labelDua.setVisible(true);
- labelTiga.setVisible(false);
- }
- if("rdio3".equals(eS.getActionCommand())) {
- labelSatu.setVisible(false);
- labelDua.setVisible(false);
- labelTiga.setVisible(true);
- }
- }
- } // end of butangRadio class
- public class GUIExercise2Buttonlistener {
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- // TODO code application logic here
- /*
- butang uji = new butang("TEKAN.LA");
- uji.setSize(300, 150);
- uji.setVisible(true);
- uji.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- */
- JFrame kerangka = new JFrame("Guna butang radio");
- kerangka.setSize(180, 300); // pixel X, pixel Y
- kerangka.setVisible(true);
- kerangka.add(new butangRadio());
- kerangka.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- }
- }
Add Comment
Please, Sign In to add comment