import javax.swing.*;
public class SwingDialog {
public static void main(String[] args) {
showMessage();
showConfirm();
showInput();
showOption();
}
public static void showMessage(){
JOptionPane.showMessageDialog(null, "error", "接單結果", 0);//error
JOptionPane.showMessageDialog(null, "warning", "接單結果", 2);//warning
JOptionPane.showMessageDialog(null, "question", "接單結果", 3);//question
JOptionPane.showMessageDialog(null, "info", "接單結果", 1);//info
JOptionPane.showMessageDialog(null, "nothing", "接單結果", -1);//nothing
}
public static void showConfirm(){
int response = JOptionPane.showConfirmDialog(null, "您確定要接該單嗎?", "接單確認", JOptionPane.YES_NO_OPTION);
if(response==0){
//TODO 點了是
}
if(response==1){
//TODO 點了否
}
}
public static void showInput(){
Object[] possibleValues = { "First", "Second", "Third" }; // 用戶的選擇項目
Object selectedValue = JOptionPane.showInputDialog(null, "Choose one","Input",JOptionPane.INFORMATION_MESSAGE, null, possibleValues,possibleValues[0]);
System.out.println("您選擇的選項爲:"+selectedValue);
String inputValue = JOptionPane.showInputDialog("Please input a value");
System.out.println("您輸入的內容爲:"+inputValue);
}
public static void showOption(){
Object[] options = {" 確定 "," 取消 "," 幫助 "};
int response=JOptionPane.showOptionDialog(null, " 這是個選項對話框,用戶可以選擇自己的按鈕的個數 ", " 選項對話框標題 ",JOptionPane.YES_OPTION,
JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
if(response==0){
System.out.println(" 您按下了第 OK 按鈕 ");
} else if(response==1){
System.out.println(" 您按下了第 Cancel 按鈕 ");
} else if(response==2){
System.out.println(" 您按下了第 Help 按鈕 ");
}
}
}