一个关于背景颜色改变的问题
这是一个改变背景颜色的程序,但运行后颜色没改变阿?
请帮忙看看。谢谢
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class FirstPanel extends JPanel
implements ActionListener
{ public FirstPanel()
{ JButton redButton = new JButton("Red");
JButton blueButton = new JButton("Blue");
JButton yellowButton = new JButton("Yellow");
add(redButton);
add(blueButton);
add(yellowButton);
redButton.addActionListener(this);
blueButton.addActionListener(this);
yellowButton.addActionListener(this);
}
public void actionPerformed(ActionEvent evt)
{ Object source = evt.getSource();
Color color = getBackground();
if (source == redButton) color = Color.red;
else if (source == blueButton) color = Color.blue;
else if (source == yellowButton) color = Color.yellow;
setBackground(color);
repaint();
}
private JButton redButton;
private JButton blueButton;
private JButton yellowButton;
}
class FirstFrame extends JFrame
{ public FirstFrame()
{ setTitle("FirstFrame");
setSize(300, 200);
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{ System.exit(0);
}
});
Container contentPane = getContentPane();
contentPane.add(new FirstPanel());
}
}
public class FirstTest
{ public static void main(String[] args)
{ JFrame frame = new FirstFrame();
frame.setVisible(true);
}
}
请帮忙看看。谢谢
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class FirstPanel extends JPanel
implements ActionListener
{ public FirstPanel()
{ JButton redButton = new JButton("Red");
JButton blueButton = new JButton("Blue");
JButton yellowButton = new JButton("Yellow");
add(redButton);
add(blueButton);
add(yellowButton);
redButton.addActionListener(this);
blueButton.addActionListener(this);
yellowButton.addActionListener(this);
}
public void actionPerformed(ActionEvent evt)
{ Object source = evt.getSource();
Color color = getBackground();
if (source == redButton) color = Color.red;
else if (source == blueButton) color = Color.blue;
else if (source == yellowButton) color = Color.yellow;
setBackground(color);
repaint();
}
private JButton redButton;
private JButton blueButton;
private JButton yellowButton;
}
class FirstFrame extends JFrame
{ public FirstFrame()
{ setTitle("FirstFrame");
setSize(300, 200);
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{ System.exit(0);
}
});
Container contentPane = getContentPane();
contentPane.add(new FirstPanel());
}
}
public class FirstTest
{ public static void main(String[] args)
{ JFrame frame = new FirstFrame();
frame.setVisible(true);
}
}
...全文
相关推荐
改变窗体背景颜色
可以根据个人喜爱随意改变窗体、控件的背景颜色编程技巧:1、改变窗体、控件背景色2、调出系统调色板3、读写注册表
易语言改变编辑框背景颜色的方法
今天小编就为大家分享一篇关于易语言改变编辑框背景颜色的方法,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
C# TabConTrol控件背景颜色问题
C# TabConTrol控件背景颜色问题 PDF
DBGrid改变背景颜色Delphi源代码
DBGrid改变背景颜色Delphi源代码
Javascript网页特效(2)在线改变背景颜色
Javascript网页特效(2)在线改变背景颜色 背景颜色随输入的颜色代码不同而改变,注意输入框在网页顶部,你可以在设计中合适的安置。txt文件是JavaScript的源代码


Java SE
