JOptionPane क्लास JComponent का एक सबक्लास है जिसमें मोडल डायलॉग बॉक्स बनाने और कस्टमाइज़ करने के लिए स्टेटिक मेथड्स शामिल हैं। JOptionPane का उपयोग JDialog के बजाय कोड की जटिलता को कम करने के लिए किया जाता है। यह चार स्टैंडर्ड आइकन्स (प्रश्न, सूचना, चेतावनी, और त्रुटि) या यूज़र द्वारा निर्दिष्ट कस्टम आइकन्स के साथ डायलॉग बॉक्स प्रदर्शित करता है।
JOptionPane क्लास चार प्रकार के डायलॉग बॉक्स प्रदर्शित करता है
- MessageDialog - एक डायलॉग बॉक्स जो संदेश प्रदर्शित करता है, जिससे यूज़र को अलर्ट करने के लिए आइकन्स जोड़ना संभव होता है।
- ConfirmDialog - एक डायलॉग बॉक्स जो संदेश भेजने के अलावा, यूज़र को किसी प्रश्न का उत्तर देने में सक्षम बनाता है।
- InputDialog - एक डायलॉग बॉक्स जो संदेश भेजने के अलावा, टेक्स्ट एंट्री की अनुमति देता है।
- OptionDialog - एक डायलॉग बॉक्स जो पिछले तीनों प्रकारों को कवर करता है।
उदाहरण
import javax.swing.*;
public class JoptionPaneTest {
public static void main(String[] args) {
JFrame frame = new JFrame("JoptionPane Test");
frame.setSize(200, 200);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
JOptionPane.showMessageDialog(frame, "Hello Java");
JOptionPane.showMessageDialog(frame, "You have less amount, please recharge", "Apocalyptic message", JOptionPane.WARNING_MESSAGE);
int result = JOptionPane.showConfirmDialog(null, "Do you want to remove item now?");
switch (result) {
case JOptionPane.YES_OPTION:
System.out.println("Yes");
break;
case JOptionPane.NO_OPTION:
System.out.println("No");
break;
case JOptionPane.CANCEL_OPTION:
System.out.println("Cancel");
break;
case JOptionPane.CLOSED_OPTION:
System.out.println("Closed");
break;
}
String name = JOptionPane.showInputDialog(null, "Please enter your name.");
System.out.println(name);
JTextField userField = new JTextField();
JPasswordField passField = new JPasswordField();
String message = "Please enter your user name and password.";
result = JOptionPane.showOptionDialog(frame, new Object[] {message, userField, passField},
"Login", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, null, null);
if (result == JOptionPane.OK_OPTION)
System.out.println(userField.getText() + " " + new String(passField.getPassword()));
System.exit(0);
}
}
आउटपुट
उदाहरण चलाने पर विभिन्न प्रकार के डायलॉग बॉक्स प्रदर्शित होंगे: संदेश डायलॉग, पुष्टिकरण डायलॉग, इनपुट डायलॉग, और विकल्प डायलॉग।