求指导——菜单栏菜单监听器不响应
有界面,运行时
NullPointerException,不知道哪不对,求指导,代码如下
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MenuFrame implements ActionListener,ItemListener{
JFrame mFrame;
JMenuBar menuBar;
JMenu menu,help,subMenu;
JTextArea textArea;
JMenuItem redItem,blueItem,exitItem,helpItem;
JCheckBoxMenuItem cbItem;
public void go(){
mFrame=new JFrame();
mFrame.setSize(600,400);
mFrame.setResizable(false);
JTextArea textArea=new JTextArea();
textArea.setEditable(false);
JScrollPane scroll=new JScrollPane(textArea);
scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
menuBar=new JMenuBar ();
menu=new JMenu("Manage");
menuBar.add(menu);
subMenu=new JMenu("Color");
menu.add(subMenu);
redItem=new JMenuItem("Red");
blueItem=new JMenuItem("Blue");
subMenu.add(redItem);
subMenu.add(blueItem);
menu.addSeparator();
cbItem=new JCheckBoxMenuItem("Check Box");
menu.add(cbItem);
exitItem=new JMenuItem("Exit");
menu.add(exitItem);
help=new JMenu("Help");
//menuBar.setHelpMenu(help);
menuBar.add(help);
helpItem=new JMenuItem("help");
help.add(helpItem);
mFrame.setJMenuBar(menuBar);
mFrame.getContentPane().add(scroll,BorderLayout.CENTER);
mFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mFrame.setVisible(true);
redItem.addActionListener(new RedListener());
blueItem.addActionListener(new BlueListener());
cbItem.addItemListener(new CBListener());
helpItem.addActionListener(new HelpListener());
exitItem.addActionListener(new ExitListener());
}
public static void main(String args[]) throws Exception{
MenuFrame frame=new MenuFrame();
frame.go();
}
class RedListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
textArea.setForeground(Color.RED);
System.out.println("change color");
}
}
class BlueListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
textArea.setForeground(Color.BLUE);
System.out.println("change color");
}
}
class CBListener implements ItemListener{
public void itemStateChanged(ItemEvent e) {