Pages

Minggu, 04 Desember 2011

Kajian 2 - ButtonGroup


JButton Group adalah membuat  multi-lingkup  pengecualian  untuk  satu  set
tombol. Menciptakan satu set tombol ButtonGroup dengan objek yang sama berarti
salah  satu  yang  dipilh,  dari  tombol-tombol  tersebut  akan  menonaktifkan  semua
tombol lain dalam grup.

Beikut Contoh Sintaknya :
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class buttong extends JFrame implements ActionListener {
      JRadioButton bgRed, bgBlue, bgBlack;
      ButtonGroup grup1;

      public buttong() {
                  setTitle("Demo Radio Button");
                  setDefaultCloseOperation(EXIT_ON_CLOSE);
                  getContentPane().setLayout(
                                          new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));

                  bgRed = new JRadioButton("Red");
                  bgBlue = new JRadioButton("Blue", false);
                  bgBlack = new JRadioButton("Black", false);

                  bgRed.setActionCommand("merah");
                  bgBlue.setActionCommand("biru");
                  bgBlack.setActionCommand("hitam");

                  grup1 = new ButtonGroup();
                  grup1.add(bgRed);
                  grup1.add(bgBlue);
                  grup1.add(bgBlack);

                  getContentPane().add(bgRed);
                  getContentPane().add(bgBlue);
                  getContentPane().add(bgBlack);

                  bgRed.addActionListener(this);
                  bgBlue.addActionListener(this);
                  bgBlack.addActionListener(this);

      }

      public void actionPerformed(ActionEvent evt) {
                  if (bgRed.isSelected())
                              getContentPane().setBackground(Color.red);
                  else if (evt.getActionCommand().equals("biru"))
                              getContentPane().setBackground(Color.blue);
                  else
                              getContentPane().setBackground(Color.black);
      }

      public static void main(String[] args) {
                  buttong demoApp = new buttong();
                  demoApp.setSize(250, 225);
                  demoApp.setVisible(true);
      }

}

0 komentar:

Posting Komentar