/*Exception in thread "main" java.lang.NullPointerException
at MenuTest.<init>(MenuTest.java:75)
at MenuTest.main(MenuTest.java:108)
Press any key to continue...
为何运行时出现以上错误.
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MenuTest extends JFrame
{
private Color colorValue[] = {Color.black,Color.blue,Color.red,Color.green};
private JRadioButtonMenuItem colorItems[],fonts[];
private JCheckBoxMenuItem styleItems[];
private JLabel displayLabel;
private ButtonGroup fontGroup,colorGroup;
private int style;
public MenuTest()
{
super("Using JMenus!");
JMenu fileMenu = new JMenu("File");
fileMenu.setMnemonic('F');
JMenuItem aboutItem = new JMenuItem("About...");
aboutItem.setMnemonic('A');
aboutItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
JOptionPane.showMessageDialog(MenuTest.this,
"This is an example\n of using menus","About",JOptionPane.PLAIN_MESSAGE);
}
});
fileMenu.add(aboutItem);
JMenuItem exitItem = new JMenuItem("Exit");
exitItem.setMnemonic('X');
exitItem.addActionListener( new ActionListener(){
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
fileMenu.add(exitItem);
JMenuBar bar = new JMenuBar();
setJMenuBar(bar);
bar.add(fileMenu);
JMenu formatMenu = new JMenu("Format");
formatMenu.setMnemonic('r');
String colors[] = {"Black","Blue","Red","Green"};
JMenu colorMenu = new JMenu("Color");
colorMenu.setMnemonic('c');
colorItems = new JRadioButtonMenuItem[colors.length];
colorGroup = new ButtonGroup();
ItemHandler itemHandler = new ItemHandler();
for(int count = 0;count<colors.length;count++)
{
colorItems[count] = new JRadioButtonMenuItem(colors[count]);
colorMenu.add(colorItems[count]);
colorGroup.add(colorItems[count]);
colorItems[count].addActionListener(itemHandler);
}
colorItems[0].setSelected(true);
formatMenu.add(colorMenu);
formatMenu.addSeparator();
String fontNames[] = {"Serif","Monospaced","SansSerif"};
JMenu fontMenu = new JMenu("Font");
fontMenu.setMnemonic('n');
fonts = new JRadioButtonMenuItem[fontNames.length];
for(int count = 0;count<fonts.length;count++)
{
fonts[count] = new JRadioButtonMenuItem(fontNames[count]);
fontMenu.add(fonts[count]);
fontGroup.add(fonts[count]);
fonts[count].addActionListener(itemHandler);
}
fonts[0].setSelected(true);
fontMenu.addSeparator();
String styleNames[] = {"Bold","Italic"};
styleItems = new JCheckBoxMenuItem[styleNames.length];
StyleHandler styleHandler = new StyleHandler();
for(int count = 0;count<styleNames.length;count++)
{
styleItems[count] = new JCheckBoxMenuItem(styleNames[count]);
fontMenu.add(styleItems[count]);
styleItems[count].addItemListener(styleHandler);
}
formatMenu.add(fontMenu);
bar.add(formatMenu);
displayLabel = new JLabel("Sample Text",SwingConstants.CENTER );
displayLabel.setForeground(colorValue[0]);
displayLabel.setFont( new Font("TimesRoman",Font.PLAIN,72));
getContentPane().setBackground(Color.cyan);
getContentPane().add(displayLabel,BorderLayout.CENTER);
setSize(500,200);
setVisible(true);
}//end construct mathod MenuTest
public static void main(String agrs[])
{
MenuTest application = new MenuTest();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}//end main mathod
private class ItemHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
for(int count = 0;count<colorItems.length;count++)
{
if(colorItems[count].isSelected())
{
displayLabel.setForeground(colorValue[count]);
break;
} }
for(int count= 0;count<fonts.length;count++)
{
if(e.getSource()==fonts[count])
{
displayLabel.setFont(new Font(fonts[count].getText(),style,72));
break;
}
}
repaint();
}//end mathod actionPerformed
}//end class ItemHander
private class StyleHandler implements ItemListener
{
public void itemStateChanged(ItemEvent e)
{
style = 0;
if(styleItems[0].isSelected())
{
style+=Font.BOLD;
}
if(styleItems[1].isSelected())
{
style += Font.ITALIC;
}
displayLabel.setFont(new Font(displayLabel.getFont().getName(),style,72));
repaint();
}//end mathod itemStateChanged
} //end mathod StyleHander
}//end class MenuTest
fontGroup = new ButtonGroup();
没初始化,当然空指针异常啦~
JAVA里有指针的概念,你不要误导新手
那个异常是说你使用了没有实例化的对象.
像以后遇到问题可以先去查一下API,在API里会有比较详细的解释,那样可以提高自己的能力.
你把他翻译成什么?
java没有指针只是语言层面上没有指针
实际上到处是指针
在定义后只对colorGroup实例化,
fontGroup没有进行实例化,所以报空指针异常。
fontGroup= new ButtonGroup();
养成定义后就实例化的好习惯,防止空指针异常。
一般都是这么叫啊.