【问题标题】:Changing JOptionPane background color based on If statement根据 If 语句更改 JOptionPane 背景颜色
【发布时间】:2013-11-24 19:59:26
【问题描述】:

我想根据 If 语句更改 JOptionPane 消息的背景颜色。如果用户点击一个按钮,则会出现绿色消息背景,否则会出现红色消息。使用以下代码,两条消息一个接一个地出现。我究竟做错了什么?谢谢

if (e.getSource() == cmdYes)
                 new UIManager();
            UIManager.put("OptionPane.background",new   ColorUIResource(0,255,0));
             UIManager.put("Panel.background",new ColorUIResource(0,255,0));
                JOptionPane.showMessageDialog(null, "Green Message", 
                        "Green", 
                        JOptionPane.INFORMATION_MESSAGE);

            if (e.getSource() == cmdNo)
                 new UIManager();
            UIManager.put("OptionPane.background",new ColorUIResource(255,0,0));
             UIManager.put("Panel.background",new ColorUIResource(255,0,0));
                JOptionPane.showMessageDialog(null, "Red Message", 
                        "Red", 
                        JOptionPane.INFORMATION_MESSAGE);

【问题讨论】:

  • 如何启动cmdYes和cmdNo

标签: java swing background joptionpane


【解决方案1】:

像这样改变。你错过了这个{} 括号..

if (e.getSource() == cmdYes) {
            new UIManager();
            UIManager.put("OptionPane.background", new ColorUIResource(0, 255, 0));
            UIManager.put("Panel.background", new ColorUIResource(0, 255, 0));
            JOptionPane.showMessageDialog(null, "Green Message", "Green", JOptionPane.INFORMATION_MESSAGE);
}
if (e.getSource() == cmdNo) {
            new UIManager();
            UIManager.put("OptionPane.background", new ColorUIResource(255, 0, 0));
            UIManager.put("Panel.background", new ColorUIResource(255, 0, 0));
            JOptionPane.showMessageDialog(null, "Red Message", "Red", JOptionPane.INFORMATION_MESSAGE);
}

【讨论】:

  • 啊!!我知道事情就是这么简单...谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-22
  • 1970-01-01
  • 1970-01-01
  • 2013-10-24
  • 1970-01-01
  • 2016-06-02
相关资源
最近更新 更多