Welcome to Abdul Malik Ikhsan's Blog

JRadioButton

Posted in Tutorial Java by samsonasik on April 13, 2007

import javax.swing.*;
import java.awt.event.*;
public class radio
{
public static void main(String args[]){
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame f=new JFrame();
f.setTitle(“harga barang”);
f.setBounds(20,20,300,200);
f.getContentPane().setLayout(null);

JRadioButton r1=new JRadioButton(“mobil”);
r1.setBounds(20,20,60,20);
f.getContentPane().add(r1);

JRadioButton r2=new JRadioButton(“motor”);
r2.setBounds(20,50,60,20);
f.getContentPane().add(r2);

JLabel label=new JLabel();
label.setText(“Harga”);
label.setBounds(90,20,60,20);
f.getContentPane().add(label);

final JTextField t=new JTextField(“”);
t.setBounds(90,50,90,20);
f.getContentPane().add(t);

ButtonGroup g=new ButtonGroup();
g.add(r1);
g.add(r2);

/*
**event ketika radiobutton diklik
*/

r1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
int harga=50000000;
t.setText(“”+harga);
}});

r2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
int harga=12000000;
t.setText(“”+harga);
}});

/*
**Ketika di close, proses run berhenti
*/

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
/*
**Tampilkan
*/

f.setVisible(true);
}}

/*
**Berikut hasil runningnya
*/

upload-radio.jpg

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: