社区
Java SE
帖子详情
getActionCommand()是什么东东?
zbc
2002-09-13 06:05:55
它是ActionEvent类里面定义的一个方法,但是我不知道是什么东东?
我看的书上找不到,只有问各位了,问题实在太小,我想也应该不会难回答。所以只给5分了,谢谢,^_*
...全文
479
6
打赏
收藏
getActionCommand()是什么东东?
它是ActionEvent类里面定义的一个方法,但是我不知道是什么东东? 我看的书上找不到,只有问各位了,问题实在太小,我想也应该不会难回答。所以只给5分了,谢谢,^_*
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
6 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
zbc
2002-09-13
打赏
举报
回复
楼上见位能如此的帮助java菜鸟,可见并非是为分而来的,!万分的感谢你们的帮助,我决定为此贴加到50分,以感谢各位对小弟的关怀!谢谢!
dytsoft
2002-09-13
打赏
举报
回复
up
babyfu
2002-09-13
打赏
举报
回复
就是得到点击某个菜单条的事件的名称,而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);
}
}
playgb
2002-09-13
打赏
举报
回复
调用setActionCommand()方法可以为按钮设置一个命令名,这个命令名在响应事件时可能用到;调用getActionCommand()方法可以获得这个命令名
希望看到这些文章你会有所收获.
playgb
2002-09-13
打赏
举报
回复
在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;
}
Java基础之文件压缩解压工具
以前为了了解哈夫曼树,而做的一个文件压缩的
东东
,,实现给文件压缩,与解压 话不多多说先上图,当然界面不是非常的亮,有关界面的问题,上面两篇博客已经写得很清楚了! 关键压缩技术是哈弗曼树的压缩,所谓...
Java-文件压缩解压工具--(仅供自己收藏)
以前为了了解哈夫曼树,而做的一个文件压缩的
东东
,,实现给文件压缩,与解压 话不多多说先上图,当然界面不是非常的亮,有关界面的问题,上面两篇博客已经写得很清楚了! 关键压缩技术是哈弗曼树的压缩,所谓...
Java基础之聊天器
这是刚开始学习java网络编程的时候做的一个
东东
,,局域网聊天器,可以在局域网内,和小伙伴聊天,共同画图(游戏原型)。先上图吧 一,首先讲解下基本知识,建立网络连接,你首先得有服务器和客户端, 1,...
java基础之文件搜索工具
这是本人以前学习java时做的一个文件搜索的
东东
,一方面对File进行一些了解,另一方面对递归一些了解。。。先上图 一,首先对文件进行一些介绍(可以跳过,直接看后面的代码)File f=new File("路径");...
java聊天--(仅供自己收藏)
这是刚开始学习java网络编程的时候做的一个
东东
,,局域网聊天器,可以在局域网内,和小伙伴聊天,共同画图(游戏原型)。先上图吧 一,首先讲解下基本知识,建立网络连接,你首先得有服务器和客户端, 1,...
Java SE
62,614
社区成员
307,327
社区内容
发帖
与我相关
我的任务
Java SE
Java 2 Standard Edition
复制链接
扫一扫
分享
社区描述
Java 2 Standard Edition
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章