java - 设置文本背景颜色?

标签 java swing jtextfield look-and-feel nimbus

如何在 JOptionPane 中设置文本背景颜色?

图片:

enter image description here

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;
}

输出:

PANEIMAGE


更新 1:

否则你可以试试这个来改变一切,

uimanager.put("OptionPane.background", Color.BLUE);
uimanager.put("OptionPane.messagebackground", Color.BLUE);
uimanager.put("Panel.background", Color.BLUE);

更新输出:

OPTIONPANEIMAGE

关于java - 设置文本背景颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17920182/

相关文章:

java - JTextField 上的最大值

java - 为什么我需要添加 Material Theme 依赖项才能解析 DocumentFile

java - 在 GlassFish 中配置 jdbcRealm 的表命名约定

Java 拖动时始终将 JLabel 保持在 JPanel 上方

java swing JTextField 设置 PlaceHolder

java - 'sanitize' JTextField 输入的更清晰方法?

java - 我在 sqlite 中第一次插入会发生什么?

java - 检查扑克牌

java - 多线程应用程序中编译时和运行时类路径不匹配的原因是什么?

java - 如何停止在 java 中重叠两个 jpanel