java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > Java 事件处理

Java 入门图形用户界面设计之事件处理下

作者:小旺不正经

图形界面(简称GUI)是指采用图形方式显示的计算机操作用户界面。与早期计算机使用的命令行界面相比,图形界面对于用户来说在视觉上更易于接受,本篇精讲Java语言中关于图形用户界面的事件处理

Java程序设计 图形用户界面 【八】事件处理下

动作事件及监听处理

想让按钮变得有意义,就必须使用事件处理

使用ActionListener接口处理按钮的动作事件

方法作用
void actionPerformed(ActionEvent e)发生操作时调用

使用ActionListener监听

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

class ActionHandle{
    private JFrame frame = new JFrame("一");
    private JButton but = new JButton("显示");
    private JLabel lab = new JLabel();
    private JTextField text = new JTextField(10);
    private JPanel pan = new JPanel();
    public ActionHandle(){
        Font font = new Font("Serief",Font.ITALIC+Font.BOLD,28);
        lab.setFont(font);
        lab.setText("请输入信息");
        but.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(e.getSource()==but){
                    lab.setText(text.getText());
                }
            }
        });
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                super.windowClosing(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.setVisible(true);
    }
}
public class Hello {
    public static void main(String[] args) {
        new ActionHandle();
    }
}

image-20220212125346032

image-20220212125358374

if(e.getSource()==but){} //判断触发源是否为按钮

用户登录操作

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

class LoginCheck{
    private String username;
    private String password;
    public LoginCheck(String username,String password){
        this.username=username;
        this.password=password;
    }
    public boolean validate(){
        if("abc".equals(username)&&"123".equals(password)){
            return true;
        }else {
            return false;
        }
    }
}
class ActionHandle{
    private JFrame frame = new JFrame("一");
    private JButton sub = new JButton("登录");
    private JButton res = new JButton("重置");
    private JLabel nameLab = new JLabel("用户名:");
    private JLabel passLab = new JLabel("密  码:");
    private JLabel infoLab = new JLabel("用户登录");
    private JTextField username = new JTextField();
    private JPasswordField password = new JPasswordField();
    public ActionHandle(){
        Font font = new Font("Serief",Font.BOLD,12);
        infoLab.setFont(font);
        sub.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(e.getSource()==sub){
                    String uname = username.getText();
                    String upass = new String(password.getPassword());
                    LoginCheck log = new LoginCheck(uname,upass);
                    if(log.validate()){
                        infoLab.setText("登录成功");
                    }else {
                        infoLab.setText("登录失败");
                    }
                }
                if(e.getSource()==res){
                    username.setText("");
                    password.setText("");
                    infoLab.setText("用户登录");
                }
            }
        });
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                super.windowClosing(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);
        username.setBounds(65,5,100,20);
        password.setBounds(65,30,100,20);
        sub.setBounds(165,5,60,20);
        res.setBounds(165,30,60,20);
        frame.add(nameLab);
        frame.add(passLab);
        frame.add(infoLab);
        frame.add(username);
        frame.add(password);
        frame.add(sub);
        frame.add(res);
        frame.setSize(280,130);
        frame.setVisible(true);
    }
}
public class Hello {
    public static void main(String[] args) {
        new ActionHandle();
    }
}

image-20220212132501548

image-20220212132521043

键盘事件及监听处理

KeyListener接口对键盘的操作进行监听

方法作用
void keyTyped(KeyEvent e)键入某个键时调用
void keyPressed(KeyEvent e)键盘按下时调用
void keyReleased(KeyEvent e)键盘松开时调用

通过KeyEvent取得键盘键入的内容

方法作用
public char getKeyChar()返回键入的字符,只针对于keyTyped有意义
public int getKeyCode()返回输入字符的键码
public static String getKeyText(int keyCode)返回此键的信息

实现键盘监听

import javax.swing.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

class MyKeyHandle extends JFrame implements KeyListener{
    private JTextArea text = new JTextArea();
    public MyKeyHandle(){
        super.setTitle("一");
        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() {
            @Override
            public void windowClosing(WindowEvent e) {
                super.windowClosing(e);
                System.exit(1);
            }
        });
    }

    @Override
    public void keyTyped(KeyEvent e) {
        text.append("输入的内容是:"+e.getKeyChar()+"\n");
    }

    @Override
    public void keyPressed(KeyEvent e) {
        text.append("键盘"+KeyEvent.getKeyText(e.getKeyCode())+"键按下\n");
    }

    @Override
    public void keyReleased(KeyEvent e) {
        text.append("键盘"+KeyEvent.getKeyText(e.getKeyCode())+"键松开\n");
    }
}
public class Hello {
    public static void main(String[] args) {
        new MyKeyHandle();
    }
}

image-20220212152738888

使用KeyAdapter适配器

import javax.swing.*;
import java.awt.event.*;

class MykeyHandle extends JFrame{
    private JTextArea text = new JTextArea();
    public MykeyHandle(){
        super.setTitle("一");
        JScrollPane scr = new JScrollPane(text);
        scr.setBounds(5,5,300,200);
        super.add(scr);
        text.addKeyListener(new KeyAdapter() {
            @Override
            public void keyTyped(KeyEvent e) {
                super.keyTyped(e);
                text.append("输入的内容是:"+e.getKeyChar()+"\n");
            }
        });
        super.setSize(310,210);
        super.setVisible(true);
        super.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                super.windowClosing(e);
                System.exit(1);
            }
        });
    }
}
public class Hello {
    public static void main(String[] args) {
        new MykeyHandle();
    }
}

鼠标事件及监听处理

MouseListener接口

方法作用
void mouseClicked(MouseEvent e)鼠标单击时调用(按下并释放)
void mousePressed(MouseEvent e)鼠标按下时调用
void mouseReleased(MouseEvent e)鼠标松开时调用
void mouseEntered(MouseEvent e)鼠标进入到组件时调用

MouseEvent类

方法作用
public static final int BUTTON1表示鼠标左键的常量
public static final int BUTTON2表示鼠标滚轴的常量
public static final int BUTTON3表示鼠标右键的常量
public int getButton()以数字形式返回按下的鼠标键
public int getClickCount()返回鼠标的单击次数
public static String getMouseModifiersText(int modifiers)以字符串形式返回鼠标按下的键信息
public int getX()返回鼠标操作的X坐标
public int getY()返回鼠标操作的Y坐标
import javax.swing.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

class MyMouseHandle extends JFrame implements MouseListener{
    private JTextArea text = new JTextArea();
    public MyMouseHandle(){
        super.setTitle("一");
        JScrollPane src = new JScrollPane(text);
        src.setBounds(5,5,300,200);
        super.add(src);
        text.addMouseListener(this);
        super.setSize(310,210);
        super.setVisible(true);
        super.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                super.windowClosing(e);
                System.exit(1);
            }
        });
    }
    public void mouseClicked(MouseEvent e){
        int c = e.getButton();
        String mouseInfo = null;
        if(c==MouseEvent.BUTTON1){
            mouseInfo="左键";
        }else if(c==MouseEvent.BUTTON3){
            mouseInfo="右键";
        }else {
            mouseInfo="滚轴";
        }
        text.append("鼠标单击"+mouseInfo+"\n");
    }

    @Override
    public void mousePressed(MouseEvent e) {
        text.append("鼠标按下\n");
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        text.append("鼠标松开\n");
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        text.append("鼠标进入组件\n");
    }

    @Override
    public void mouseExited(MouseEvent e) {
        text.append("鼠标离开组件\n");
    }
}
public class Hello {
    public static void main(String[] args) {
        new MyMouseHandle();
    }
}

image-20220212161416692

通过MouseAdapter实现对指定鼠标操作的监听

import javax.swing.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

class MyMouseHandle extends JFrame{
    private JTextArea text = new JTextArea();
    public MyMouseHandle(){
        super.setTitle("一");
        JScrollPane scr = new JScrollPane(text);
        scr.setBounds(5,5,300,200);
        super.add(scr);
        text.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                int c = e.getButton();
                String mouseInfo = null;
                if(c==MouseEvent.BUTTON1){
                    mouseInfo="左键";
                }else if (c==MouseEvent.BUTTON3){
                    mouseInfo="右键";
                }else {
                    mouseInfo="滚轴";
                }
                text.append("鼠标单击"+mouseInfo+"\n");
            }
        });
        super.setSize(310,210);
        super.setVisible(true);
        super.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                super.windowClosing(e);
                System.exit(1);
            }
        });
    }
}
public class Hello {
    public static void main(String[] args) {
        new MyMouseHandle();
    }
}

image-20220212164231754

鼠标拖拽事件及监听处理

MouseMotionListener接口

方法作用
void mouseDragged(MouseEvent e)在组件上按下并拖动时调用
void mouseMoved(MouseEvent e)鼠标移动到组件时调用
import javax.swing.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

class MyMouseMotionHandle extends JFrame{
    public MyMouseMotionHandle(){
        super.setTitle("一");
        super.addMouseMotionListener(new MouseMotionListener() {
            @Override
            public void mouseDragged(MouseEvent e) {
                System.out.println("鼠标拖拽到:X="+e.getX()+
                        " Y="+e.getY());
            }

            @Override
            public void mouseMoved(MouseEvent e) {
                System.out.println("鼠标移动到窗体");
            }
        });
        super.setSize(310,210);
        super.setVisible(true);
        super.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                super.windowClosing(e);
                System.exit(1);
            }
        });
    }
}
public class Hello {
    public static void main(String[] args) {
        new MyMouseMotionHandle();
    }
}

image-20220212170638251

使用MouseMotionAdapter类

super.addMouseMotionListener(new MouseMotionAdapter() {
    @Override
    public void mouseDragged(MouseEvent e) {
        System.out.println("鼠标拖拽到:X="+e.getX()+
                " Y="+e.getY());
    }
});

到此这篇关于Java 入门图形用户界面设计之事件处理的文章就介绍到这了,更多相关Java 事件处理内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文