getActionCommand() - Method in class java.awt.Button Returns the command name of the action event fired by this button. getActionCommand() - Method in class java.awt.MenuItem Gets the command name of the action event that is fired by this menu item. getActionCommand() - Method in class java.awt.event.ActionEvent Returns the command string associated with this action. getActionCommand() - Method in class javax.swing.DefaultButtonModel Returns the action command for this button. getActionCommand() - Method in class javax.swing.JComboBox Returns the action command that is included in the event sent to action listeners. getActionCommand() - Method in interface javax.swing.ButtonModel Returns the action command for this button. getActionCommand() - Method in class javax.swing.AbstractButton Returns the action command for this button.
JFrame里可能有多个控件的事件,为了让事件监听器知道用户按下了哪个按扭或点击了那个菜单项,在public void actionPerformed(ActionEvent e)中参数e调用getActionCommand()来得到控件Command字符串以确定是哪个控件的动作,进而来执行回调。 例如: import javax.swing.*; import java.awt.*; import java.awt.event.*;public class test { public static void main(String[] args) { JFrame frame = new JFrame("test"); frame.setSize(new Dimension(400,300)); JButton btn = new JButton("test"); frame.getContentPane().setLayout(new FlowLayout()); frame.getContentPane().add(btn); btn.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ Runtime rt = Runtime.getRuntime(); String command = new String("javadoc -d d:\\doc d:\\test.java"); try{ rt.exec(command); }catch(Exception ex){ ex.printStackTrace(); } } }); frame.show(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } 在给一个RadioButton的例子,准备三张Gif图片pie.gif,cake.gif,ice.gif,然后测试下面的程序: import java.awt.*; import java.awt.event.*; import javax.swing.*;public class RadioDemo { static JFrame jframe = new JFrame("Example"); public static void setupFrame() { jframe.setSize(400,130); jframe.getContentPane().setLayout( new FlowLayout() ); WindowListener l = new WindowAdapter() { public void windowClosing(WindowEvent e) {System.exit(0);} }; jframe.addWindowListener(l); } static String pieString = "pie"; static String cakeString = "cake"; static String iceString = "ice"; public static void main(String[] args) { setupFrame(); final JLabel piclabel = new JLabel(new ImageIcon( pieString + ".gif")); /** Listens to the radio buttons. */ class RadioListener implements ActionListener { public void actionPerformed(ActionEvent e) { piclabel.setIcon(new ImageIcon( e.getActionCommand() + ".gif")); } } JRadioButton pieButton = new JRadioButton(pieString); pieButton.setMnemonic('b'); pieButton.setActionCommand(pieString); pieButton.setSelected(true); JRadioButton cakeButton = new JRadioButton(cakeString); JRadioButton iceButton = new JRadioButton(iceString); // Group the radio buttons. ButtonGroup group = new ButtonGroup(); group.add(pieButton); group.add(cakeButton); group.add(iceButton); // Register a listener for the radio buttons. RadioListener myListener = new RadioListener(); pieButton.addActionListener(myListener); cakeButton.addActionListener(myListener); iceButton.addActionListener(myListener); // Put the radio buttons in a column in a panel JPanel radioPanel = new JPanel(); radioPanel.setLayout(new GridLayout(0, 1)); radioPanel.add(pieButton); radioPanel.add(cakeButton); radioPanel.add(iceButton); // setLayout(new BorderLayout()); jframe.getContentPane().add(radioPanel); jframe.getContentPane().add(piclabel); jframe.setVisible(true); } }
不好意思上面的第一个例子用错了,应该是: 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\[email protected]"); 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); } }
Returns the command name of the action event fired by this button. getActionCommand() - Method in class java.awt.MenuItem
Gets the command name of the action event that is fired by this menu item. getActionCommand() - Method in class java.awt.event.ActionEvent
Returns the command string associated with this action. getActionCommand() - Method in class javax.swing.DefaultButtonModel
Returns the action command for this button. getActionCommand() - Method in class javax.swing.JComboBox
Returns the action command that is included in the event sent to action listeners. getActionCommand() - Method in interface javax.swing.ButtonModel
Returns the action command for this button. getActionCommand() - Method in class javax.swing.AbstractButton
Returns the action command for this button.
例如:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;public class test
{
public static void main(String[] args) {
JFrame frame = new JFrame("test");
frame.setSize(new Dimension(400,300));
JButton btn = new JButton("test");
frame.getContentPane().setLayout(new FlowLayout());
frame.getContentPane().add(btn);
btn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
Runtime rt = Runtime.getRuntime();
String command =
new String("javadoc -d d:\\doc d:\\test.java");
try{
rt.exec(command);
}catch(Exception ex){
ex.printStackTrace();
}
}
});
frame.show();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
在给一个RadioButton的例子,准备三张Gif图片pie.gif,cake.gif,ice.gif,然后测试下面的程序:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class RadioDemo { static JFrame jframe = new JFrame("Example"); public static void setupFrame() {
jframe.setSize(400,130);
jframe.getContentPane().setLayout( new FlowLayout() ); WindowListener l = new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}
};
jframe.addWindowListener(l);
} static String pieString = "pie";
static String cakeString = "cake";
static String iceString = "ice"; public static void main(String[] args) {
setupFrame();
final JLabel piclabel = new JLabel(new ImageIcon( pieString + ".gif")); /** Listens to the radio buttons. */
class RadioListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
piclabel.setIcon(new ImageIcon( e.getActionCommand()
+ ".gif"));
}
}
JRadioButton pieButton = new JRadioButton(pieString);
pieButton.setMnemonic('b');
pieButton.setActionCommand(pieString);
pieButton.setSelected(true); JRadioButton cakeButton = new JRadioButton(cakeString);
JRadioButton iceButton = new JRadioButton(iceString); // Group the radio buttons.
ButtonGroup group = new ButtonGroup();
group.add(pieButton);
group.add(cakeButton);
group.add(iceButton); // Register a listener for the radio buttons.
RadioListener myListener = new RadioListener();
pieButton.addActionListener(myListener);
cakeButton.addActionListener(myListener);
iceButton.addActionListener(myListener);
// Put the radio buttons in a column in a panel
JPanel radioPanel = new JPanel();
radioPanel.setLayout(new GridLayout(0, 1));
radioPanel.add(pieButton);
radioPanel.add(cakeButton);
radioPanel.add(iceButton); // setLayout(new BorderLayout()); jframe.getContentPane().add(radioPanel);
jframe.getContentPane().add(piclabel);
jframe.setVisible(true); }
}
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\[email protected]");
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);
}
}