zoukankan      html  css  js  c++  java
  • 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();
    	}
    }
    

      

  • 相关阅读:
    [转]windows下mysql配置文件my.ini的位置
    [转]Oracle 11g不能导出空表的多种解决方法
    [转]ORACLE WITH AS 用法(相当于查询开始前构造临时表,提高效率)
    [转]基于WordPress的微信小程序支付功能开发
    从数据库优化到治病(2)治好心悸过程
    算命三十多年的资深命理师的人生感悟!
    从高维度看世界
    鸾书精华
    实用QPS和TPS高的高效分析方法
    windows 安装 mysql
  • 原文地址:https://www.cnblogs.com/liao13160678112/p/6476883.html
Copyright © 2011-2022 走看看