JOptionPane হল JComponent ক্লাসের একটি সাবক্লাস যাতে মডাল ডায়ালগ বক্স তৈরি এবং কাস্টমাইজ করার জন্য স্ট্যাটিক মেথড রয়েছে। কোডের জটিলতা কমাতে JDialog-এর পরিবর্তে JOptionPane ব্যবহার করা হয়। JOptionPane চারটি স্ট্যান্ডার্ড আইকন (প্রশ্ন, তথ্য, সতর্কতা, এবং ত্রুটি) বা ব্যবহারকারীর নির্দিষ্ট কাস্টম আইকন সহ ডায়ালগ বক্স প্রদর্শন করে।
JOptionPane ক্লাস চার ধরনের ডায়ালগ বক্স প্রদর্শন করতে ব্যবহৃত হয়
- MessageDialog - ব্যবহারকারীকে সতর্ক করার জন্য আইকন যোগ করার সুবিধা সহ একটি মেসেজ প্রদর্শন করে।
- ConfirmDialog - মেসেজ পাঠানোর পাশাপাশি ব্যবহারকারীকে একটি প্রশ্নের উত্তর দিতে সক্ষম করে।
- InputDialog - মেসেজ পাঠানোর পাশাপাশি টেক্সট ইনপুট लेने দেয়।
- OptionDialog - উপরের তিনটি টাইপের সমন্বয় করে একটি linh hoạt ডায়ালগ বক্স তৈরি করে।
উদাহরণ
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);
}
}
আউটপুট




