Java Radio Button (JRadioButton) - Swing Example |
Java Radio Button (JRadioButton) - Swing Example สำหรับ Radio Button หรือ JRadioButton (javax.swing.JRadioButton) จัดอยู่ในกลุ่มของ Component ใช้สร้างตัวเลือกรายการที่ที่ตัวเลือกมากกว่า 1 แต่สามารถเลือกได้รายการเดียว โดยในการสร้าง JRadioButton สามารถทำการสร้าง Group ด้วยการใช้ ButtonGroup เข้ามาช่วย และสำหรับ JRadioButton มี Property ที่ใช้ในการอ่านค่าคือ isSelected() จะได้ค่าเป็น true และ false ซึ่งสามารถตรวจสอบสถานะในแต่ล่ะตัวได้
Java Radio Button (JRadioButton) - Swing Example
Syntax
// Radio Button
JRadioButton radio1 = new JRadioButton("Item 1");
radio1.setBounds(119, 58, 109, 23);
getContentPane().add(radio1);
JRadioButton radio2 = new JRadioButton("Item 2");
radio2.setBounds(119, 84, 109, 23);
getContentPane().add(radio2);
// Set Group
ButtonGroup group = new ButtonGroup();
group.add(radio1);
group.add(radio2);
Controls Icon Tools

Example ตัวอย่างการสร้าง Radio Button ด้วย JRadioButton และการสร้าง Group ด้วย ButtonGroup
MyForm.java
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);
}
}
Output

แสดง Radio Button

ทดสอบเลือกรายการ

แสดงรายการ Radion Button ที่เลือก
Property & Method (Others Related) |
|
ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท
|
|
|
By : |
ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ) |
|
Score Rating : |
   |
|
|
Create/Update Date : |
2013-09-03 22:03:16 /
2013-09-04 11:15:23 |
|
Download : |
No files |
|
Sponsored Links / Related |
|
|
|
|
|
|