更改JOptionPane按钮属性和背景颜色

时间:2014-09-26 12:04:34

标签: java swing button joptionpane

我已使用UIManager设置修改了JOptionPane的字体,但我无法找到更改按钮属性的属性。 例如。我想删除文本周围的边框,如下所示。另外,我想删除Y和N下的下划线,如下所示。

enter image description here

此外,我可以将JOptionPane的背景颜色从这种灰白色改变为其他颜色吗?

1 个答案:

答案 0 :(得分:0)

您可以更改单个对话框:

您需要customJDialog和自定义JOptionPane。按钮突出显示与助记符匹配的第一个字母。将其更改为未使用的内容。矩形是关于按钮焦点。

JDialog dialog = new JDialog();

JOptionPane pane = new JOptionPane("Some Message",
JOptionPane.PLAIN_MESSAGE, JOptionPane.YES_NO_OPTION);

List<JButton> list = SwingUtils.getDescendantsOfType(JButton.class, pane);
//this method is part of the SwingUtils. It is free to use. You can download it from the link.

pane.setBackground(Color.green); //this does not completely change the background. I am not sure how you can change it completely

for (JButton b : list) {
    b.setMnemonic(0); //call it with an int or char that does not exist in your button.
    b.setFocusable(false); //removes the rectangle
    b.setBackground(Color.red);
    //you can do more since you have direct access to the button
}

如果您没有使用java自定义JOptionPane的经验,请查看按钮操作:Pressing Yes/No on embedded JOptionPane has no effect

SwingUtil链接:http://tips4java.wordpress.com/2008/11/13/swing-utils/

如果您想将代码转换为库,请阅读:http://www.programcreek.com/2011/07/build-a-java-library-for-yourself/