统计
  • 建站日期:2021-03-10
  • 文章总数:518 篇
  • 评论总数:151 条
  • 分类总数:32 个
  • 最后更新:4月20日
文章 未分类

java getSource()和 getActionCommand()

梦幻书涯
首页 未分类 正文

比如说 按纽的事件,同一个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

  1. package com.liuyanzhao;
  2. import java.awt.Button;
  3. import java.awt.Color;
  4. import java.awt.Frame;
  5. import java.awt.Label;
  6. import java.awt.Panel;
  7. import java.awt.event.ActionEvent;
  8. import java.awt.event.ActionListener;
  9. import javax.swing.JButton;
  10. import javax.swing.JFrame;
  11. import javax.swing.JLabel;
  12. import javax.swing.JPanel;
  13. import javax.swing.WindowConstants;
  14. public class Demo2 implements ActionListener {
  15.     JButton button_open ;
  16.     JButton button_close;
  17.     Label label;//这个地方不要用JLable,否则空白符不占位
  18.     Label label2;
  19.     public static void main(String[] args) {
  20.         Demo2 d = new Demo2();
  21.         d.go();
  22.     }
  23.     public void go() {
  24.         JFrame frame = new JFrame();
  25.         frame.setSize(300100);
  26.         frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  27.         frame.setVisible(true);
  28.         JPanel panel = new JPanel();
  29.         frame.add(panel);
  30.         label = new Label("灯状态:");
  31.         label2 = new Label("  ");
  32.         button_open = new JButton("开灯");
  33.         button_close = new JButton("关灯");
  34.         button_open.addActionListener(this);
  35.         button_close.addActionListener(this);
  36.         panel.add(label);
  37.         panel.add(label2);
  38.         panel.add(button_open);
  39.         panel.add(button_close);
  40.     }
  41.     @Override
  42.     public void actionPerformed(ActionEvent e) {
  43.         //方法一:getActionCommand
  44. //      if(e.getActionCommand()=="开灯") {
  45. //          label2.setBackground(Color.red);
  46. //          button_open.setEnabled(false);
  47. //          button_close.setEnabled(true);
  48. //      } else if(e.getActionCommand()=="关灯") {
  49. //          label2.setBackground(Color.black);
  50. //          button_close.setEnabled(false);
  51. //          button_open.setEnabled(true);
  52. //      }
  53.         //方法二:getSource
  54.         if(e.getSource()==button_open) {//button_open不要加引号
  55.             label2.setBackground(Color.red);
  56.             button_open.setEnabled(false);
  57.             button_close.setEnabled(true);
  58.         } else if(e.getSource()==button_close) {//button_closen不要加引号
  59.             label2.setBackground(Color.black);
  60.             button_close.setEnabled(false);
  61.             button_open.setEnabled(true);
  62.         }
  63.     }
  64. }

这里要小心,请看注释

版权说明
文章采用: 《署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)》许可协议授权。
版权声明:未标注转载均为本站原创,转载时请以链接形式注明文章出处。如有侵权、不妥之处,请联系站长删除。敬请谅解!

这篇文章最后更新于2019-6-8,已超过 1 年没有更新,如果文章内容或图片资源失效,请留言反馈,我们会及时处理,谢谢!
JFrame的层次结构究竟是什么样的,什么又是ContentPane()
« 上一篇
java swing实现一个简单的计算器
下一篇 »

发表评论

HI ! 请登录
注册会员,享受下载全站资源特权。
Array

日历

热门文章