JAVA事件处理,改变窗体背景颜色...
下面这个程序为什么改变不了窗体的背景颜色用Swingimportjava.awt.*;importjavax.swing.*;importjava.awt.event.*...
下面这个程序为什么改变不了窗体的背景颜色
用Swing
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class ActionEventExample extends JFrame{
public ActionEventExample(){
setTitle("按钮改变面板的背景色");
setSize(300,150);
Container con = getContentPane();
JPanel panel = new JPanel();
JButton yellowButton = new JButton("Yellow");
JButton blueButton = new JButton("Blue");
JButton redButton = new JButton("Red");
panel.add(yellowButton);
panel.add(blueButton);
panel.add(redButton);
con.add(panel);
redButton.addActionListener(new ColorAction(this,Color.red));
yellowButton.addActionListener(new ColorAction(this,Color.yellow));
blueButton.addActionListener(new ColorAction(this,Color.blue));
}
public static void main(String[] args){
ActionEventExample frame = new ActionEventExample();
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.show();
}
}
class ColorAction implements ActionListener{
private ActionEventExample frame;
private Color backgroundcolor;
public ColorAction(ActionEventExample aef,Color c) {
frame = aef;
backgroundcolor = c;
}
public void actionPerformed(ActionEvent e) {
frame.setBackground(backgroundcolor);
}
}
下面这个程序可以改变背景颜色
用AWT
import java.awt.*;
import java.awt.event.*;
class ColorAction implements ActionListener {
private ActionEventExample frame;
private Color backgroundcolor;
public ColorAction(ActionEventExample aef,Color c) {
frame = aef;
backgroundcolor = c;
}
public void actionPerformed(ActionEvent e) {
frame.setBackground(backgroundcolor);
}
}
public class ActionEventExample extends Frame{
ActionEventExample() {
setLayout(new FlowLayout());
setTitle("按钮改变面板的背景色");
setSize(300,150);
setVisible(true);
Button redbutton = new Button("红色");
Button yellowbutton = new Button("黄色");
Button bluebutton = new Button("蓝色");
add(redbutton);
add(yellowbutton);
add(bluebutton);
redbutton.addActionListener(new ColorAction(this,Color.red));
yellowbutton.addActionListener(new ColorAction(this,Color.yellow));
bluebutton.addActionListener(new ColorAction(this,Color.blue));
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(1);
}
});
}
public static void main(String[] args) {
new ActionEventExample();
}
}
在用Swing和AWT各需要注意什么~~~~ 展开
用Swing
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class ActionEventExample extends JFrame{
public ActionEventExample(){
setTitle("按钮改变面板的背景色");
setSize(300,150);
Container con = getContentPane();
JPanel panel = new JPanel();
JButton yellowButton = new JButton("Yellow");
JButton blueButton = new JButton("Blue");
JButton redButton = new JButton("Red");
panel.add(yellowButton);
panel.add(blueButton);
panel.add(redButton);
con.add(panel);
redButton.addActionListener(new ColorAction(this,Color.red));
yellowButton.addActionListener(new ColorAction(this,Color.yellow));
blueButton.addActionListener(new ColorAction(this,Color.blue));
}
public static void main(String[] args){
ActionEventExample frame = new ActionEventExample();
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.show();
}
}
class ColorAction implements ActionListener{
private ActionEventExample frame;
private Color backgroundcolor;
public ColorAction(ActionEventExample aef,Color c) {
frame = aef;
backgroundcolor = c;
}
public void actionPerformed(ActionEvent e) {
frame.setBackground(backgroundcolor);
}
}
下面这个程序可以改变背景颜色
用AWT
import java.awt.*;
import java.awt.event.*;
class ColorAction implements ActionListener {
private ActionEventExample frame;
private Color backgroundcolor;
public ColorAction(ActionEventExample aef,Color c) {
frame = aef;
backgroundcolor = c;
}
public void actionPerformed(ActionEvent e) {
frame.setBackground(backgroundcolor);
}
}
public class ActionEventExample extends Frame{
ActionEventExample() {
setLayout(new FlowLayout());
setTitle("按钮改变面板的背景色");
setSize(300,150);
setVisible(true);
Button redbutton = new Button("红色");
Button yellowbutton = new Button("黄色");
Button bluebutton = new Button("蓝色");
add(redbutton);
add(yellowbutton);
add(bluebutton);
redbutton.addActionListener(new ColorAction(this,Color.red));
yellowbutton.addActionListener(new ColorAction(this,Color.yellow));
bluebutton.addActionListener(new ColorAction(this,Color.blue));
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(1);
}
});
}
public static void main(String[] args) {
new ActionEventExample();
}
}
在用Swing和AWT各需要注意什么~~~~ 展开
2个回答
展开全部
改变不了颜色是因为你上面的那个程序在主窗口上又加了一个Panel,而你去改变的却是主窗口的背景色,你可以把按钮直接这样写:con.add(redButton);……去掉panel,然后在actionPerformed方法中这样写:frame.getContentPane().setBackground(backgroundcolor);或者你去改变panel的背景色。
追问
为什么得这样写:frame.getContentPane().setBackground(backgroundcolor);
而不能是:frame.setBackground(backgroundcolor);
追答
这个问题我还真解释不了,网上也没找到合理的解释。只有这些:“
在使用JFrame.setBackground(new Color);是发现这个方法调用后并没有改变JFrame窗口的背景颜色,这体现了点JFrame和Frame的区别:
JFrame要用:
JFrame.getContentPane().setBackground(new Color);
来改变JFrame的背景颜色;
Frame中则可以直接使用:
Frame.setBackground(new Color);”
我想要搞清楚这个问题可能需要研究一下Swing与AWT组件模型之类的东西吧,我猜应该是JFrame在Frame之上添加了一个最基本的容器——就是getContentPane()方法获取的,你设置的Frame的背景应该还是被这个组件盖着的,这只是我的猜想不知道对不对,你自己再查查资料吧。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询