求java中getActionCommand方法的一个简单的实例。不懂getActionCommand方法是干什么用的,API看了不解
1个回答
展开全部
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class TestApp implements ActionListener
{
JFrame f = new JFrame("getActionCommand() Test");
public JMenuBar menuBar;
public TestApp(){
menuBar = new JMenuBar();
JMenu Option = new JMenu("Option");
ButtonGroup group = new ButtonGroup();
JRadioButtonMenuItem stadmodel = new JRadioButtonMenuItem("Standard", true);
stadmodel.addActionListener(this);
Option.add(stadmodel);
group.add(stadmodel);
JRadioButtonMenuItem scicemodel = new JRadioButtonMenuItem("Science");
scicemodel.addActionListener(this);
Option.add(scicemodel);
group.add(scicemodel);
Option.setMnemonic(KeyEvent.VK_O);
JMenu About = new JMenu("About");
JMenuItem about = new JMenuItem("About calculator");
About.add(about);
About.setMnemonic(KeyEvent.VK_A);
menuBar.add(Option);
menuBar.add(About);
about.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("About calculator"))
JOptionPane.showMessageDialog(null,
"JCaculator version 1.0\n copyright zsl\nahfaj@hotmail.com");
if(e.getActionCommand().equals("Science")) {
JOptionPane.showMessageDialog(null,
"Science selected"); }
if(e.getActionCommand().equals("Standard")) {
JOptionPane.showMessageDialog(null,
"Science selected"); }
}
public static void main(String[] args)
{
TestApp appFrame = new TestApp();
appFrame.f.setJMenuBar(appFrame.menuBar);
appFrame.f.setSize(500,250);
appFrame.f.setResizable(false);
appFrame.f.getContentPane().setLayout(new GridLayout());
appFrame.f.setVisible(true);
}
}
import java.awt.*;
import javax.swing.*;
public class TestApp implements ActionListener
{
JFrame f = new JFrame("getActionCommand() Test");
public JMenuBar menuBar;
public TestApp(){
menuBar = new JMenuBar();
JMenu Option = new JMenu("Option");
ButtonGroup group = new ButtonGroup();
JRadioButtonMenuItem stadmodel = new JRadioButtonMenuItem("Standard", true);
stadmodel.addActionListener(this);
Option.add(stadmodel);
group.add(stadmodel);
JRadioButtonMenuItem scicemodel = new JRadioButtonMenuItem("Science");
scicemodel.addActionListener(this);
Option.add(scicemodel);
group.add(scicemodel);
Option.setMnemonic(KeyEvent.VK_O);
JMenu About = new JMenu("About");
JMenuItem about = new JMenuItem("About calculator");
About.add(about);
About.setMnemonic(KeyEvent.VK_A);
menuBar.add(Option);
menuBar.add(About);
about.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("About calculator"))
JOptionPane.showMessageDialog(null,
"JCaculator version 1.0\n copyright zsl\nahfaj@hotmail.com");
if(e.getActionCommand().equals("Science")) {
JOptionPane.showMessageDialog(null,
"Science selected"); }
if(e.getActionCommand().equals("Standard")) {
JOptionPane.showMessageDialog(null,
"Science selected"); }
}
public static void main(String[] args)
{
TestApp appFrame = new TestApp();
appFrame.f.setJMenuBar(appFrame.menuBar);
appFrame.f.setSize(500,250);
appFrame.f.setResizable(false);
appFrame.f.getContentPane().setLayout(new GridLayout());
appFrame.f.setVisible(true);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询