java.lang.NullPointerException是什么错误?
以下是段java程序:点击addColor下的red时报java.lang.NullPointerException热切希望高手回答?importjavax.swing....
以下是段java程序:点击addColor下的red时报java.lang.NullPointerException
热切希望高手回答?
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.Color;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JMenuBar;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class MenuDemo extends JFrame implements ActionListener
{
public static final int WEIGHT=300;
public static final int HEIGHT=200;
private JPanel redPanel;
private JPanel whitePanel;
private JPanel bluePanel;
public static void main(String [] args)
{
MenuDemo m=new MenuDemo();
m.setVisible(true);
}
public MenuDemo()
{
super("MenuDemo");
setSize(WEIGHT,HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contentPane=getContentPane();
contentPane.setLayout(new GridLayout(1,3));
JPanel redPanel= new JPanel();
redPanel.setBackground(Color.LIGHT_GRAY);
contentPane.add(redPanel);
JPanel whitePanel=new JPanel();
contentPane.add(whitePanel);
whitePanel.setBackground(Color.LIGHT_GRAY);
JPanel bluePanel=new JPanel();
contentPane.add(bluePanel);
bluePanel.setBackground(Color.LIGHT_GRAY);
JMenu addColor= new JMenu("addColor");
JMenuItem redColor= new JMenuItem("red");
redColor.addActionListener(this);
addColor.add(redColor);
JMenuItem whiteColor=new JMenuItem("white");
whiteColor.addActionListener(this);
addColor.add(whiteColor);
JMenuItem blueColor=new JMenuItem("blue");
blueColor.addActionListener(this);
addColor.add(blueColor);
JMenuBar bar=new JMenuBar();
bar.add(addColor);
setJMenuBar(bar);
}
public void actionPerformed (ActionEvent e)
{
String buttonString= e.getActionCommand();
if (buttonString.equals("red"))
redPanel.setBackground(Color.RED);
else if(buttonString.equals("white"))
whitePanel.setBackground(Color.WHITE);
else if(buttonString.equals("blue"))
bluePanel.setBackground(Color.BLUE);
else
System.out.println("Unexpexted error.");
}
} 展开
热切希望高手回答?
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.Color;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JMenuBar;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class MenuDemo extends JFrame implements ActionListener
{
public static final int WEIGHT=300;
public static final int HEIGHT=200;
private JPanel redPanel;
private JPanel whitePanel;
private JPanel bluePanel;
public static void main(String [] args)
{
MenuDemo m=new MenuDemo();
m.setVisible(true);
}
public MenuDemo()
{
super("MenuDemo");
setSize(WEIGHT,HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contentPane=getContentPane();
contentPane.setLayout(new GridLayout(1,3));
JPanel redPanel= new JPanel();
redPanel.setBackground(Color.LIGHT_GRAY);
contentPane.add(redPanel);
JPanel whitePanel=new JPanel();
contentPane.add(whitePanel);
whitePanel.setBackground(Color.LIGHT_GRAY);
JPanel bluePanel=new JPanel();
contentPane.add(bluePanel);
bluePanel.setBackground(Color.LIGHT_GRAY);
JMenu addColor= new JMenu("addColor");
JMenuItem redColor= new JMenuItem("red");
redColor.addActionListener(this);
addColor.add(redColor);
JMenuItem whiteColor=new JMenuItem("white");
whiteColor.addActionListener(this);
addColor.add(whiteColor);
JMenuItem blueColor=new JMenuItem("blue");
blueColor.addActionListener(this);
addColor.add(blueColor);
JMenuBar bar=new JMenuBar();
bar.add(addColor);
setJMenuBar(bar);
}
public void actionPerformed (ActionEvent e)
{
String buttonString= e.getActionCommand();
if (buttonString.equals("red"))
redPanel.setBackground(Color.RED);
else if(buttonString.equals("white"))
whitePanel.setBackground(Color.WHITE);
else if(buttonString.equals("blue"))
bluePanel.setBackground(Color.BLUE);
else
System.out.println("Unexpexted error.");
}
} 展开
展开全部
将构造函数中这三句话
JPanel redPanel= new JPanel();
JPanel whitePanel=new JPanel();
JPanel bluePanel=new JPanel();
改成
redPanel= new JPanel();
whitePanel=new JPanel();
bluePanel=new JPanel();
你这样写是在构造函数中重新声明了3个JPanel的局部变量并实例化,
而不是将3个JPanel类成员变量实例化。
JPanel redPanel= new JPanel();
JPanel whitePanel=new JPanel();
JPanel bluePanel=new JPanel();
改成
redPanel= new JPanel();
whitePanel=new JPanel();
bluePanel=new JPanel();
你这样写是在构造函数中重新声明了3个JPanel的局部变量并实例化,
而不是将3个JPanel类成员变量实例化。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询