| 方法 | 用途 | 回傳值 | 常用圖示常數 |
|---|---|---|---|
showMessageDialog | 單純訊息通知 | 無 | ERROR_MESSAGE, WARNING_MESSAGE, INFORMATION_MESSAGE, PLAIN_MESSAGE |
showConfirmDialog | 是/否/取消 確認 | int: YES/NO/CANCEL/OK/CLOSED_OPTION | QUESTION_MESSAGE |
showOptionDialog | 自訂按鈕選項 | int: 按鈕索引 (0-based) | 自訂 |
showInputDialog | 文字輸入 / 下拉選擇 | String / Object (使用者輸入/選擇) | QUESTION_MESSAGE, INFORMATION_MESSAGE |
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class JOptionPaneDemo extends JFrame {
public JOptionPaneDemo() {
setTitle("JOptionPane 完整示範");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(400, 200);
setLocationRelativeTo(null);
setLayout(new FlowLayout());
String[] actions = {
"1. 錯誤訊息", "2. 確認對話框 (Yes/No/Cancel)",
"3. 自訂按鈕選項", "4. 警告含自訂按鈕",
"5. 文字輸入框", "6. 下拉選擇框"
};
for (String label : actions) {
JButton btn = new JButton(label);
btn.addActionListener(e -> handleAction(label));
add(btn);
}
}
private void handleAction(String cmd) {
switch (cmd.charAt(0)) {
case '1': // 錯誤訊息
JOptionPane.showMessageDialog(this,
"資料庫連線失敗!\n請檢查網路設定。",
"系統錯誤", JOptionPane.ERROR_MESSAGE);
break;
case '2': // 確認對話框
int result = JOptionPane.showConfirmDialog(this,
"離開前要儲存變更嗎?",
"確認儲存", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
if (result == JOptionPane.YES_OPTION) saveAndExit();
else if (result == JOptionPane.NO_OPTION) exitWithoutSave();
break;
case '3': // 自訂按鈕
Object[] opts = {"繼續執行", "暫停", "取消"};
int idx = JOptionPane.showOptionDialog(this,
"偵測到異常,如何處理?", "異常處理",
JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE,
null, opts, opts[0]); // 預設聚焦「繼續執行」
System.out.println("使用者選擇: " + opts[idx]);
break;
case '4': // 警告含自訂按鈕 (同 case 3,示範 ICON 差異)
JOptionPane.showMessageDialog(this,
"此操作將永久刪除資料!",
"危險操作", JOptionPane.WARNING_MESSAGE);
break;
case '5': // 文字輸入
String name = JOptionPane.showInputDialog(this,
"請輸入您的姓名:", "歡迎", JOptionPane.QUESTION_MESSAGE);
if (name != null && !name.trim().isEmpty())
System.out.println("姓名: " + name.trim());
break;
case '6': // 下拉選擇
Object[] choices = {"工程師", "設計師", "產品經理", "資料科學家"};
Object sel = JOptionPane.showInputDialog(this,
"選擇職稱:", "人員登記",
JOptionPane.INFORMATION_MESSAGE, null, choices, choices[0]);
if (sel != null) System.out.println("職稱: " + sel);
break;
}
}
private void saveAndExit() { System.out.println("已儲存,離開"); System.exit(0); }
private void exitWithoutSave() { System.out.println("未儲存,離開"); System.exit(0); }
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new JOptionPaneDemo().setVisible(true));
}
}
ImageIcon icon = new ImageIcon(getClass().getResource("/icons/custom.png"));
JOptionPane.showMessageDialog(null, "自訂圖示", "標題", JOptionPane.PLAIN_MESSAGE, icon);
JPanel panel = new JPanel(new GridLayout(0,1));
panel.add(new JLabel("帳號:")); panel.add(new JTextField(15));
panel.add(new JLabel("密碼:")); panel.add(new JPasswordField(15));
int ok = JOptionPane.showConfirmDialog(null, panel, "登入", JOptionPane.OK_CANCEL_OPTION);
if (ok == JOptionPane.OK_OPTION) { /* 取得欄位值 */ }
JDialog dialog = new JDialog(frame, "進度", false); // false = non-modal
dialog.add(new JProgressBar());
dialog.pack(); dialog.setLocationRelativeTo(frame);
dialog.setVisible(true); // 程式繼續執行,不阻塞
UIManager.put("OptionPane.yesButtonText", "是");
UIManager.put("OptionPane.noButtonText", "否");
UIManager.put("OptionPane.cancelButtonText", "取消");
UIManager.put("OptionPane.okButtonText", "確定");
// 需在建立第一個 JOptionPane 前設定
// JavaFX (需 javafx.controls 模組)
Alert alert = new Alert(Alert.AlertType.CONFIRMATION, "儲存變更?", ButtonType.YES, ButtonType.NO);
alert.showAndWait().ifPresent(btn -> { if (btn == ButtonType.YES) save(); });
新專案建議採用 JavaFX;維護 Swing 專案時,`JOptionPane` 仍是最快速建立標準對話框的工具。