窗体frame
this.setContentPane(pane);//将面板添加到窗体中
this.setSize(300,200);设置窗体大小
this.setLocationRelativeTo(null);//将窗体水平垂直中间
this.setVisible(true);窗体可视
禁止窗口最大化
this.setResizable(false);//禁止设置窗口大小 窗口不能最大化
监听
login.addActionListener(this);
添加监听 语句是添加监听,但是不懂this指向什么,查查了资料
add是添加监听,但还需要添加监听对象
这个代码有两个动作
this表示“我正在处理的对象”可以说就是当前对象
将this替换成当前类的名字(这句代码所在的类)不异常,此时我才理解了什么是当前类的对象
login.addActionListener(this);
login.addActionListener(new third());
this关键字的作用:
1、调用类中的属性和方法
2、调用类中的构造方法
3、调用当前类
监听的执行
public void actionPerformed(ActionEvent e){}
调用一个接口必须重定义里面的方法、
actionPerformed方法(ActionListener中的唯一方法)将接收一个ActionEvent类型的对象作为参数
ActionEvent常见方法如下:
(1)String getActionCommand():返回与此类动作相关的命令字符串,默认组件为title。
(2)int getModifiers():返回发生此动作时同时按下的键盘按钮
(3)long getWhen():返回发生此事件时的事件的long形式。
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String Command=e.getActionCommand();//获取按钮的字符串
if (Command.equals("取消")) {
admin.setText("");
pass.setText("");
}
getSource得到的组件的名称,而getActionCommand得到的是标签。
如:Button bt=new Button("buttons");
用getSource得到的是bt 而用getActionCommand得到的是:buttons
事件处理
Java将每一个鼠标或者键盘一个操作定义为“事件”
一个动作(事件源)产生一个事件(事件对象)发送到监视器那里
abc.addActionListener(监视器);
将事件源和事件处理器关联起来 abc(事件源) 监视器(事件处理程序)