Java JButton 触发事件两种方式 addActionListene


import javax.swing.JFrame;
import javax.swing.JLabel;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class LoginForm extends JFrame implements ActionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = -3457588410024263691L;
	/**
	 * 
	 */

	JPanel jp1, jp2, jp3; // 布局容器
	JLabel lblLoginName, lblPassWord; // 标签
	private JButton cmdLogin, cmdClear; // 命令按扭
	// Button cmdLogin, cmdClear; // 命令按扭

	JTextField txtLoginName; // 登录名 文本框
	JPasswordField txtPassWord; // 密码 文本框
	int inputcount = 0; // 输入的次数

	public dqTable table;

	public LoginForm() {
		super("登录");
		// TODO 自动生成的构造函数存根

		jp1 = new JPanel();
		jp2 = new JPanel();
		jp3 = new JPanel();
		lblLoginName = new JLabel("用户名");// 标签
		lblPassWord = new JLabel("密    码");

		// cmdLogin = new JButton("登录");// 按钮
		// cmdClear = new JButton("取消");

		cmdLogin = new JButton("登录");// 按钮
		cmdClear = new JButton("取消");
		cmdClear.addActionListener(this);
		cmdLogin.addActionListener(this);
		cmdClear.addActionListener(new ClearListener());
		cmdLogin.addActionListener(new LoginListener());

		txtLoginName = new JTextField(12);// 文本框,里面给一个长度
		txtPassWord = new JPasswordField(12);// 密码框,给个长度

		jp1.add(lblLoginName);
		jp1.add(txtLoginName);

		jp2.add(lblPassWord);
		jp2.add(txtPassWord);

		jp3.add(cmdLogin);
		jp3.add(cmdClear);

		this.setLayout(new GridLayout(3, 1));// 网格布局三行一列
		this.add(jp1);
		this.add(jp2);

		this.add(jp3);

		this.setBounds(100, 100, 400, 150); // 位置大小
		this.setTitle("用户登录"); // 标签
		this.setResizable(false); // 不可改大小
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);

	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO 自动生成的方法存根

		LoginForm form = new LoginForm();

	}

	public void actionPerformed(ActionEvent event) {
		Object source = event.getSource();
		if (source == cmdClear) {
			System.out.println("cmdClear");
			System.exit(0);// 退出系统
		} else if (source == cmdLogin) {
			System.out.println("cmdLogin");
		}
	}

	public class LoginListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO 自动生成的方法存根
			System.out.println("1");

		}

	}

	public class ClearListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO 自动生成的方法存根
			System.out.println("2");

		}

	}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
package BookTest; import java.awt.*; import java.awt.event.*; import java.util.AbstractQueue; import java.util.Vector; import javax.swing.*; import javax.swing.table.*; import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.DefaultTreeCellRenderer; import javax.swing.tree.DefaultTreeModel; import javax.swing.tree.TreeSelectionModel; import javax.swing.event.*; import javax.swing.table.TableColumnModel; //import DbHelp.OperatorJobs; import Dao.ArrayBook; public class Test extends JFrame { public JPanel jp=null; public String aa; //JTable jTable1 = new JTable(); public JTable jTable1=null; private JTree jTree1; DefaultTableModel mode; private JScrollPane scrollPane = null; DefaultMutableTreeNode node; JButton[] btn={new JButton("增加"),new JButton("删除"),new JButton("修改"),new JButton("查询"),new JButton("重置")}; public JTextField[] txt ={new JTextField(),new JTextField(),new JTextField(),new JTextField(),new JTextField(),new JTextField()}; public String[] str1={"1","summer","boy","20","",""}; public String[] str2={"2","wulei","girl","19","",""}; public String[] str3={"3","west","boy","20","",""}; public String[] str4={"4","fish","girl","18","",""}; public String[] str5={"5","youku","boy","21","",""}; public String[] str6={"6","aa","boy","21","",""}; public String[][] tempData1={str1,str2,str3,str4,str5,str6}; public String[] aa1={"1","summer","boy","20","123","123"}; public String[] aa2={"2","wulei","girl","19","123",""}; public String[] aa3={"3","west","boy","20","123",""}; public String[] aa4={"4","fish","girl","18","1",""}; public String[] aa5={"5","youku","boy","21","12",""}; public String[] aa6={"6","aa","boy","21","123",""}; public String[][] tempData4={aa1,aa2,aa3,aa4,aa5,aa6}; //设置表头数据 public String[] tempTitle={"ID","标题","内容","作者","时间","是否选中"}; //设置表格的数据集合 Vector v1=new Vector(); Vecto

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值