在 JOptionPane showConfirmDialog 中更改按钮文本
Change Button text in JOptionPane showConfirmDialog
我在 JOptionPane.showConfirmDialog 中有一个 JPanel。我想将 Yes/No/Cancel 按钮更改为自定义值,但不确定如何更改。
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;
}
我尝试使用 UIManager 但没有成功。
以下代码是从 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
来源可以找到here。
我在 JOptionPane.showConfirmDialog 中有一个 JPanel。我想将 Yes/No/Cancel 按钮更改为自定义值,但不确定如何更改。
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;
}
我尝试使用 UIManager 但没有成功。
以下代码是从 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
来源可以找到here。