修改后的程序:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class SimpleGui implements ActionListener
{
/**
* @param args
*/
public int a;
public JFrame aFrame;
public JMenuBar aMenuBar;
public JToolBar aToolBar;
public Container c;
public JMenu fileMenu, viewMenu,saveMenu;
public JMenuItem newMenuItem,openMenuItem,saveMenuItem,saveAsMenuItem;
public JButton firstButton,secondButton,thirdButton;
public SimpleGui()
{
aFrame = new JFrame( "The SimpleGui");
Container c=aFrame.getContentPane();
aMenuBar=new JMenuBar( );
aToolBar=new JToolBar();
fileMenu=new JMenu( "File");
fileMenu.setMnemonic('F');
viewMenu=new JMenu("View");
viewMenu.setMnemonic('V');
saveMenu=new JMenu("Save");
saveMenu.setMnemonic('S');
newMenuItem=new JMenuItem("New" ,'N');
newMenuItem.setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_N,InputEvent.CTRL_MASK,false));
openMenuItem =new JMenuItem("Open",'O' );
saveMenuItem =new JMenuItem("Save",'S' );
saveMenuItem.setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_S,InputEvent.CTRL_MASK,false));
saveAsMenuItem =new JMenuItem("SaveAs");
firstButton=new JButton("Green");
secondButton=new JButton("Yellow");
thirdButton=new JButton("Orange");
//
aMenuBar.add(fileMenu);
aMenuBar.add(viewMenu);
aMenuBar.add(saveMenu);
fileMenu.add(newMenuItem);
fileMenu.add(openMenuItem);
fileMenu.addSeparator( );
fileMenu.add(saveMenu);
saveMenu.add(saveMenuItem);
saveMenu.add(saveAsMenuItem);
aToolBar.add(firstButton);
aToolBar.add(secondButton);
aToolBar.add(thirdButton);
//
c.setLayout(new BorderLayout());
c.add(aToolBar,BorderLayout.NORTH);
firstButton.addActionListener(this);
secondButton.addActionListener(this);
thirdButton.addActionListener(this);
aFrame.setJMenuBar(aMenuBar);
aFrame.setSize(400,250);
aFrame.setVisible(true);
aFrame.setDefaultCloseOperation
(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e)
{
JButton button = (JButton)e.getSource();
if(button == firstButton)
c.setBackground(Color.green);
if(button == secondButton)
c.setBackground(Color.yellow);
else if(button == thirdButton)
c.setBackground(Color.orange);
}
public static void main(String[] args)
{
// TODO 自动生成方法存根
new SimpleGui();
}
}
编译没有问题,这是运行时的错误
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at SimpleGui.actionPerformed(SimpleGui.java:73)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
[此贴子已经被作者于2007-6-20 12:32:59编辑过]