就是按下red或blue的时候,背景的颜色不改变
import javax.swing.*;
import java.awt.*;
import java.awt.event .*;public class Test1{
public static void main(String[] args){
TestFrame frame = new TestFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}class TestFrame extends JFrame{
public TestFrame(){
setTitle("wa haha");
setSize(width,height);

JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);

JMenu editMenu = new JMenu("Edit");
menuBar.add(editMenu);
JMenu helpMenu = new JMenu("Color");
menuBar.add(helpMenu);
JMenu fileMenu = new JMenu("File");
menuBar.add(fileMenu);
JMenuItem red = new JMenuItem("red");
helpMenu.add(red);
red.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event){
setBackground(Color.BLUE);
}
}
);
JMenuItem blue = new JMenuItem("blue");
helpMenu.add(blue);
blue.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event){
setBackground(Color.BLUE);
}
}
);
}
private static final int width = 300;
private static final int height = 200;
}

解决方案 »

  1.   


    public void actionPerformed(ActionEvent event){
    getContentPane()anegetsetBackground(Color.BLUE);
    }
      

  2.   

    public void actionPerformed(ActionEvent event){
    getContentPane().setBackground(Color.BLUE);
    }
      

  3.   

    谢谢。。
    可以说下为啥要加上getContentPane()可以么??
      

  4.   

    只有你得到了当前容器,才能进行诸如setBackground之类的操作
      

  5.   

    JFrame对应的是WINDOWS的窗口,操作起来受很多限制 最好加个JPANEL上面或getContentPane()