[英]Setting text background color?
如何在JOptionPane
设置文本背景颜色?
图片:
UIManager UI = new UIManager();
UI.put("OptionPane.background", Color.white);
UIManager.put("Button.background", Color.white);
UI.put("Panel.background", Color.white);
UI.put("OptionPane.foreground", Color.white);
UI.put("OptionPane.messagebackground", Color.white);
UI.put("OptionPane.textbackground", Color.white);
UI.put("OptionPane.warningDialog.titlePane.shadow", Color.white);
UI.put("OptionPane.warningDialog.border.background", Color.white);
UI.put("OptionPane.warningDialog.titlePane.background", Color.white);
UI.put("OptionPane.warningDialog.titlePane.foreground", Color.white);
UI.put("OptionPane.questionDialog.border.background", Color.white);
UI.put("OptionPane.questionDialog.titlePane.background", Color.white);
UI.put("OptionPane.questionDialog.titlePane.foreground", Color.white);
UI.put("OptionPane.questionDialog.titlePane.shadow", Color.white);
UI.put("OptionPane.messageForeground", Color.white);
UI.put("OptionPane.foreground", Color.white);
UI.put("OptionPane.errorDialog.border.background", Color.white);
UI.put("OptionPane.errorDialog.titlePane.background", Color.white);
UI.put("OptionPane.errorDialog.titlePane.foreground", Color.white);
UI.put("OptionPane.errorDialog.titlePane.shadow", Color.white);
JOptionPane.showMessageDialog(null, "Hello world", "HELLO WORLD", JOptionPane.INFORMATION_MESSAGE);
为什么不在这个地方添加自定义JPanel
,如下所示:
JOptionPane.showMessageDialog(frame, getLabelPanel(), "Hello World!",
JOptionPane.INFORMATION_MESSAGE);
您可以从一个方法获取JPanel
,例如:
private JPanel getLabelPanel() {
JPanel panel = new JPanel();
panel.setOpaque(true);
panel.setBackground(Color.BLUE);
JLabel helloLabel = new JLabel("Hello World!", JLabel.CENTER);
helloLabel.setForeground(Color.WHITE);
panel.add(helloLabel);
return panel;
}
输出:
否则你可以试试这个改变一切,
uimanager.put("OptionPane.background", Color.BLUE);
uimanager.put("OptionPane.messagebackground", Color.BLUE);
uimanager.put("Panel.background", Color.BLUE);
更新产量:
尝试
UIManager UI=new UIManager();
UI.put("OptionPane.background",new ColorUIResource(255,255,0));
UI.put("Panel.background",new ColorUIResource(255,255,0));
最干净的是使用JDialog创建自己的JOptionPane替换,就像nIcE cOw建议的那样。 JOptionPane
在其框架中包含大量垃圾,并且几个组件根本不检查JOptionPane
特定属性。
如果你仍然坚持使用JOptionPane,并且想要仅改变那些背景而不是应用程序中的所有内容,我会说如果你想为整个对话框设置其所有子组件的背景,你最好的选择是迭代所有窗口内容并在每个窗口内容上调用setBackground()
。 这可以在其UI类中完成,也可以单独用于使用JOptionPane.createDialog()
获得的对话框。 大概是这样的:
void colorComponentTree(Component component, Color color) {
if (component instanceof Container) {
if (component instanceof JComponent) {
((JComponent) component).setBackground(color);
}
for (Component child : ((Container) component).getComponents()) {
colorComponentTree(child, color);
}
}
}
然而,这真的很难看,我认真推荐采用自定义对话路线。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.