170227网络编程-事件处理
//本节课学得 使用内部类 外部类 匿名类添加侦听器的方法。
package event; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class MyFrame extends JFrame { /** * */ private static final long serialVersionUID = 1L; JButton jbtn; JLabel lbl; public MyFrame(){ this.setLocation(100,100); this.setSize(500,500); this.setDefaultCloseOperation(DISPOSE_ON_CLOSE); jbtn=new JButton("测试"); lbl=new JLabel(); //(1)给按钮注册一个侦听器对象 jbtn.addActionListener(new FirstButtonListen()); //(2)给按钮注册一个侦听器对象 /*jbtn.addActionListener(new SecondButtonListen());*/ //(3)利用匿名类实现给按钮添加一个侦听器 /*jbtn.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub System.out.println("hello zhbit"); } });*/ //(2)利用内部类实现给按钮添加一个侦听器 //this.addWindowListener(new MyWindowListener()); //(3)使用内部类的方法添加WindowListener监听器 this.addWindowListener(new WindowListener(){ @Override public void windowActivated(WindowEvent arg0) { // TODO Auto-generated method stub } @Override public void windowClosed(WindowEvent arg0) { // TODO Auto-generated method stub System.out.println("windows is closed!"); } @Override public void windowClosing(WindowEvent arg0) { // TODO Auto-generated method stub System.out.println("windows is closing!"); } @Override public void windowDeactivated(WindowEvent arg0) { // TODO Auto-generated method stub } @Override public void windowDeiconified(WindowEvent arg0) { // TODO Auto-generated method stub } @Override public void windowIconified(WindowEvent arg0) { // TODO Auto-generated method stub } @Override public void windowOpened(WindowEvent arg0) { // TODO Auto-generated method stub lbl.setText("hello 我待会会记录坐标"); } }); this.addMouseMotionListener(new MouseMotionListener(){ @Override public void mouseDragged(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseMoved(MouseEvent e) { // TODO Auto-generated method stub int x=e.getX(); int y=e.getY(); lbl.setText("我的位置是x="+x+","+"y="+y+"。"); } }); this.add(jbtn); this.add(lbl); this.setLayout(new FlowLayout()); this.setVisible(true); } public static void main(String[] args){ new MyFrame(); } //(2)利用内部类实现给按钮添加一个侦听器 /*class SecondButtonListen implements ActionListener{ @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub System.out.println("hello the second way"); } }*/ //(2)利用内部类实现WindowListener监听 /*class MyWindowListener extends WindowAdapter{ @Override public void windowClosed(WindowEvent arg0) { // TODO Auto-generated method stub System.out.println("windows is closed!!!!"); super.windowClosed(arg0); } }*/ } //(1)创建一个Action类型的侦听器类 外部类 class FirstButtonListen implements ActionListener{ @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub System.out.println("hello the first way"); } }
课后习题
习题:利用图形界面编写一个简单的文本编辑器,包含一个文本域和一个保存按钮。单击保存按钮将文本域(TextArea)中的内容保存到一个文本文件mytext.txt中。
提示:
使用TextArea的getText()方法可以获得文本域中的内容。
使用内部类定义事件监听器,实现事件监听器接口ActionListener。
可使用fileWriter的Write(String str, int off, int len)方法将内容写入文件。
package event;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
public class test1 extends JFrame {
JButton jb;
JTextArea jta;
public test1(){
this.setLocation(200,200);
this.setSize(500,500);
jta=new JTextArea(5,20);
jta.setLineWrap(true);
jb=new JButton("保存");
jb.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
try {
getJta();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
this.addWindowListener(new WindowListener(){
@Override
public void windowActivated(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowClosed(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowDeactivated(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowDeiconified(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowIconified(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowOpened(WindowEvent e) {
// TODO Auto-generated method stub
}
});
this.add(jta);
this.add(jb);
this.setLayout(new FlowLayout());
this.setVisible(true);
}
void getJta() throws IOException{
String t;
t=jta.getText();
System.out.println(t);
write(t,0,t.length());
}
public void write(String str,int off,int len) throws IOException{
String path="D://mytext.txt";
FileOutputStream fs=new FileOutputStream(path);
OutputStreamWriter opsw=new OutputStreamWriter(fs);
opsw.write(str);
opsw.close();
fs.close();
}
public static void main(String[]args){
new test1();
}
}

浙公网安备 33010602011771号