根据If语句更改JOptionPane背景颜色

时间:2013-11-24 19:59:26

标签: java swing background joptionpane

我想基于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);

1 个答案:

答案 0 :(得分:2)

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

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