package com.java.myapp;
import java.awt.EventQueue;
import javax.swing.JFrame;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.ButtonGroup;
import javax.swing.JOptionPane;
import javax.swing.JButton;
import javax.swing.JRadioButton;
public class MyForm extends JFrame {
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
MyForm frame = new MyForm();
frame.setVisible(true);
}
});
}
/**
* Create the frame.
*/
public MyForm() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 343, 273);
setTitle("ThaiCreate.Com Java GUI Tutorial");
getContentPane().setLayout(null);
// Radio Button
final JRadioButton radio1 = new JRadioButton("Item 1");
radio1.setBounds(119, 58, 109, 23);
getContentPane().add(radio1);
final JRadioButton radio2 = new JRadioButton("Item 2");
radio2.setBounds(119, 84, 109, 23);
getContentPane().add(radio2);
final JRadioButton radio3 = new JRadioButton("Item 3");
radio3.setBounds(119, 110, 109, 23);
getContentPane().add(radio3);
// Set Group
ButtonGroup group = new ButtonGroup();
group.add(radio1);
group.add(radio2);
group.add(radio3);
// Button
JButton btn = new JButton("Button");
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// Check Checkbox 1
if(radio1.isSelected()){
JOptionPane.showMessageDialog(null,
"You select : Item 1");
} else if (radio2.isSelected()) {
JOptionPane.showMessageDialog(null,
"You select : Item 2");
} else if (radio3.isSelected()) {
JOptionPane.showMessageDialog(null,
"You select : Item3");
} else {
JOptionPane.showMessageDialog(null,
"You not select.");
}
}
});
btn.setBounds(125, 154, 89, 23);
getContentPane().add(btn);
}
}