`
哼哼哈哈云卷云舒
  • 浏览: 2657 次
最近访客 更多访客>>
社区版块
存档分类
最新评论

e.getSource()和e.getActionCommand()的区别

阅读更多

<div class="iteye-blog-content-contain" style="font-size: 14px"></div>

e.getSource()方法依赖于事件对象。比如:JButton jb= new JButton("开始");中的事件对象就是jb。

e.getActionCommand()方法得到的是按钮上的字符串。比如:JButton jb = new JButton("开始");中返回的是字符串"开始"。


总结:用e.getSource()得到的是添加监听器的组件对象,而用e.getActionCommand()得到的是组件对象上的字符串。

 

/**
 * 该代码是为了验证getSource()和getActionCommand()的区别
 * 从程序的运行结果可以看出getSource()获取的是组件对象
 * getActionCommand()获取的是组件上的字符串
 * 具体分析见博客链接:http://1710127116.iteye.com/admin/blogs/2161725
 * 
 */
package 获取按钮对象;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;

public class AtWho extends JFrame {
public void initUi(){
	this.setSize(400, 300);
	this.setLocationRelativeTo(null);
	this.setLayout(new FlowLayout());
	JButton jb=new JButton("ok");
	this.add(jb);
	jb.addActionListener(new ActionListener(){

		@Override
		public void actionPerformed(ActionEvent e) {
			System.out.println("e.getActionCommand():"+e.getActionCommand());
			System.out.println("e.getSource():"+e.getSource());
		}});
	
	this.setDefaultCloseOperation(3);//参数的作用:0:点击关闭按钮对话框不会被关闭;
									//1:点击 关闭按钮对话框被立即关闭,但程序始终在开启状态;
									//2:点击关闭按钮后对话框立即被关闭,但要几秒钟后程序才能结束;
									//3:点击关闭按钮后对话框和程序都立即被关闭;其他数字将会报参数不合法的错误。
	this.setVisible(true);
}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		AtWho aw=new AtWho();
		aw.initUi();
	}

}

 

 程序运行结果:

e.getActionCommand():ok
e.getSource():javax.swing.JButton[,168,5,48x28,alignmentX=0.0,alignmentY=0.5,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@f8f7db,flags=296,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,margin=javax.swing.plaf.InsetsUIResource[top=2,left=14,bottom=2,right=14],paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=true,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=ok,defaultCapable=true]

显然,e.getSource()获取的是按钮对象!

分享到:
评论

相关推荐

    编译器用Java编写程序

    else if(e2.getSource()==b15)//单击乘号按钮获得x的值和z的值并清空y的值 { x=Double.parseDouble(tf1.getText().trim()); str.setLength(0); y=0d; z=2; } else if(e2.getSource()==b10)//单击除号按钮获得...

    小型简单的记事本程序

    import java.awt.*; import java.awt.event.*; import java.io.*; import java.util.*; import javax.swing.*; ...public static void main(String[] args){ ... if(p.getActionCommand()=="新建 "|...

    JAVA编程计算器和记事本.rar

    menuItem =new JMenuItem("退出",KeyEvent.VK_E); menuItem.addActionListener(al); menu.add(menuItem); //创建Edit菜单 menu =new JMenu("编辑"); menu.setMnemonic(KeyEvent.VK_E); menubar.add(menu); ...

    JAVA编写的计算器

    tf.setText(tf.getText()+e.getActionCommand()); } } else { tf.setText(e.getActionCommand()); enable=true; } } if(e.getActionCommand()=="="){ switch(code){ case 0: y=x+Float....

    java编的简单计算器

    if("0,1,2,3,4,5,6,7,8,9".indexOf(e.getActionCommand())!=-1){ if(j==0||g==1){//判断是否第一次输入或者运算符后输入; if("0".equals(e.getActionCommand())) jt.setText("0"); else {jt...

    图书馆图书管理系统 oracle数据库

    else if(e.getActionCommand()=="书籍还入信息修改") { ReturnInfo ReturnInfoFrame=new ReturnInfo(); Dimension FrameSize=ReturnInfoFrame.getPreferredSize(); Dimension MainFrameSize=getSize();...

    一个带有括号的计算器

    } if(e.getSource()==jbutton[2]) { text.setText(&quot;-&quot;+text.getText()); } if(e.getActionCommand().equals(&quot;)&quot;)) { a=text.getText(); oper.EvaluateExpression(a); oper.EvaluateExpression...

    Java聊天室源代码

    import java.awt.*; import java.awt.event.*; import java.net.*; public class Chat { Frame f = new Frame("聊天室"); TextField tfIP = new ... ((TextField) e.getSource()).setText(""); } }); } }

    获得当前时间并且停止或开始时间

    String str=e.getActionCommand(); if(str.equals("stop")){ th.stop(); } } }); startButton.addActionListener(new ActionListener(){ ...

    Java编写的简易计算器

    }else if (e.getSource() == jb_ce || e.getSource() == jb_c) { tf_out.setText("0"); ch = '#'; return; } // 数字键 if (act == "0") { if (!tf_out.getText().equals("0")) { tf_out.setText(tf_out....

    java项目之餐饮管理系统

    String name = e.getActionCommand(); if (name.equals("确定")) { // 检验输入是否正确 if (dialog.checkInputValue()) { LoginInfoDao dao = null; LoginInfoVo vo = null; try { // 获得界面输入信息 ...

    摇号机代码

    } } } public void actionPerformed(ActionEvent e){ String s = e.getActionCommand(); if(s.equals("start")){ isGo = true; b1.setEnabled(false); b2.setEnabled(true); }else{ isGo = false; b2.setEnabled...

    基于Java实现的图像识别

    if(BROWSE_CMD.equals(e.getActionCommand())) { JFileChooser chooser = new JFileChooser(); chooser.showOpenDialog(null); File f = chooser.getSelectedFile(); BufferedImage bImage = null; ...

    计算器_java

    ((Frame)e.getSource()).dispose(); } } ); TextArea title = new TextArea(" 软件125实训项目 一 制作:第二组 常志铭 朱靖 2013.5.10 ",10,8,TextArea.SCROLLBARS_NONE); title....

    计算器(JAVA)awt包

    自己写的一个JAVA计算器程序,有点儿简单,主要的思想就是:在进行某种运算前先进行前一种运算。 class Operator implements ActionListener{ ... operator=e.getActionCommand().toString(); nn=true; } }

    java课程设计

    String s=e.getActionCommand(); if(s.equals("静音")){ sound.sthread.flag=false; } else if(s.equals("有声")){ sound.sthread.flag=true; } else if(('0'&lt;=s.charAt(0)&&s.charAt(0))||s.charAt(0)=='.'...

    java事件监听器

    getSource( ) 获取产生事件组件的组件名 getActionCommand( ) 获取与组件有关的文本,如命令按钮的标签 键盘事件(KeyEvent) 发生在键盘上的某个键被按下时。 类为了能够处理这些事件必须实现接口 ...

    java认证所有课程

    7.+ e.getActionCommand()); 8.} 9.} 这两个范例的特征如下: - Button类有一个addActionListner(ActionListener)方法。 - AddActionListner接口定义了一个方法actionPerformed,用来接收一个ActionEvent。 - ...

    合并排序算法的演示

    if(e.getActionCommand().equals("生成数")) //如果响应的事件是生成数 { int num; for(int i=0;i &lt; this.y.length;i++) //随机生成按钮上的数 { num = (int)(Math.random()*70); //得到随机数 this.button...

    java弹窗美化Demo

    String cmd = ae.getActionCommand(); if (CMD_1.equals(cmd)) { // 设置按钮显示效果 UIManager.put("OptionPane.buttonFont", new FontUIResource(new Font("宋体", Font.ITALIC, 13))...

Global site tag (gtag.js) - Google Analytics