在JOptionPane showConfirmDialog中更改Button文本

Saj*_*Saj 1 java swing jpanel joptionpane

我在JOptionPane.showConfirmDialog中有一个JPanel.我想将是/否/取消按钮更改为自定义值但不确定如何.

    private void displayGUI(String msg) {
    UIManager.put("OptionPane.okButtonText", "SNOOZE");
    final int selectedVal = JOptionPane.showConfirmDialog(null, getPanel(msg));
    if (selectedVal == JOptionPane.OK_OPTION){
        System.out.println("Ok button has been pressed.");
    }
}

private JPanel getPanel(String msg) {
    JPanel panel = new JPanel();
    JLabel label = new JLabel (msg, JLabel.CENTER);
    label.setFont (label.getFont ().deriveFont (32.0f));
    panel.setPreferredSize(new Dimension(500,500));
    panel.add(label);

    return panel;
}
Run Code Online (Sandbox Code Playgroud)

我尝试使用UIManager,但没有成功.

小智 13

以下代码是从oracle的文档站点中获取的.它似乎工作得很好.

Object[] options = {"Yes, please",
                "No way!"};
int n = JOptionPane.showOptionDialog(frame,
"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
Run Code Online (Sandbox Code Playgroud)

产量

来源可以在这里找到.