Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // paket yang diperlukan
- import javax.swing.*;
- import java.awt.event.ActionListener;
- import java.awt.event.ActionEvent;
- class TestRButton extends JFrame{
- // attribut
- //membuat tombol
- JButton tombol1 = new JButton("cek ?");
- JButton tombol2 = new JButton("set nilai true");
- JLabel label = new JLabel("nilai check box");
- // JRadioButton(String nama)
- JRadioButton cb = new JRadioButton("Makan");
- JRadioButton cb2 = new JRadioButton("Ayam");
- JRadioButton cb3 = new JRadioButton("goreng");
- ButtonGroup group = new ButtonGroup();
- //konstruktor
- public TestRButton(){
- //frame seting---------------------------------------------------------------------
- //memanggil konstruktor kelas induk (JFrame)
- super("Contoh Tombol");
- //seting besar frame 400 x 400 px
- this.setSize(200,250);
- //seting agar bisa ditutup
- this.setDefaultCloseOperation(EXIT_ON_CLOSE);
- //seting kemunculan frame di tengah window
- this.setLocationRelativeTo(null);
- //seting layout frame
- this.getContentPane().setLayout(null);
- //frame seting---------------------------------------------------------------------
- //menambahkan komponen ke frame dan seting letak
- this.add(tombol1);
- this.add(tombol2);
- this.add(label);
- this.add(cb);
- this.add(cb2);
- this.add(cb3);
- // setbounds(x,y,lebar,tinggi)
- tombol1.setBounds(10,10,150,25);
- tombol2.setBounds(10,40,150,25);
- label.setBounds(10,70,150,25);
- cb.setBounds(10,110,150,25);
- cb2.setBounds(10,140,150,25);
- cb3.setBounds(10,170,150,25);
- //seting aksi tombol
- group.add(cb);
- group.add(cb2);
- group.add(cb3);
- tombol1.addActionListener(new ActionListener( ) {
- public void actionPerformed(ActionEvent e){
- // kode disini; cek keadaan check box
- if(cb.isSelected()) label.setText("Makan");
- if(cb2.isSelected()) label.setText("Ayam");
- if(cb3.isSelected()) label.setText("Goreng");
- }
- });
- tombol2.addActionListener(new ActionListener( ) {
- public void actionPerformed(ActionEvent e){
- // kode disini; cek keadaan check box ganti keadaan chek box
- if(cb.isSelected()) cb2.setSelected(true);
- else if(cb2.isSelected()) cb3.setSelected(true);
- else if(cb3.isSelected()) cb.setSelected(true);
- }
- });
- // menampilkan frame
- show();
- }
- public static void main(String[] args){
- new TestRButton();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment