java学习--GUI3

事件处理

 监听适配器

import java.awt.Color;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;

public class MyWindowEvent {
    public static void main(String[] args) {
        JFrame frame = new JFrame("Welcome to leaf's blog");
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent arg0) {
                System.exit(-1);
            }
        });

        frame.setSize(100, 100);
        frame.setLocation(200, 200);
        frame.setBackground(Color.WHITE);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

动作事件

  用户登录

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

class ActionHandle {
    private JFrame frame = new JFrame("Welcome to leaf's blog");
    private JButton submit = new JButton("登录");
    private JButton resert = new JButton("重置");
    private JLabel nameLab = new JLabel("用户名:");
    private JLabel passLab = new JLabel("密  码:");
    private JLabel infoLab = new JLabel("用户登录系统");
    private JTextField nameText = new JTextField();
    private JPasswordField passText = new JPasswordField();

    public ActionHandle() {
        Font fnt = new Font("Serief", Font.BOLD, 12);
        infoLab.setFont(fnt);
        submit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                if (arg0.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("登陆失败");
                    }
                }
                if (arg0.getSource() == resert) {
                    nameText.setText("");
                    passText.setText("");
                    infoLab.setText("用户登录系统!");

                }
            }
        });

        frame.addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent arg0) {
                System.exit(-1);
        }
        });
        frame.setLayout(null);
        nameLab.setBounds(5, 5, 60, 20);
        passLab.setBounds(5, 30, 60, 20);
        infoLab.setBounds(5, 65, 2200, 30);
        nameText.setBounds(65, 30, 100, 20);
        passText.setBounds(65, 3, 100, 20);
        submit.setBounds(165, 5, 60, 20);
        resert.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(resert);
        frame.setSize(100, 100);
        frame.setLocation(200, 200);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

class LoginCheck {
    private String name;
    private String password;
    public LoginCheck(String name, String password) {
        this.name = name;
        this.password = password;
    }

    public boolean validate() {
        if ("leaf".equals(name) && "leaf".equals(password)) {
            return true;
        } else {
            return false;
        }
    }
}

public class MyActionEvent {
    public static void main(String[] args) {
        new ActionHandle();
    }
}

 键盘事件及监听处理

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

class MyKeyHandle extends JFrame implements KeyListener {
    private JTextArea text = new JTextArea();

    public MyKeyHandle() {
        super.setTitle("Welcome to leaf's blog");
        JScrollPane scr = new JScrollPane(text);
        scr.setBounds(5, 5, 300, 200);
        super.add(scr);
        text.addKeyListener(this);
        super.setSize(310, 210);
        super.setVisible(true);
        super.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent arg0) {
                System.exit(-1);
            }
        });
    }

    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 MyKeyEventDemo {
    public static void main(String[] args) {
        new MyKeyHandle();
    }
}

也可以使用KeyAdapter适配器完成键盘事件的监听

import java.awt.event.KeyEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

class MyKeyHandle extends JFrame {
    private JTextArea text = new JTextArea();

    public MyKeyHandle() {
        super.setTitle("Welcome to leaf's blog");
        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.setSize(310, 210);
        super.setVisible(true);
        super.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent arg0) {
                System.exit(-1);
            }
        });
    }
}

public class MyKeyEventDemo2 {
    public static void main(String[] args) {
        new MyKeyHandle();
    }
}

鼠标事件及监听处理

import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

class MyMouseHandle extends JFrame implements MouseListener {
    private JTextArea text = new JTextArea();

    public MyMouseHandle() {
        super.setTitle("Welcome to leaf's blog");
        JScrollPane scr = new JScrollPane(text);
        scr.setBounds(5, 5, 300, 200);
        super.add(scr);
        text.addMouseListener(this);
        super.setSize(310, 210);
        super.setVisible(true);
        super.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent arg0) {
                System.exit(-1);
            }
        });
    }

    public void mouseClicked(MouseEvent e) {
        int c = e.getButton();
        String mouseInfo = null;
        if (c == MouseEvent.BUTTON1) {
            mouseInfo = "左键";
        } else if (c == MouseEvent.BUTTON2) {
            mouseInfo = "右键";
        } else {
            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 MyMouseEventDemo {
    public static void main(String[] args) {
        new MyMouseHandle();    
    }
}

也可以使用MouseAdapter完成对鼠标事件的监听

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

class MyMouseHandle extends JFrame {
    private JTextArea text = new JTextArea();

    public MyMouseHandle() {
        super.setTitle("Welcome to leaf's blog");
        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 = "左键";
                } else if (c == MouseEvent.BUTTON2) {
                    mouseInfo = "右键";
                } else {
                    mouseInfo = "滚轴";
                }
                text.append("鼠标单击:" + mouseInfo + "
");
            }
        });

        super.setSize(310, 210);
        super.setVisible(true);
        super.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent arg0) {
                System.exit(-1);
            }
        });
    }
}

public class MyMouseEventDemo2 {
    public static void main(String[] args) {
        new MyMouseHandle();    
    }
}

鼠标拖拽事件及监听处理

import java.awt.event.MouseMotionListener;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;

class MyMouseMotionHandle extends JFrame {
    public MyMouseMotionHandle() {
        super.setTitle("Welcome to leaf's blog");
        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.setSize(310, 210);
        super.setVisible(true);
        super.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent arg0) {
                System.exit(-1);
            }
        });
    }
}

public class MyMouseMotionEventDemo {
    public static void main(String[] args) {
        new MyMouseMotionHandle();
    }
}
原文地址:https://www.cnblogs.com/lea-fu/p/3254485.html