พื้นฐาน Form การสร้าง Dialog MessageBox บน Java GUI เพื่อโต้ตอบกับผู้ใช้ |
พื้นฐาน Form การสร้าง Dialog Message Box บน Java GUI เพื่อโต้ตอบกับผู้ใช้ ในการใช้งาน Dialog หรือ MessagBox บน Java GUI ถือว่าเป็นหัวข้อหนึ่งที่เราจะต้องสนใจอย่างยิ่ง เพราะมันจำเป็นจะต้องนำไปใช้กับการเขียนโปแกรมบน Form อย่างแน่นอน และ Dialog หรือ MessagBox ก็มีให้เลือกใช้งานได้หลานชนิด ตามประเภทของ Message เช่น เป็นแค่ Information แจ้งให้ทราบ , Warning หรือ Error และนอกจากนี้ยังมี MessagBox ที่ทำหน้าที่เป็น Input และ Optional Conform Dialog ได้อีกด้วย
Java GUI Dialog MessagBox ประเภทของ Dialog MessageBox
ในการใช้งาน Dialog หรือ MessagBox บน Java GUI เราจะใช้ Class ของ JOptionPane (javax.swing.JOptionPane) และมี Property อาทิเช่น showMessageDialog , showOptionDialog และอื่น ๆ รวมทั้งมี Argument ที่รอรับ Parameters ที่สามารถกำหนดรูปแบบของ Dialog ได้อย่างหลากหลาย
อ่านบทความเกี่ยวกับการสร้าง Dialog MessageBox แบบง่าย ๆ
Eclipse : สร้าง Java GUI และการสร้าง Event Action และ Dialog โต้ตอบแบบง่าย ๆ
Netbeans : สร้าง Java GUI และการสร้าง Event Action และ Dialog โต้ตอบแบบง่าย ๆ
พื้นฐาน Java สร้าง Controls บน GUI และการสร้าง Action Event Handler กับ Controls
ตัวอย่างชนิดของ Dialog แบบต่าง ๆ
JOptionPane.showMessageDialog(null,
"Eggs are not supposed to be green.");

JOptionPane.showMessageDialog(null,
"Eggs are not supposed to be green.",
"Inane warning",
JOptionPane.WARNING_MESSAGE);

JOptionPane.showMessageDialog(null,
"Eggs are not supposed to be green.",
"Inane error",
JOptionPane.ERROR_MESSAGE);

JOptionPane.showMessageDialog(null,
"Eggs are not supposed to be green.",
"A plain message",
JOptionPane.PLAIN_MESSAGE);

final ImageIcon icon = new ImageIcon(getClass().getResource("icon.gif"))
JOptionPane.showMessageDialog(null,
"Eggs are not supposed to be green.",
"Inane custom dialog",
JOptionPane.INFORMATION_MESSAGE,
icon);

Object[] options = { "Yes, please", "No, thanks", "No eggs, no ham!" };
int n = JOptionPane.showOptionDialog(null,
"Would you like some green eggs to go " + "with that ham?",
"A Silly Question", JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE, null, options, options[2]);
System.out.print(n); // Use n for response

int n = JOptionPane.showConfirmDialog(
null,
"Would you like green eggs and ham?",
"An Inane Question",
JOptionPane.YES_NO_OPTION);
System.out.print(n); // Use n for response

Object[] options = {"Yes, please","No way!"};
int n = JOptionPane.showOptionDialog(null,
"Would you like green eggs and ham?",
"A Silly Question",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE,
null, //do not use a custom Icon
options, //the titles of buttons
options[0]); //default button title
System.out.print(n); // Use n for response

Object[] possibilities = {"ham", "spam", "yam"};
String s = (String)JOptionPane.showInputDialog(
null,
"Complete the sentence:\n"
+ "\"Green eggs and...\"",
"Customized Dialog",
JOptionPane.PLAIN_MESSAGE,
null,
possibilities,
"ham");
//If a string was returned, say so.
if ((s != null) && (s.length() > 0)) {
System.out.print("Green eggs and... " + s + "!");
}

String s = (String)JOptionPane.showInputDialog(
null,
"Complete the sentence:\n"
+ "\"Input your name?\"",
"Customized Dialog",
JOptionPane.PLAIN_MESSAGE,
null,
null,
"Name");
//If a string was returned, say so.
if ((s != null) && (s.length() > 0)) {
System.out.print("Hello... " + s + "!");
}

|