java getSource()和 getActionCommand()区别
2017-05-25 19:01
344 查看
比如说 按纽的事件,同一个JFrame里可能有多个按钮的事件,为了避免冲突,给每个按钮设置不同的
ActionCommand,在监听时间的时候,用这个做条件区分事件,以做不同的响应
追问
他与getSource有什么区别
回答
getSource()
Returns:
The object on which the Event initially occurred.
依赖于事件对象
getActionCommand()
Returns the command name of the action event fired by this button. If the command name is null (default) then this method returns the label of the button.
依赖于按钮上的字符串
getSource得到的组件的名称,而getActionCommand得到的是标签。
如:Button bt=new Button(“buttons”);
用getSource得到的是bt
而用getActionCommand得到的是:buttons
e.getSource() 返回的当前动作所指向的对象,包含对象的所有信息
e.getActionCommand() 返回的是当前动作指向对象的名称
这里附一个开灯的demo
这里要小心,请看注释
本文链接:http://liuyanzhao.com/4051.html
ActionCommand,在监听时间的时候,用这个做条件区分事件,以做不同的响应
追问
他与getSource有什么区别
回答
getSource()
Returns:
The object on which the Event initially occurred.
依赖于事件对象
getActionCommand()
Returns the command name of the action event fired by this button. If the command name is null (default) then this method returns the label of the button.
依赖于按钮上的字符串
getSource得到的组件的名称,而getActionCommand得到的是标签。
如:Button bt=new Button(“buttons”);
用getSource得到的是bt
而用getActionCommand得到的是:buttons
e.getSource() 返回的当前动作所指向的对象,包含对象的所有信息
e.getActionCommand() 返回的是当前动作指向对象的名称
这里附一个开灯的demo
package com.liuyanzhao; import java.awt.Button; import java.awt.Color; import java.awt.Frame; import java.awt.Label; import java.awt.Panel; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.WindowConstants; public class Demo2 implements ActionListener { JButton button_open ; JButton button_close; Label label;//这个地方不要用JLable,否则空白符不占位 Label label2; public static void main(String[] args) { Demo2 d = new Demo2(); d.go(); } public void go() { JFrame frame = new JFrame(); frame.setSize(300, 100); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); frame.setVisible(true); JPanel panel = new JPanel(); frame.add(panel); label = new Label("灯状态:"); label2 = new Label(" "); button_open = new JButton("开灯"); button_close = new JButton("关灯"); button_open.addActionListener(this); button_close.addActionListener(this); panel.add(label); panel.add(label2); panel.add(button_open); panel.add(button_close); } @Override public void actionPerformed(ActionEvent e) { //方法一:getActionCommand // if(e.getActionCommand()=="开灯") { // label2.setBackground(Color.red); // button_open.setEnabled(false); // button_close.setEnabled(true); // } else if(e.getActionCommand()=="关灯") { // label2.setBackground(Color.black); // button_close.setEnabled(false); // button_open.setEnabled(true); // } //方法二:getSource if(e.getSource()==button_open) {//button_open不要加引号 label2.setBackground(Color.red); button_open.setEnabled(false); button_close.setEnabled(true); } else if(e.getSource()==button_close) {//button_closen不要加引号 label2.setBackground(Color.black); button_close.setEnabled(false); button_open.setEnabled(true); } } }
这里要小心,请看注释
本文链接:http://liuyanzhao.com/4051.html
相关文章推荐
- JAVA事件处理中e.getSource()和e.getActionCommand()方法的区别
- getSource与getActionCommand的区别
- java getSource()和 getActionCommand()
- java getSource()和 getActionCommand()
- getSourse() 与getActionCommand 区别
- Java反射中的getFields()和getDeclaredFields()的区别。
- java-----instanceof与getClass的区别
- getSource() && getActionCommand
- Java日志管理:Logger.getLogger()和LogFactory.getLog()的区别(详解Log4j)
- java getCanonicalName getSimpleName和getName的区别和用法
- java_class.getMethod 与 class.getDeclaredMethod的区别
- android MotionEvent getAction,getActionMasked,getActionIndex区别
- Rhyme/Java getClass()与instanceof的区别以及如何编写一个完美的equals()
- JAVA-getComponent()与getSource()
- java.io.File 中 getPath(), getAbsolutePath() 和 getCanonicalPath() 三个方法的区别
- java 私有属性加get/set方法跟直接把属性设置成public有区别吗?
- convert curl command into java HttpGet
- java .class与getClass的区别
- java中File类的getPath(),getAbsolutePath(),getCanonicalPath()区别
- Java中get,post乱码的区别