Programming Basics SQL HTML CSS JavaScript Python C++ Java JavaFX Swing Problem Solving English English Conversations Computer Fundamentals Learn Typing

Swingطريقة معرفة الـ JRadioButton الذي قام المستخدم بإختياره

المثال التالي يعلمك طريقة معرفة الـ Radio Button الذي قام المستخدم بإختياره ضمن مجموعة Radio Buttons.


مثال

Main.java
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
public class Main {
// هنا قمنا بإنشاء النافذة و جميع الأشياء التي سنضعها فيها
static JFrame frame = new JFrame("JRadioButton demo");
static JLabel label = new JLabel("Select your language");
static JRadioButton radioButton_1 = new JRadioButton("Arabic", true);
static JRadioButton radioButton_2 = new JRadioButton("English");
static JRadioButton radioButton_3 = new JRadioButton("French");
static JLabel labelResult = new JLabel();
static JButton button = new JButton("View selected choice");
public static void main(String[] args) {
// ضمن مجموعة واحدة JRadioButton و الذي سنسخدمه لوضع كائنات الـ ButtonGroup هنا أنشأنا كائن من الكلاس
ButtonGroup group = new ButtonGroup();
group.add(radioButton_1);
group.add(radioButton_2);
group.add(radioButton_3);
// frame هنا قمنا بتحديد أماكن الأشياء التي سنضيفها في الـ
label.setBounds(40, 10, 150, 30);
radioButton_1.setBounds(40, 40, 100, 30);
radioButton_2.setBounds(40, 70, 100, 30);
radioButton_3.setBounds(40, 100, 100, 30);
button.setBounds(40, 150, 170, 30);
labelResult.setBounds(40, 190, 180, 30);
// frame هنا قمنا بإضافة جميع الأشياء التي قمنا بتعريفها سابقاً في الـ
frame.add(label);
frame.add(radioButton_1);
frame.add(radioButton_2);
frame.add(radioButton_3);
frame.add(button);
frame.add(labelResult);
// frame هنا قمنا بتحديد خصائص الـ
frame.setSize(300, 300); // هنا قمنا بتحديد حجم النافذة. عرضها 300 و طولها 300
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // هنا جعلنا زر الخروج من النافذة يغلق البرنامج
frame.setLayout(null); // لذلك قمنا بتحديد مكان كل شيء قمنا بإضافته في النافذة Layout Manager أي لم نستخدم أي null هنا وضعنا
frame.setVisible(true); // هنا جعلنا النافذة مرئية
// button هنا نضع الأوامر التي نريد تنفيذها عند النقر على الـ
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e)
{
// labelResult سيتم وضع النص التالي كنص للـ radioButton_1 إذا قام المستخدم بإختيار الـ
if(radioButton_1.isSelected())
labelResult.setText("You language is: "+radioButton_1.getText());
// labelResult سيتم وضع النص التالي كنص للـ radioButton_2 إذا قام المستخدم بإختيار الـ
else if(radioButton_2.isSelected())
labelResult.setText("You language is: "+radioButton_2.getText());
// labelResult سيتم وضع النص التالي كنص للـ radioButton_3 إذا قام المستخدم بإختيار الـ
else if(radioButton_3.isSelected())
labelResult.setText("You language is: "+radioButton_3.getText());
}
});
}
}
import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JRadioButton; import javax.swing.ButtonGroup; import javax.swing.JButton; public class Main { // هنا قمنا بإنشاء النافذة و جميع الأشياء التي سنضعها فيها static JFrame frame = new JFrame("JRadioButton demo"); static JLabel label = new JLabel("Select your language"); static JRadioButton radioButton_1 = new JRadioButton("Arabic", true); static JRadioButton radioButton_2 = new JRadioButton("English"); static JRadioButton radioButton_3 = new JRadioButton("French"); static JLabel labelResult = new JLabel(); static JButton button = new JButton("View selected choice"); public static void main(String[] args) { // ضمن مجموعة واحدة JRadioButton و الذي سنسخدمه لوضع كائنات الـ ButtonGroup هنا أنشأنا كائن من الكلاس ButtonGroup group = new ButtonGroup(); group.add(radioButton_1); group.add(radioButton_2); group.add(radioButton_3); // frame هنا قمنا بتحديد أماكن الأشياء التي سنضيفها في الـ label.setBounds(40, 10, 150, 30); radioButton_1.setBounds(40, 40, 100, 30); radioButton_2.setBounds(40, 70, 100, 30); radioButton_3.setBounds(40, 100, 100, 30); button.setBounds(40, 150, 170, 30); labelResult.setBounds(40, 190, 180, 30); // frame هنا قمنا بإضافة جميع الأشياء التي قمنا بتعريفها سابقاً في الـ frame.add(label); frame.add(radioButton_1); frame.add(radioButton_2); frame.add(radioButton_3); frame.add(button); frame.add(labelResult); // frame هنا قمنا بتحديد خصائص الـ frame.setSize(300, 300); // هنا قمنا بتحديد حجم النافذة. عرضها 300 و طولها 300 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // هنا جعلنا زر الخروج من النافذة يغلق البرنامج frame.setLayout(null); // لذلك قمنا بتحديد مكان كل شيء قمنا بإضافته في النافذة Layout Manager أي لم نستخدم أي null هنا وضعنا frame.setVisible(true); // هنا جعلنا النافذة مرئية // button هنا نضع الأوامر التي نريد تنفيذها عند النقر على الـ button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // labelResult سيتم وضع النص التالي كنص للـ radioButton_1 إذا قام المستخدم بإختيار الـ if(radioButton_1.isSelected()) labelResult.setText("You language is: "+radioButton_1.getText()); // labelResult سيتم وضع النص التالي كنص للـ radioButton_2 إذا قام المستخدم بإختيار الـ else if(radioButton_2.isSelected()) labelResult.setText("You language is: "+radioButton_2.getText()); // labelResult سيتم وضع النص التالي كنص للـ radioButton_3 إذا قام المستخدم بإختيار الـ else if(radioButton_3.isSelected()) labelResult.setText("You language is: "+radioButton_3.getText()); } }); } }

ستظهر لك النافذة التالية عند التشغيل.

طريقة معرفة ال JRadioButton الذي قام المستخدم بإختياره في جافا