文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

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

2024-04-02 19:55

关注

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 事件处理内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     807人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     351人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     314人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     433人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     221人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-后端开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯