zoukankan      html  css  js  c++  java
  • 吴裕雄--天生自然JAVA图形界面编程学习笔记:事件处理

    import java.awt.event.WindowAdapter ;
    import java.awt.event.ActionListener ;
    import java.awt.event.WindowEvent ;
    import java.awt.event.ActionEvent ;
    import java.awt.Color ;
    import java.awt.GridLayout ;
    import java.awt.Font ;
    import javax.swing.JFrame ;
    import javax.swing.JButton ;
    import javax.swing.JLabel ;
    import javax.swing.JTextField ;
    import javax.swing.JPanel ;
    
    class ActionHandle{
        private JFrame frame = new JFrame("Welcome To MLDN") ; 
        private JButton but = new JButton("显示");
        private JLabel lab = new JLabel() ;
        private JTextField text = new JTextField(10) ;
        private JPanel pan = new JPanel() ;
        public ActionHandle(){
            Font fnt = new Font("Serief",Font.ITALIC + Font.BOLD,28) ;
            lab.setFont(fnt) ;    // 设置标签的显示文字
            lab.setText("等待用户输入信息!") ;
            but.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e){
                    // if(e.getSource() instanceof JButton){}    // 判断是否是按钮
                    if(e.getSource()==but){
                        lab.setText(text.getText()) ;
                    }
                }
            }) ;
            frame.addWindowListener(new WindowAdapter(){
                public void windowClosing(WindowEvent e){
                    System.exit(1) ;
                }
            }) ;    // 加入事件
            frame.setLayout(new GridLayout(2,1)) ;
            pan.setLayout(new GridLayout(1,2)) ;
            pan.add(text);
            pan.add(but) ;
            frame.add(pan) ;
            frame.add(lab) ;
            frame.pack() ;
            frame.setBackground(Color.WHITE) ;
            frame.setLocation(300,200) ;
            frame.setVisible(true) ;
    
        }
    };
    
    public class MyActionEventDemo01{
        public static void main(String args[]){
             new ActionHandle() ;
            
        }
    };
    import java.awt.event.WindowAdapter ;
    import java.awt.event.ActionListener ;
    import java.awt.event.WindowEvent ;
    import java.awt.event.ActionEvent ;
    import java.awt.Color ;
    import java.awt.GridLayout ;
    import java.awt.Font ;
    import javax.swing.JFrame ;
    import javax.swing.JButton ;
    import javax.swing.JLabel ;
    import javax.swing.JTextField ;
    import javax.swing.JPanel ;
    
    class ActionHandle{
        private JFrame frame = new JFrame("Welcome To MLDN") ; 
        private JButton but = new JButton("显示");
        private JLabel lab = new JLabel() ;
        private JTextField text = new JTextField(10) ;
        private JPanel pan = new JPanel() ;
        public ActionHandle(){
            Font fnt = new Font("Serief",Font.ITALIC + Font.BOLD,28) ;
            lab.setFont(fnt) ;    // 设置标签的显示文字
            lab.setText("等待用户输入信息!") ;
            but.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e){
                    if(e.getSource() instanceof JButton){
                        lab.setText(text.getText()) ;
                    }    // 判断是否是按钮
                }
            }) ;
            frame.addWindowListener(new WindowAdapter(){
                public void windowClosing(WindowEvent e){
                    System.exit(1) ;
                }
            }) ;    // 加入事件
            frame.setLayout(new GridLayout(2,1)) ;
            pan.setLayout(new GridLayout(1,2)) ;
            pan.add(text);
            pan.add(but) ;
            frame.add(pan) ;
            frame.add(lab) ;
            frame.pack() ;
            frame.setBackground(Color.WHITE) ;
            frame.setLocation(300,200) ;
            frame.setVisible(true) ;
    
        }
    };
    
    public class MyActionEventDemo01{
        public static void main(String args[]){
             new ActionHandle() ;
            
        }
    };
    import java.awt.event.WindowAdapter ;
    import java.awt.event.ActionListener ;
    import java.awt.event.WindowEvent ;
    import java.awt.event.ActionEvent ;
    import java.awt.Color ;
    import java.awt.GridLayout ;
    import java.awt.Font ;
    import javax.swing.JFrame ;
    import javax.swing.JButton ;
    import javax.swing.JLabel ;
    import javax.swing.JTextField ;
    import javax.swing.JPasswordField ;
    import javax.swing.JPanel ;
    
    class LoginCheck{
        private String name ;
        private String password ;
        public LoginCheck(String name,String password){
            this.name = name ;
            this.password = password ;
        }
        public boolean validate(){
            if("lixinghua".equals(name)&&"mldn".equals(password)){
                return true ;
            }else{
                return false ;
            }
        }
    };
    
    class ActionHandle{
        private JFrame frame = new JFrame("Welcome To MLDN") ; 
        private JButton submit = new JButton("登陆");
        private JButton reset = new JButton("重置");
        private JLabel nameLab = new JLabel("用户名:") ;
        private JLabel passLab = new JLabel("密   码:") ;
        private JLabel infoLab = new JLabel("用户登陆系统") ;
        private JTextField nameText = new JTextField(10) ;
        private JPasswordField passText = new JPasswordField() ;
        private JPanel pan = new JPanel() ;
        public ActionHandle(){
            Font fnt = new Font("Serief",Font.ITALIC + Font.BOLD,12) ;
            infoLab.setFont(fnt) ;    // 设置标签的显示文字
            submit.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e){
                    if(e.getSource()==submit){
                        String tname = nameText.getText() ;
                        String tpass = new String(passText.getPassword()) ;
                        LoginCheck log = new LoginCheck(tname,tpass) ;
                        if(log.validate()){
                            infoLab.setText("登陆成功,欢迎光临!") ;
                        }else{
                            infoLab.setText("登陆失败,错误的用户名或密码!") ;
                        }
                    }
                }
            }) ;
            reset.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e){
                    if(e.getSource()==reset){
                        nameText.setText("") ;
                        passText.setText("") ;
                        infoLab.setText("用户登陆系统") ;
                    }
                }
            }) ;
            
            frame.addWindowListener(new WindowAdapter(){
                public void windowClosing(WindowEvent e){
                    System.exit(1) ;
                }
            }) ;    // 加入事件
            frame.setLayout(null) ;
            nameLab.setBounds(5,5,60,20) ;
            passLab.setBounds(5,30,60,20) ;
            infoLab.setBounds(5,65,220,30) ;
            nameText.setBounds(65,5,100,20) ;
            passText.setBounds(65,30,100,20) ;
            submit.setBounds(165,5,60,20) ;
            reset.setBounds(165,30,60,20) ;
            frame.add(nameLab) ;
            frame.add(passLab) ;
            frame.add(infoLab) ;
            frame.add(nameText) ;
            frame.add(passText) ;
            frame.add(submit) ;
            frame.add(reset) ;
            frame.setSize(280,130) ;
            frame.setBackground(Color.WHITE) ;
            frame.setLocation(300,200) ;
            frame.setVisible(true) ;
    
        }
    };
    
    public class MyActionEventDemo03{
        public static void main(String args[]){
             new ActionHandle() ;
            
        }
    };
    import java.awt.event.WindowListener ;
    import java.awt.event.WindowEvent ;
    import java.awt.Color ;
    import javax.swing.JFrame ;
    class MyWindowEventHandle implements WindowListener{
    	public void windowActivated(WindowEvent e){
    		System.out.println("windowActivated --> 窗口被选中") ;
    	}
    	public void windowClosed(WindowEvent e){
    		System.out.println("windowClosed --> 窗口被关闭") ;
    	}
    	public void windowClosing(WindowEvent e){
    		System.out.println("windowClosing --> 窗口关闭") ;
    		System.exit(1) ;
    	}
    	public void windowDeactivated(WindowEvent e){
    		System.out.println("windowDeactivated --> 取消窗口选中") ;
    	}
    	public void windowDeiconified(WindowEvent e){
    		System.out.println("windowDeiconified --> 窗口从最小化恢复") ;
    	}
    	public void windowIconified(WindowEvent e){
    		System.out.println("windowIconified --> 窗口最小化") ;
    	}
    	public void windowOpened(WindowEvent e){
    		System.out.println("windowOpened --> 窗口被打开") ;
    	}
    };
    
    public class MyEventWindowEventJFrame01{
    	public static void main(String args[]){
    		JFrame frame = new JFrame("Welcome To MLDN") ; 
    		frame.addWindowListener(new MyWindowEventHandle()) ;	// 加入事件
    		frame.setSize(300,150) ;
    		frame.setBackground(Color.WHITE) ;
    		frame.setLocation(300,200) ;
    		frame.setVisible(true) ;
    	}
    };
    

      

    import java.awt.event.WindowAdapter ;
    import java.awt.event.WindowEvent ;
    import java.awt.Color ;
    import javax.swing.JFrame ;
    class MyWindowEventHandle extends WindowAdapter{
        public void windowClosing(WindowEvent e){
            System.out.println("windowClosing --> 窗口关闭") ;
            System.exit(1) ;
        }
    };
    
    public class MyEventWindowEventJFrame02{
        public static void main(String args[]){
            JFrame frame = new JFrame("Welcome To MLDN") ; 
            frame.addWindowListener(new MyWindowEventHandle()) ;    // 加入事件
            frame.setSize(300,150) ;
            frame.setBackground(Color.WHITE) ;
            frame.setLocation(300,200) ;
            frame.setVisible(true) ;
        }
    };
    import java.awt.event.WindowAdapter ;
    import java.awt.event.WindowEvent ;
    import java.awt.Color ;
    import javax.swing.JFrame ;
    public class MyEventWindowEventJFrame03{
    	public static void main(String args[]){
    		JFrame frame = new JFrame("Welcome To MLDN") ; 
    		frame.addWindowListener(new WindowAdapter(){
    			public void windowClosing(WindowEvent e){
    				System.out.println("windowClosing --> 窗口关闭") ;
    				System.exit(1) ;
    			}
    		}) ;	// 加入事件
    		frame.setSize(300,150) ;
    		frame.setBackground(Color.WHITE) ;
    		frame.setLocation(300,200) ;
    		frame.setVisible(true) ;
    	}
    };
    

      

    import java.awt.event.WindowAdapter ;
    import java.awt.event.KeyListener ;
    import java.awt.event.WindowEvent ;
    import java.awt.event.KeyEvent ;
    import java.awt.Color ;
    import javax.swing.JFrame ;
    import java.awt.event.ActionListener ;
    import java.awt.event.WindowEvent ;
    import java.awt.event.ActionEvent ;
    import java.awt.Color ;
    import java.awt.GridLayout ;
    import java.awt.Font ;
    import javax.swing.JFrame ;
    import javax.swing.JButton ;
    import javax.swing.JLabel ;
    import javax.swing.JTextArea ;
    import javax.swing.JPasswordField ;
    import javax.swing.JScrollPane ;
    import javax.swing.JPanel ;
    
    class MyKeyHandle extends JFrame implements KeyListener{
    	private JTextArea text = new JTextArea() ;
    	public MyKeyHandle(){
    		super.setTitle("Welcome To MLDN") ;
    		JScrollPane scr = new JScrollPane(text) ;
    		scr.setBounds(5,5,300,200) ;
    		super.add(scr) ;
    		text.addKeyListener(this) ;
    		super.addWindowListener(new WindowAdapter(){
    			public void windowClosing(WindowEvent e){
    				System.exit(1) ;
    			}
    		}) ;	// 加入事件
    		super.setSize(310,210) ;
    		super.setVisible(true) ;
    	}
    	public void keyPressed(KeyEvent e){
    		text.append("键盘“" + KeyEvent.getKeyText(e.getKeyCode())+ "”键按下
    ") ;
    	}
    
    	public void keyReleased(KeyEvent e){
    		text.append("键盘“" + KeyEvent.getKeyText(e.getKeyCode())+ "”键松开
    ") ;
    	}
    
    	public void keyTyped(KeyEvent e){
    		text.append("输入的内容是:" + e.getKeyChar() + "
    ") ;
    	} 
    };
    
    
    public class MyKeyEventDemo01{
    	public static void main(String args[]){
    		new MyKeyHandle() ;
    	}
    };
    

      

    import java.awt.event.WindowAdapter ;
    import java.awt.event.KeyAdapter ;
    import java.awt.event.WindowEvent ;
    import java.awt.event.KeyEvent ;
    import java.awt.Color ;
    import javax.swing.JFrame ;
    import java.awt.event.ActionListener ;
    import java.awt.event.WindowEvent ;
    import java.awt.event.ActionEvent ;
    import java.awt.Color ;
    import java.awt.GridLayout ;
    import java.awt.Font ;
    import javax.swing.JFrame ;
    import javax.swing.JButton ;
    import javax.swing.JLabel ;
    import javax.swing.JTextArea ;
    import javax.swing.JPasswordField ;
    import javax.swing.JScrollPane ;
    import javax.swing.JPanel ;
    
    class MyKeyHandle extends JFrame{
    	private JTextArea text = new JTextArea() ;
    	public MyKeyHandle(){
    		super.setTitle("Welcome To MLDN") ;
    		JScrollPane scr = new JScrollPane(text) ;
    		scr.setBounds(5,5,300,200) ;
    		super.add(scr) ;
    		text.addKeyListener(new KeyAdapter(){
    			public void keyTyped(KeyEvent e){
    				text.append("输入的内容是:" + e.getKeyChar() + "
    ") ;
    			} 
    		}) ;
    		super.addWindowListener(new WindowAdapter(){
    			public void windowClosing(WindowEvent e){
    				System.exit(1) ;
    			}
    		}) ;	// 加入事件
    		super.setSize(310,210) ;
    		super.setVisible(true) ;
    	}
    	
    };
    
    
    public class MyKeyEventDemo02{
    	public static void main(String args[]){
    		new MyKeyHandle() ;
    	}
    };
    

      

    import java.awt.event.WindowAdapter ;
    import java.awt.event.MouseListener ;
    import java.awt.event.WindowEvent ;
    import java.awt.event.MouseEvent ;
    import java.awt.Color ;
    import javax.swing.JFrame ;
    import java.awt.event.ActionListener ;
    import java.awt.event.WindowEvent ;
    import java.awt.event.ActionEvent ;
    import java.awt.Color ;
    import java.awt.GridLayout ;
    import java.awt.Font ;
    import javax.swing.JFrame ;
    import javax.swing.JButton ;
    import javax.swing.JLabel ;
    import javax.swing.JTextArea ;
    import javax.swing.JPasswordField ;
    import javax.swing.JScrollPane ;
    import javax.swing.JPanel ;
    
    class MyMouseHandle extends JFrame implements MouseListener{
        private JTextArea text = new JTextArea() ;
        public MyMouseHandle(){
            super.setTitle("Welcome To MLDN") ;
            JScrollPane scr = new JScrollPane(text) ;
            scr.setBounds(5,5,300,200) ;
            super.add(scr) ;
            text.addMouseListener(this) ;
            super.addWindowListener(new WindowAdapter(){
                public void windowClosing(WindowEvent e){
                    System.exit(1) ;
                }
            }) ;    // 加入事件
            super.setSize(310,210) ;
            super.setVisible(true) ;
        }
        public void mouseClicked(MouseEvent e){
            int c = e.getButton() ;
            String mouseInfo = null ;
            if(c==MouseEvent.BUTTON1){
                mouseInfo = "左键" ;
            }
            if(c==MouseEvent.BUTTON3){
                mouseInfo = "右键" ;
            }
            if(c==MouseEvent.BUTTON2){
                mouseInfo = "滚轴" ;
            }
            text.append("鼠标单击:" + mouseInfo + "
    ") ;
        }
    
        public void mouseEntered(MouseEvent e){
            text.append("鼠标进入组件。
    ") ;
        }
        public void mouseExited(MouseEvent e){
            text.append("鼠标离开组件。
    ") ;
        }
        public void mousePressed(MouseEvent e){
            text.append("鼠标按下。
    ") ;
        }
        public void mouseReleased(MouseEvent e){
            text.append("鼠标松开。
    ") ;
        }
        
    };
    
    
    public class MyMouseEventDemo01{
        public static void main(String args[]){
            new MyMouseHandle() ;
        }
    };
    import java.awt.event.WindowAdapter ;
    import java.awt.event.MouseAdapter ;
    import java.awt.event.WindowEvent ;
    import java.awt.event.MouseEvent ;
    import java.awt.Color ;
    import javax.swing.JFrame ;
    import java.awt.event.ActionListener ;
    import java.awt.event.WindowEvent ;
    import java.awt.event.ActionEvent ;
    import java.awt.Color ;
    import java.awt.GridLayout ;
    import java.awt.Font ;
    import javax.swing.JFrame ;
    import javax.swing.JButton ;
    import javax.swing.JLabel ;
    import javax.swing.JTextArea ;
    import javax.swing.JPasswordField ;
    import javax.swing.JScrollPane ;
    import javax.swing.JPanel ;
    
    class MyMouseHandle extends JFrame{
        private JTextArea text = new JTextArea() ;
        public MyMouseHandle(){
            super.setTitle("Welcome To MLDN") ;
            JScrollPane scr = new JScrollPane(text) ;
            scr.setBounds(5,5,300,200) ;
            super.add(scr) ;
            text.addMouseListener(new MouseAdapter(){
                public void mouseClicked(MouseEvent e){
                    int c = e.getButton() ;
                    String mouseInfo = null ;
                    if(c==MouseEvent.BUTTON1){
                        mouseInfo = "左键" ;
                    }
                    if(c==MouseEvent.BUTTON3){
                        mouseInfo = "右键" ;
                    }
                    if(c==MouseEvent.BUTTON2){
                        mouseInfo = "滚轴" ;
                    }
                    text.append("鼠标单击:" + mouseInfo + "
    ") ;
                }
            }) ;
            super.addWindowListener(new WindowAdapter(){
                public void windowClosing(WindowEvent e){
                    System.exit(1) ;
                }
            }) ;    // 加入事件
            super.setSize(310,210) ;
            super.setVisible(true) ;
        }
    };
    
    
    public class MyMouseEventDemo02{
        public static void main(String args[]){
            new MyMouseHandle() ;
        }
    };
    import java.awt.event.WindowAdapter ;
    import java.awt.event.MouseMotionListener ;
    import java.awt.event.WindowEvent ;
    import java.awt.event.MouseEvent ;
    import java.awt.Color ;
    import javax.swing.JFrame ;
    import java.awt.event.ActionListener ;
    import java.awt.event.WindowEvent ;
    import java.awt.event.ActionEvent ;
    import java.awt.Color ;
    import java.awt.GridLayout ;
    import java.awt.Font ;
    import javax.swing.JFrame ;
    import javax.swing.JButton ;
    import javax.swing.JLabel ;
    import javax.swing.JTextArea ;
    import javax.swing.JPasswordField ;
    import javax.swing.JScrollPane ;
    import javax.swing.JPanel ;
    
    class MyMouseMotionHandle extends JFrame{
        public MyMouseMotionHandle(){
            super.setTitle("Welcome To MLDN") ;
            super.addMouseMotionListener(new MouseMotionListener(){
                public void mouseDragged(MouseEvent e){
                    System.out.println("鼠标拖拽到:X = " + e.getX() + ",Y = " + e.getY()) ;
                }
                public void mouseMoved(MouseEvent e){
                    System.out.println("鼠标移动到窗体。") ;
                }
            }) ;
            super.addWindowListener(new WindowAdapter(){
                public void windowClosing(WindowEvent e){
                    System.exit(1) ;
                }
            }) ;    // 加入事件
            super.setSize(310,210) ;
            super.setVisible(true) ;
        }
    };
    
    
    public class MyMouseMotionEventDemo01{
        public static void main(String args[]){
            new MyMouseMotionHandle() ;
        }
    };
    import java.awt.event.WindowAdapter ;
    import java.awt.event.MouseMotionAdapter ;
    import java.awt.event.WindowEvent ;
    import java.awt.event.MouseEvent ;
    import java.awt.Color ;
    import javax.swing.JFrame ;
    import java.awt.event.ActionListener ;
    import java.awt.event.WindowEvent ;
    import java.awt.event.ActionEvent ;
    import java.awt.Color ;
    import java.awt.GridLayout ;
    import java.awt.Font ;
    import javax.swing.JFrame ;
    import javax.swing.JButton ;
    import javax.swing.JLabel ;
    import javax.swing.JTextArea ;
    import javax.swing.JPasswordField ;
    import javax.swing.JScrollPane ;
    import javax.swing.JPanel ;
    
    class MyMouseMotionHandle extends JFrame{
        public MyMouseMotionHandle(){
            super.setTitle("Welcome To MLDN") ;
            super.addMouseMotionListener(new MouseMotionAdapter(){
                public void mouseDragged(MouseEvent e){
                    System.out.println("鼠标拖拽到:X = " + e.getX() + ",Y = " + e.getY()) ;
                }
            }) ;
            super.addWindowListener(new WindowAdapter(){
                public void windowClosing(WindowEvent e){
                    System.exit(1) ;
                }
            }) ;    // 加入事件
            super.setSize(310,210) ;
            super.setVisible(true) ;
        }
    };
    
    
    public class MyMouseMotionEventDemo02{
        public static void main(String args[]){
            new MyMouseMotionHandle() ;
        }
    };
  • 相关阅读:
    JS LeetCode 1423. 可获得的最大点数简单题解
    SpringBoot 学集 (第六章) Docker
    Linux 学记 (第三章)
    Linux 学记 (第二章)
    Linux 学记 (第一章)
    SpringBoot 学集 (第五章) Web开发续
    SpringBoot 学集 (第四章)Web开发
    SpringBoot 学集 (第三章) 日志框架
    SpringBoot 学集 (第二章) 配置文件
    SpringBoot 学集 (第一章)
  • 原文地址:https://www.cnblogs.com/tszr/p/12398891.html
Copyright © 2011-2022 走看看