import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public static void main(String[] args) {
JFrame frame = new JFrame("JOptionPane demo"); // أي قمنا بإنشاء نافذة مع وضع عنوان لها JFrame هنا أنشأنا كائن من الكلاس
frame.setSize(400, 250); // هنا قمنا بتحديد حجم النافذة. عرضها 400 و طولها 250
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // هنا جعلنا زر الخروج من النافذة يغلق البرنامج
frame.setLayout(null); // في النافذة بنفسنا Buttons لذلك سنقوم بتحديد مكان الـ Layout Manager أي لم نستخدم أي null هنا وضعنا
// واحد Label و Buttons هنا قمنا بتعريف ثلاثة
JButton btn1 = new JButton("Delete your history");
JButton btn2 = new JButton("Do you like programming");
JButton btn3 = new JButton("Exit");
JLabel label = new JLabel("Here we display user interactions");
// Frame هنا قمنا بتحديد موقع و حجم جميع الأشياء التي سنضيفها في الـ
btn1.setBounds(95, 20, 200, 30);
btn2.setBounds(95, 60, 200, 30);
btn3.setBounds(95, 100, 200, 30);
label.setBounds(95, 150, 200, 30);
// Frame هنا قمنا بإضافة جميع الأشياء التي قمنا بتعريفها في الـ
// مرئية Frame هنا جعلنا الـ
// btn1 عند النقر على الـ Confirm Dialog هنا قلنا أنه سيتم إظهار
btn1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// result ثم تخزين قيمة الزر الذي نقر عليه المستخدم في المتغير Confirm Dialog سيتم إظهار
int result = JOptionPane.showConfirmDialog( frame,
"You are going to delete you history.\nAre you sure?",
JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.WARNING_MESSAGE );
// result على أساس القيمة التي تم تخزينها في المتغير label هنا سيتم تغيير نص الـ
case JOptionPane.YES_OPTION:
label.setText("User click: Yes");
case JOptionPane.NO_OPTION:
label.setText("User click: No");
case JOptionPane.CANCEL_OPTION:
label.setText("User click: Cancel");
case JOptionPane.CLOSED_OPTION:
label.setText("User click: Close");
// btn2 عند النقر على الـ Confirm Dialog هنا قلنا أنه سيتم إظهار
btn2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// result ثم تخزين قيمة الزر الذي نقر عليه المستخدم في المتغير Confirm Dialog سيتم إظهار
int result = JOptionPane.showConfirmDialog( frame,
"Do you like programming?",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE );
// result على أساس القيمة التي تم تخزينها في المتغير label هنا سيتم تغيير نص الـ
case JOptionPane.YES_OPTION:
label.setText("User click: Yes");
case JOptionPane.NO_OPTION:
label.setText("User click: No");
case JOptionPane.CLOSED_OPTION:
label.setText("User click: Close");
// btn3 عند النقر على الـ Confirm Dialog هنا قلنا أنه سيتم إظهار
btn3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// result ثم تخزين قيمة الزر الذي نقر عليه المستخدم في المتغير Confirm Dialog سيتم إظهار
int result = JOptionPane.showConfirmDialog( frame,
"Close The program now?",
JOptionPane.OK_CANCEL_OPTION,
JOptionPane.WARNING_MESSAGE );
// result على أساس القيمة التي تم تخزينها في المتغير label هنا سيتم تغيير نص الـ
case JOptionPane.OK_OPTION:
case JOptionPane.CANCEL_OPTION:
label.setText("User click: Cancel");
case JOptionPane.CLOSED_OPTION:
label.setText("User click: Close");
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame("JOptionPane demo"); // أي قمنا بإنشاء نافذة مع وضع عنوان لها JFrame هنا أنشأنا كائن من الكلاس
frame.setSize(400, 250); // هنا قمنا بتحديد حجم النافذة. عرضها 400 و طولها 250
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // هنا جعلنا زر الخروج من النافذة يغلق البرنامج
frame.setLayout(null); // في النافذة بنفسنا Buttons لذلك سنقوم بتحديد مكان الـ Layout Manager أي لم نستخدم أي null هنا وضعنا
// واحد Label و Buttons هنا قمنا بتعريف ثلاثة
JButton btn1 = new JButton("Delete your history");
JButton btn2 = new JButton("Do you like programming");
JButton btn3 = new JButton("Exit");
JLabel label = new JLabel("Here we display user interactions");
// Frame هنا قمنا بتحديد موقع و حجم جميع الأشياء التي سنضيفها في الـ
btn1.setBounds(95, 20, 200, 30);
btn2.setBounds(95, 60, 200, 30);
btn3.setBounds(95, 100, 200, 30);
label.setBounds(95, 150, 200, 30);
// Frame هنا قمنا بإضافة جميع الأشياء التي قمنا بتعريفها في الـ
frame.add(btn1);
frame.add(btn2);
frame.add(btn3);
frame.add(label);
// مرئية Frame هنا جعلنا الـ
frame.setVisible(true);
// btn1 عند النقر على الـ Confirm Dialog هنا قلنا أنه سيتم إظهار
btn1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// result ثم تخزين قيمة الزر الذي نقر عليه المستخدم في المتغير Confirm Dialog سيتم إظهار
int result = JOptionPane.showConfirmDialog( frame,
"You are going to delete you history.\nAre you sure?",
"Delete History",
JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.WARNING_MESSAGE );
// result على أساس القيمة التي تم تخزينها في المتغير label هنا سيتم تغيير نص الـ
switch (result) {
case JOptionPane.YES_OPTION:
label.setText("User click: Yes");
break;
case JOptionPane.NO_OPTION:
label.setText("User click: No");
break;
case JOptionPane.CANCEL_OPTION:
label.setText("User click: Cancel");
break;
case JOptionPane.CLOSED_OPTION:
label.setText("User click: Close");
break;
}
}
});
// btn2 عند النقر على الـ Confirm Dialog هنا قلنا أنه سيتم إظهار
btn2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// result ثم تخزين قيمة الزر الذي نقر عليه المستخدم في المتغير Confirm Dialog سيتم إظهار
int result = JOptionPane.showConfirmDialog( frame,
"Do you like programming?",
"Question",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE );
// result على أساس القيمة التي تم تخزينها في المتغير label هنا سيتم تغيير نص الـ
switch (result) {
case JOptionPane.YES_OPTION:
label.setText("User click: Yes");
break;
case JOptionPane.NO_OPTION:
label.setText("User click: No");
break;
case JOptionPane.CLOSED_OPTION:
label.setText("User click: Close");
break;
}
}
});
// btn3 عند النقر على الـ Confirm Dialog هنا قلنا أنه سيتم إظهار
btn3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// result ثم تخزين قيمة الزر الذي نقر عليه المستخدم في المتغير Confirm Dialog سيتم إظهار
int result = JOptionPane.showConfirmDialog( frame,
"Close The program now?",
"Warning",
JOptionPane.OK_CANCEL_OPTION,
JOptionPane.WARNING_MESSAGE );
// result على أساس القيمة التي تم تخزينها في المتغير label هنا سيتم تغيير نص الـ
switch (result) {
case JOptionPane.OK_OPTION:
System.exit(0);
break;
case JOptionPane.CANCEL_OPTION:
label.setText("User click: Cancel");
break;
case JOptionPane.CLOSED_OPTION:
label.setText("User click: Close");
break;
}
}
});
}
}