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