你的JMenuBar对象add了自己,我在修改的地方添加了标记(//**),修改如下,不懂得地方再发短消息给我:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class ActionSample extends JFrame
{
Action sampleAction;
Action exitAction;
public ActionSample()
{
super("Using Actions");
sampleAction=new AbstractAction()
{
public void actionPerformed(ActionEvent event)
{
JOptionPane.showMessageDialog(ActionSample.this,
"The SampleAction was invoked");
exitAction.setEnabled(true);
}
};
sampleAction.putValue(Action.NAME,"Sample Action");
sampleAction.putValue(Action.SMALL_ICON,new ImageIcon("help.gif"));
sampleAction.putValue(Action.SHORT_DESCRIPTION,"A Sample Action");
sampleAction.putValue(Action.MNEMONIC_KEY,new Integer('s'));
exitAction=new AbstractAction()
{
public void actionPerformed(ActionEvent event)
{
JOptionPane.showMessageDialog(ActionSample.this,
"The exitAction was invoked");
System.exit(0);
}
};
exitAction.putValue(Action.NAME,"Exit");
exitAction.putValue(Action.SMALL_ICON,new ImageIcon("exit.gif"));
exitAction.putValue(Action.SHORT_DESCRIPTION,"Exit Application");
exitAction.putValue(Action.MNEMONIC_KEY,new Integer('x'));
exitAction.setEnabled(false);
JMenu fileMenu=new JMenu("File");
fileMenu.add(sampleAction);
fileMenu.add(exitAction);
fileMenu.setMnemonic('F');
JMenuBar menuBar=new JMenuBar();
menuBar.add(fileMenu);
//menuBar.add(menuBar); //**不可以自己添加自己,会抛出异常
JToolBar toolBar=new JToolBar();
toolBar.add(sampleAction);
toolBar.add(exitAction);
JButton sampleButton=new JButton();
sampleButton.setAction(sampleAction);
JButton exitButton=new JButton(exitAction);
JPanel buttonPanel=new JPanel();
buttonPanel.add(sampleButton);
buttonPanel.add(exitButton);
Container container=getContentPane();
container.add(toolBar,BorderLayout.NORTH);
container.add(buttonPanel,BorderLayout.CENTER);
}
public static void main(String[] args)
{
ActionSample sample=new ActionSample();
sample.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
sample.pack();
sample.setVisible(true);
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class ActionSample extends JFrame
{
Action sampleAction;
Action exitAction;
public ActionSample()
{
super("Using Actions");
sampleAction=new AbstractAction()
{
public void actionPerformed(ActionEvent event)
{
JOptionPane.showMessageDialog(ActionSample.this,
"The SampleAction was invoked");
exitAction.setEnabled(true);
}
};
sampleAction.putValue(Action.NAME,"Sample Action");
sampleAction.putValue(Action.SMALL_ICON,new ImageIcon("help.gif"));
sampleAction.putValue(Action.SHORT_DESCRIPTION,"A Sample Action");
sampleAction.putValue(Action.MNEMONIC_KEY,new Integer('s'));
exitAction=new AbstractAction()
{
public void actionPerformed(ActionEvent event)
{
JOptionPane.showMessageDialog(ActionSample.this,
"The exitAction was invoked");
System.exit(0);
}
};
exitAction.putValue(Action.NAME,"Exit");
exitAction.putValue(Action.SMALL_ICON,new ImageIcon("exit.gif"));
exitAction.putValue(Action.SHORT_DESCRIPTION,"Exit Application");
exitAction.putValue(Action.MNEMONIC_KEY,new Integer('x'));
exitAction.setEnabled(false);
JMenu fileMenu=new JMenu("File");
fileMenu.add(sampleAction);
fileMenu.add(exitAction);
fileMenu.setMnemonic('F');
JMenuBar menuBar=new JMenuBar();
menuBar.add(fileMenu);
//menuBar.add(menuBar); //**不可以自己添加自己,会抛出异常
JToolBar toolBar=new JToolBar();
toolBar.add(sampleAction);
toolBar.add(exitAction);
JButton sampleButton=new JButton();
sampleButton.setAction(sampleAction);
JButton exitButton=new JButton(exitAction);
JPanel buttonPanel=new JPanel();
buttonPanel.add(sampleButton);
buttonPanel.add(exitButton);
Container container=getContentPane();
container.add(toolBar,BorderLayout.NORTH);
container.add(buttonPanel,BorderLayout.CENTER);
}
public static void main(String[] args)
{
ActionSample sample=new ActionSample();
sample.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
sample.pack();
sample.setVisible(true);
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货