当前位置: 技术问答>java相关
getActionCommand()是什么东东?
来源: 互联网 发布时间:2015-09-02
本文导语: 它是ActionEvent类里面定义的一个方法,但是我不知道是什么东东? 我看的书上找不到,只有问各位了,问题实在太小,我想也应该不会难回答。所以只给5分了,谢谢,^_* | 在Java中,设定窗...
它是ActionEvent类里面定义的一个方法,但是我不知道是什么东东?
我看的书上找不到,只有问各位了,问题实在太小,我想也应该不会难回答。所以只给5分了,谢谢,^_*
我看的书上找不到,只有问各位了,问题实在太小,我想也应该不会难回答。所以只给5分了,谢谢,^_*
|
在Java中,设定窗口标题可以简单的调用JFrame所提供的setTitle()方法,该方法接受一个字符串作为窗口的新标题。创建一个下拉式菜单则涉及到创建一个菜单条JMenuBar,创建一个菜单JMenu,以及创建一个菜单项JMenuItem等操作。在下面的程序片断中详细展示了创建一个下拉式菜单的整个过程,其中菜单项的消息处理部分addActionListener()和setActionCommand() 是使得菜单能够正常工作的关键。F_New.addActionListener(this)方法指定程序使用NotePad类所提供的消息处理方法actionPerformed()来处理这个动作,F_New.setActionCommand("F_New")方法则指定程序在用户选取该菜单项的时候向系统发送一个参数为"F_New" 的菜单动作消息,这个消息最后将由NotePad类中的actionPerformed()方法来处理。
// 创建下拉式菜单
private JMenuBar CreateMenu()
{
// 创建一个菜单条
JMenuBar EditorMenu = new JMenuBar();
// 创建一个File菜单
JMenu FileMenu = new JMenu("File");
// 创建一个菜单项
JMenuItem F_New = new JMenuItem("New");
// 添加菜单消息处理
F_New.addActionListener(this);
F_New.setActionCommand("F_New");
// 将该裁单项添加到菜单中
FileMenu.add(F_New);
// 将File菜单添加到菜单条
EditorMenu.add(FileMenu);
// 返回菜单条
return EditorMenu;
}
// 创建下拉式菜单
private JMenuBar CreateMenu()
{
// 创建一个菜单条
JMenuBar EditorMenu = new JMenuBar();
// 创建一个File菜单
JMenu FileMenu = new JMenu("File");
// 创建一个菜单项
JMenuItem F_New = new JMenuItem("New");
// 添加菜单消息处理
F_New.addActionListener(this);
F_New.setActionCommand("F_New");
// 将该裁单项添加到菜单中
FileMenu.add(F_New);
// 将File菜单添加到菜单条
EditorMenu.add(FileMenu);
// 返回菜单条
return EditorMenu;
}
|
就是得到点击某个菜单条的事件的名称,而setActionCommand为设置点击某个菜单条的事件的名称。例子:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MenuBarExample extends JPanel implements ItemListener{
static JFrame myFrame;
Font myFont = new Font("Dialog", Font.PLAIN, 16);
public MenuBarExample(){
setFont(myFont);
JTabbedPane tabs = new JTabbedPane(SwingConstants.BOTTOM);
Icon icon = new ImageIcon ("feet.gif");
JButton button = new JButton(icon);
JLabel label = new JLabel ("Hello World!");
tabs.addTab("Hello World",label);
tabs.addTab("Feet",icon,button);
setLayout(new BorderLayout());
add(tabs,"Center");
add(createMenu(),"North");
}
public JMenuBar createMenu(){
JMenuBar menuBar = new JMenuBar();
// File Menu - create this so we have at least two menu options
JMenu file = (JMenu) menuBar.add(new JMenu("File"));
file.setMnemonic('F');
JMenuItem mi; //Temporary place holder
//Add several items under 'File' these won't do anything.
mi = (JMenuItem) file.add(new JMenuItem("Open"));
mi.setMnemonic('O');
mi = (JMenuItem) file.add(new JMenuItem("Save"));
mi.setMnemonic('S');
mi = (JMenuItem) file.add(new JMenuItem("Save As..."));
mi.setMnemonic('A');
file.add(new JSeparator());
mi = (JMenuItem) file.add(new JMenuItem("Exit"));
// Look and Feel Menu
JMenu options = (JMenu) menuBar.add(new JMenu("L&F"));
options.setMnemonic('L');
// Look and Feel Radio control
ButtonGroup group = new ButtonGroup();
mi = options.add(new JCheckBoxMenuItem("Windows L&F"));
mi.setActionCommand("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
//If the current look and feel is windows, select this item.
mi.setSelected(UIManager.getLookAndFeel().getName().equals("Windows"));
//group.add(mi);
mi.addItemListener(this);
// mi.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_1, ActionEvent.ALT_MASK));
mi = options.add(new JRadioButtonMenuItem("Motif L&F"));
mi.setActionCommand("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
mi.setSelected(UIManager.getLookAndFeel().getName().equals("CDE/Motif"));
group.add(mi);
mi.addItemListener(this);
// mi.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_2, ActionEvent.ALT_MASK));
mi = options.add(new JRadioButtonMenuItem("Metal L&F"));
mi.setActionCommand("javax.swing.plaf.metal.MetalLookAndFeel");
mi.setSelected(UIManager.getLookAndFeel().getName().equals("Metal"));
// metalMenuItem.setSelected(true);
group.add(mi);
mi.addItemListener(this);
// metalMenuItem.setAccelerator(KeyStroke.getKeyStroke (KeyEvent.VK_3, ActionEvent.ALT_MASK));
return menuBar;
}
public void itemStateChanged(ItemEvent e) {
Component root = myFrame;
//Bump the cursor into a wait mode while we make this change
root.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
//Get the source of the event.
JRadioButtonMenuItem button = (JRadioButtonMenuItem) e.getSource();
try {
if(button.isSelected()) {
UIManager.setLookAndFeel(button.getActionCommand());
button.setEnabled(true);
SwingUtilities.updateComponentTreeUI(myFrame);
}
} catch (UnsupportedLookAndFeelException exc) {
// Error - unsupported L&F
button.setEnabled(false);
System.err.println("Unsupported LookAndFeel: " + button.getText());
}catch (Exception exc2){
System.err.println("Couldn't load Look and feel" + button.getText());
}
root.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
}
public static void main(String args[]){
myFrame = new JFrame("MenuBar Example");
MenuBarExample menuExample = new MenuBarExample();
myFrame.getContentPane().add("Center",menuExample);
myFrame.setSize(400,200);
myFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}
});
myFrame.setVisible(true);
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MenuBarExample extends JPanel implements ItemListener{
static JFrame myFrame;
Font myFont = new Font("Dialog", Font.PLAIN, 16);
public MenuBarExample(){
setFont(myFont);
JTabbedPane tabs = new JTabbedPane(SwingConstants.BOTTOM);
Icon icon = new ImageIcon ("feet.gif");
JButton button = new JButton(icon);
JLabel label = new JLabel ("Hello World!");
tabs.addTab("Hello World",label);
tabs.addTab("Feet",icon,button);
setLayout(new BorderLayout());
add(tabs,"Center");
add(createMenu(),"North");
}
public JMenuBar createMenu(){
JMenuBar menuBar = new JMenuBar();
// File Menu - create this so we have at least two menu options
JMenu file = (JMenu) menuBar.add(new JMenu("File"));
file.setMnemonic('F');
JMenuItem mi; //Temporary place holder
//Add several items under 'File' these won't do anything.
mi = (JMenuItem) file.add(new JMenuItem("Open"));
mi.setMnemonic('O');
mi = (JMenuItem) file.add(new JMenuItem("Save"));
mi.setMnemonic('S');
mi = (JMenuItem) file.add(new JMenuItem("Save As..."));
mi.setMnemonic('A');
file.add(new JSeparator());
mi = (JMenuItem) file.add(new JMenuItem("Exit"));
// Look and Feel Menu
JMenu options = (JMenu) menuBar.add(new JMenu("L&F"));
options.setMnemonic('L');
// Look and Feel Radio control
ButtonGroup group = new ButtonGroup();
mi = options.add(new JCheckBoxMenuItem("Windows L&F"));
mi.setActionCommand("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
//If the current look and feel is windows, select this item.
mi.setSelected(UIManager.getLookAndFeel().getName().equals("Windows"));
//group.add(mi);
mi.addItemListener(this);
// mi.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_1, ActionEvent.ALT_MASK));
mi = options.add(new JRadioButtonMenuItem("Motif L&F"));
mi.setActionCommand("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
mi.setSelected(UIManager.getLookAndFeel().getName().equals("CDE/Motif"));
group.add(mi);
mi.addItemListener(this);
// mi.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_2, ActionEvent.ALT_MASK));
mi = options.add(new JRadioButtonMenuItem("Metal L&F"));
mi.setActionCommand("javax.swing.plaf.metal.MetalLookAndFeel");
mi.setSelected(UIManager.getLookAndFeel().getName().equals("Metal"));
// metalMenuItem.setSelected(true);
group.add(mi);
mi.addItemListener(this);
// metalMenuItem.setAccelerator(KeyStroke.getKeyStroke (KeyEvent.VK_3, ActionEvent.ALT_MASK));
return menuBar;
}
public void itemStateChanged(ItemEvent e) {
Component root = myFrame;
//Bump the cursor into a wait mode while we make this change
root.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
//Get the source of the event.
JRadioButtonMenuItem button = (JRadioButtonMenuItem) e.getSource();
try {
if(button.isSelected()) {
UIManager.setLookAndFeel(button.getActionCommand());
button.setEnabled(true);
SwingUtilities.updateComponentTreeUI(myFrame);
}
} catch (UnsupportedLookAndFeelException exc) {
// Error - unsupported L&F
button.setEnabled(false);
System.err.println("Unsupported LookAndFeel: " + button.getText());
}catch (Exception exc2){
System.err.println("Couldn't load Look and feel" + button.getText());
}
root.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
}
public static void main(String args[]){
myFrame = new JFrame("MenuBar Example");
MenuBarExample menuExample = new MenuBarExample();
myFrame.getContentPane().add("Center",menuExample);
myFrame.setSize(400,200);
myFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}
});
myFrame.setVisible(true);
}
}
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。