java swing-如何更改窗口背景 [英] java swing - how to change window background
本文介绍了java swing-如何更改窗口背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图创建一个将窗口背景从默认切换为红色的按钮.
我没有找到任何与默认颜色匹配的预设颜色,因此在创建它时尝试从panel.getBackground
中获取它. line 11
出现错误,我不知道如何检查当前背景色.
I was trying to make a button that switches the window background from default to red.
I haven't found any preset colors to match the default so i tried to get it from panel.getBackground
when i created it. I have an error at line 11
, i don't know how to check the current background color.
JPanel panel = new JPanel();
panel.setBounds(0, 0, 434, 262);
frame.getContentPane().add(panel);
panel.setLayout(null);
panel.setVisible(true);
Color c=panel.getBackground();
JButton btnRed = new JButton("Red");
btnRed.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(panel.getBackground(c));{
panel.setBackground(Color.RED);
}
else{
panel.setBackground(c);
}
}
});
推荐答案
您想比较Color
,这是一个Object
,所以equals()
是这样. c6>,并在找到;
后完成,因此:
You want to compare the Color
, this is an Object
so equals()
is the way... also an if
expects a boolean
and will finish when ;
is found, so this:
if(panel.getBackground(c));{
// ↑ not a boolean ↑
// ↑
// ; is wrong
必须
if(panel.getBackground().equals(c)){
这篇关于java swing-如何更改窗口背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文