通过一个实例来理解按钮动作、窗口关闭事件的响应和处理,两者的委托处理模型描述如图:
运行后的结果是下面这样的:
功能:
1.在字符栏输入字符后,点击“查询Unicode码” 会获取首字符的Unicode编码并在第二行的文本框内显示出来
2.在Unicode编码栏输入相关的编码后,点击“查询字符”会在第一行显示对应的Unicode编码字符
下面是代码,思路解释在代码里
import java.awt.*;
import java.awt.event.*;
public class MyQueryFrame extends Frame implements ActionListener{
//先声明四个组件,插入组件和动作处理中都会用上的
private TextField text_char,text_uni;
private Button button_char,button_uni;
public MyQueryFrame(){
super("我的字符码查询器");
this.setBounds(300,240,400,90);//设置框架的位置和尺寸(前面两个参数是位置)
this.setBackground(Color.lightGray);//设置背景颜色
this.setLayout(new GridLayout(2,3));//设置框架为2行3列的网格布局
//框架搭好了,开始搭建按钮了
//大概想象一下,字符码查询转换就两个文本框、两个按钮加上每个文本框前面的文字
this.add(new Label("字符",Label.RIGHT));//添加文本框前面的标签
this.text_char=new TextField("汉字",10);//插入一个文本框,大小为10,默认显示“汉字”
this.add(this.text_char); //将文本框添加到框架里面去
this.button_char=new Button("点击查询Unicode查询码");//按钮组件
this.add(this.button_char);
//现在已经有了一个按钮了,想要点击按钮之后能够达结果,就要先添加注册事件监听器,然后委托this对象处理事件,待会会解释这句话
this.button_char.addActionListener(this);
//接下来添加下一行类似的组件
this.add(new Label("Unicode编码",Label.RIGHT));
this.add(this.text_uni=new TextField(10));
this.add(this.button_uni=new Button("点击查询字符"));
this.button_uni.addActionListener(this);
this.setVisible(true);
this.addWindowListener(new Winclose());//点击叉叉后退出关闭程序,new Winclose()是一个实例对象,将在下面实现,目的是处理委托的窗口关闭事件
}
@Override
public void actionPerformed(ActionEvent event) {
// TODO Auto-generated method stub
if(event.getSource()==this.button_char){//按的是第一个按钮
String str =this.text_char.getText();
if(!str.isEmpty()){//输入非空,则往下运行
char ch =str.charAt(0);//获取第一个字符
this.text_char.setText(""+ch);
this.text_uni.setText(""+(int)ch);//把这个字符的Unicode传给第二个框
}
}else if(event.getSource()==this.button_uni){//按的是第二个按钮
String str =this.text_uni.getText();
if(!str.isEmpty()){
this.text_char.setText(""+(char)Integer.parseInt(str));//将第二个文本框输入的Unicode编码转换为字符,传到第一个文本框内
}
}
}
public class Winclose implements WindowListener{
//自己实现一个关闭窗口的事件处理器,由于要实现WindowListener接口,所以一些没有用的接口也必须实现
@Override
public void windowClosing(WindowEvent arg0) {
// TODO Auto-generated method stub
System.out.print("点击了关闭窗口,将此事件委托给了一个自己创建的Winclose处理,程序已退出");
System.exit(0);
}
public void windowActivated(WindowEvent arg0) {}
public void windowClosed(WindowEvent arg0) {}
public void windowDeactivated(WindowEvent arg0) {}
public void windowDeiconified(WindowEvent arg0) {}
public void windowIconified(WindowEvent arg0) {}
public void windowOpened(WindowEvent arg0) {}
}
public static void main(String[] args){
new MyQueryFrame();
}
}
窗口的关闭还能利用内部类或者匿名内部类解决,emmmm,借鉴一下别人的思路,链接如下:Java入门之窗口关闭,巨巨巨详细!!!_java关闭窗口的语句-优快云博客
代码中有许多注册监听器的操作:
this.button_char.addActionListener(this);
this.button_uni.addActionListener(this);
this.addWindowListener(new Winclose());
前两个按钮注册的都是addActionListener(),所以,当委托this对象处理动作时,则是当前类中的实例,即:this是QueryFrame类的一个实例对象,而QueryFrame的实例实现的是ActionListener这个接口,此接口就一个抽象方法:
public void actionPerformed(ActionEvent event) {}
所以,在这个类里面,处理组件用addActionListener()传来的事件,这样思路就清晰多了
1.先排版好按扭、文本框、标签等
2.给需要触发的组件注册监听器,格式是“组件.addActionListener()”
3.实现ActionListener接口中的“动作实现方法”
public void actionPerformed(ActionEvent event) {}
而第三个注册监听器的是用的addWindowListener(),和前两个类似,也是需要对应的窗口监听事件接口的实现类的实例对象来完成的,我上面的代码用的是外部的Winclose类的实例对象完成的(Winclose类实现了WindowListener)
附上课本上的解释图片: