就是按下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;
}
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;
}
解决方案 »
- 各位前辈请教一个多态的问题
- 一道小游戏
- 如何去除版本控制
- 怎么让JAVA的JtextField获得光标时,输入法为日文平假名
- 用utf-8编码(encode)unicode码时不会有信息损失,但禁止用utf-8解码非utf-8编码的字节流。总之Utf-8可以编码任何unicode 码,但只能解码
- String a=new String("foo");一共产生了几个对象?为什么??谢谢!
- java的关键字volatile有什么用途?
- 帮我,applet在visualage里做成后,导出到ie里(jar包),显示不全,无法运行????
- 寻找一种较为方便的java代码文本编辑器(比如带有语法及关键字变色功能)?
- 输出两个变量和失败
- 代码?
- 重写!
public void actionPerformed(ActionEvent event){
getContentPane()anegetsetBackground(Color.BLUE);
}
getContentPane().setBackground(Color.BLUE);
}
可以说下为啥要加上getContentPane()可以么??