Swingطريقة إنشاء JOptionPane يمثل Message Dialog
المثال التالي يعلمك طريقة إظهار Message Dialog. كل زر يظهر Message Dialog مختلف.
مثال
import javax.swing.JFrame;
import javax.swing.JButton;
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 هنا وضعنا
// Buttons هنا قمنا بتعريف خمسة
JButton btn1 = new JButton("Display Plain Dialog");
JButton btn2 = new JButton("Display Information Dialog");
JButton btn3 = new JButton("Display Warning Dialog");
JButton btn4 = new JButton("Display Error Dialog");
JButton btn5 = new JButton("Display Question Dialog");
// منهم Button هنا قمنا بتحديد موقع و حجم كل
btn1.setBounds(95, 10, 200, 30);
btn2.setBounds(95, 50, 200, 30);
btn3.setBounds(95, 90, 200, 30);
btn4.setBounds(95, 130, 200, 30);
btn5.setBounds(95, 170, 200, 30);
// Frame بداخل الـ Buttons هنا قمنا بإضافة جميع الـ
frame.add(btn1);
frame.add(btn2);
frame.add(btn3);
frame.add(btn4);
frame.add(btn5);
// مرئية Frame هنا جعلنا الـ
frame.setVisible(true);
// btn1 عند النقر على الـ Message Dialog هنا قلنا أنه سيتم إظهار
btn1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(frame, "This is a plain message", "Plain Message", JOptionPane.PLAIN_MESSAGE);
}
});
// btn2 عند النقر على الـ Message Dialog هنا قلنا أنه سيتم إظهار
btn2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(frame, "This is an information message", "Information Message", JOptionPane.INFORMATION_MESSAGE);
}
});
// btn3 عند النقر على الـ Message Dialog هنا قلنا أنه سيتم إظهار
btn3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(frame, "This is a warning message", "Warning Message", JOptionPane.WARNING_MESSAGE);
}
});
// btn4 عند النقر على الـ Message Dialog هنا قلنا أنه سيتم إظهار
btn4.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(frame, "This is an error message", "Error Message", JOptionPane.ERROR_MESSAGE);
}
});
// btn5 عند النقر على الـ Message Dialog هنا قلنا أنه سيتم إظهار
btn5.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(frame, "This is a question message", "Question Message", JOptionPane.QUESTION_MESSAGE);
}
});
}
}
import javax.swing.JFrame;
import javax.swing.JButton;
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 هنا وضعنا
// Buttons هنا قمنا بتعريف خمسة
JButton btn1 = new JButton("Display Plain Dialog");
JButton btn2 = new JButton("Display Information Dialog");
JButton btn3 = new JButton("Display Warning Dialog");
JButton btn4 = new JButton("Display Error Dialog");
JButton btn5 = new JButton("Display Question Dialog");
// منهم Button هنا قمنا بتحديد موقع و حجم كل
btn1.setBounds(95, 10, 200, 30);
btn2.setBounds(95, 50, 200, 30);
btn3.setBounds(95, 90, 200, 30);
btn4.setBounds(95, 130, 200, 30);
btn5.setBounds(95, 170, 200, 30);
// Frame بداخل الـ Buttons هنا قمنا بإضافة جميع الـ
frame.add(btn1);
frame.add(btn2);
frame.add(btn3);
frame.add(btn4);
frame.add(btn5);
// مرئية Frame هنا جعلنا الـ
frame.setVisible(true);
// btn1 عند النقر على الـ Message Dialog هنا قلنا أنه سيتم إظهار
btn1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(frame, "This is a plain message", "Plain Message", JOptionPane.PLAIN_MESSAGE);
}
});
// btn2 عند النقر على الـ Message Dialog هنا قلنا أنه سيتم إظهار
btn2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(frame, "This is an information message", "Information Message", JOptionPane.INFORMATION_MESSAGE);
}
});
// btn3 عند النقر على الـ Message Dialog هنا قلنا أنه سيتم إظهار
btn3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(frame, "This is a warning message", "Warning Message", JOptionPane.WARNING_MESSAGE);
}
});
// btn4 عند النقر على الـ Message Dialog هنا قلنا أنه سيتم إظهار
btn4.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(frame, "This is an error message", "Error Message", JOptionPane.ERROR_MESSAGE);
}
});
// btn5 عند النقر على الـ Message Dialog هنا قلنا أنه سيتم إظهار
btn5.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(frame, "This is a question message", "Question Message", JOptionPane.QUESTION_MESSAGE);
}
});
}
}
ستظهر لك النافذة التالية عند التشغيل.
كل زر يظهر Message Dialog مختلف في المحتوى و الأيقونة.