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()获取的是按钮对象!

优化这段代码import javax.swing.*; import java.awt.*; import java.awt.event.*; public class TemperatureConverter implements ActionListener { private JFrame frame; private JPanel panel; private JLabel Label1,Label2; private JTextField Field1,Field2; private JButton Button1,Button2,Button3; public TemperatureConverter() { frame = new JFrame("温度转换"); f.setSize ( 400,200 ) ; f.setVisible ( true ) ; f.setLocationRelativeTo ( null ) ; panel = new JPanel(); Label1 = new JLabel("摄氏度:"); Label2 = new JLabel("华氏度:"); Field1 = new JTextField(10); Field2 = new JTextField(10); Button1 = new Button("摄氏度 → 华氏度"); Button2 = new Button("华氏度 → 摄氏度"); Button3 = new Button("清空"); panel.add(Label1); panel.add(Label2); panel.add(Field1); panel.add(Field2); panel.add(Button1); panel.add(Button2); panel.add(Button3); Button1.addActionListener(this); Button2.addActionListener(this); Button3.addActionListener(this); public void actionPerformed(ActionEvent e) { if (e.getSource() == Button1) { double value = Double.parseDouble(Field1.getText()); double result = (value * 9 / 5) + 32; Field1.setText(String.format("%.2f", result)); } if (e.getSource() == Button2) { double value = Double.parseDouble(Field2.getText()); double result = (value - 32) * 5 / 9; Field1.setText(String.format("%.2f", result)); } if(e.getActionCommand().equals("清空") ) { Field1.setText(null); Field2.setText(null); } } } } public static void main(String[] args) { new TemperatureConverter(); }
最新发布
05-24
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值