首页 > 作文

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

更新时间:2023-04-05 01:21:47 阅读: 评论:0

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("rief",font.italic+font.bold,28);        lab.tfont(font);        lab.ttext("请输入信息");        but.addactionlistener(new actionlistener() {            @override            public void actionperformed(actionevent e) {                if(e.getsource()==but){                    lab.ttext(text.gettext());                }            }        });        frame.addwindowlistener(new windowadapter() {            @override            public void windowclosing(windowevent e) {                super.windowclosing(e);                system.exit(1);            }        });        frame.tlayout(new gridlayout(2,1));        pan.tlayout(new gridlayout(1,2));        pan.add(text);        pan.add(but);        frame.add(pan);        frame.add(lab);        frame.pack();        frame.tvisible(true);    }}public class hello {    public static void main(string[] args) {        new actionhandle();    }}

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 urname;    private string password;    public logincheck(string urname,string password){        this.urname=urname;        this.password=password;    }    public boolean validate(){        if("abc".equals(urname)&&"123".equals(password)){            return true;        }el {            return fal;        }    }}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 urname = new jtextfield();    private jpasswordfield password = new jpasswordfield();    public actionhandle(){        font font = new font("rief",font.bold,12);        infolab.tfont(font);        sub.addactionlistener(new actionlistener() {            @override            public void actionperformed(actionevent e) {                if(e.getsource()==sub){                    string uname = urname.gettext();                    string upass = new string(password.getpassword());                    logincheck log = new logincheck(uname,upass);                    if(log.validate()){                        infolab.ttext("登录成功");                    }el {                        infolab.ttext("登录失败");                    }                }                if(e.getsource()==res){                    urname.ttext("");                    password.ttext("");                    infolab.ttext("用户登录");                }            }        });        frame.addwindowlistener(new windowadapter() {            @override            public void咸味腊八粥的做法 windowclosing(windowevent e) {                super.windowclosing(e);                system.exit(1);            }        });        frame.tlayout(null);        namelab.tbounds(5,5,60,20);        passlab.tbounds(5,30,60,20);        infolab.tbounds(5,65,22清正廉洁小故事0,30);        urname.tbounds(65,5,100,20);        password.tbounds(65,30,100,20);        sub.tbounds(165,5,60,20);        res.tbounds(165,30,60,20);        frame.add(namelab);        frame.add(passlab);        frame.add(infolab);        frame.add(urname);        frame.add(password);        frame.add(sub);        frame.add(res);        frame.tsize(280,130);        frame.tvisible(true);    }}public class hello {    public static void main(string[] args) {        new actionhandle();    }}

键盘事件及监听处理

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

方法作用void keytyped(keyevent e)键入某个键时调用void keypresd(keyevent e)键盘按下时调用void keyrelead(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.ttitle("一");        jscrollpane scr = new jscrollpane(text);        scr.tbounds(5,5,300,200);        super.add(scr);        text.addkeylistener(this);        super.tsize(310,210);        super.tvisible(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 keypresd(keyevent e) {        text.append("键盘"+keyevent.getkeytext(e.getkeycode())+"键按下\n");    }    @override    public void keyrelead(keyevent e) {        text.append("键盘"+keyevent.getkeytext(e.getkeycode())+"键松开\n");    }}public class hello {    public static void main(string[] args) {        new mykeyhandle();    }}

使用keyadapter适配器

import javax.swing.*;import java.awt.event.*;class mykeyhandle extends jframe{    private jtextarea text = new jtextarea();    public mykeyhandle(){        super.ttitle("一");        jscrollpane scr = new jscrollpane(text);        scr.tbounds(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.tsize(310,210);        super.tvisible(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();    }}

鼠标事件及监听处理

moulistener接口

方法作用void mouclicked(mouevent e)鼠标单击时调用(按下并释放)void moupresd(mouevent e)鼠标按下时调用void mourelead(mouevent e)鼠标松开时调用void mouentered(mouevent e)鼠标进入到组件时调用

mouevent类

方法作用public static final int button1表示鼠标左键的常量public static final int button2表示鼠标滚轴的常量public static final int button3表示鼠标右键的常量public int getbutton()以数字形式返回按下的鼠标键public int getclickcount()返回鼠标的单击次数public static string getmoumodifierstext(int modifiers)以字符串形式返回鼠标按下的键信息public int getx()返回鼠标操作的x坐标public int gety()返回鼠标操作的y坐标
import javax.swing.*;import java.awt.event.mouevent;import java.awt.event.moulistener;import java.awt.event.windowadapter;import java.awt.event.windowevent;class mymouhandle extends jframe implements moulistener{    private jtextarea text = new jtextarea();    public mymouhandle(){        super.ttitle("一");        jscrollpane src = new jscrollpane(text);        src.tbounds(5,5,300,200);        super.add(src);        text.addmoulistener(this);        super.tsize(310,210);        super.tvisible(true);        super.addwindowlistener(new windowadapter() {            @override            public void windowclosing(windowevent e) {                super.windowclosing(e);                system.exit(1);            }        });    }    public void mouclicked(mouevent e){        int c = e.getbutton();        string mouinfo = null;        if(c==mouevent.button1){            mouinfo="左键";        }el if(c==mouevent.button3){            mouinfo="右键";        }el {            mouinfo="滚轴";        }        text.append("鼠标单击"+mouinfo+"\n");    }    @override    public void moupresd(mouevent e) {        text.append("鼠标按下\n");    }    @override    public void mourelead(mouevent e) {        text.append("鼠标松开\n");    }    @override    public void mouentered(mouevent e) {        text.append("鼠标进入组件\n");    }    @override    public void mouexited(mouevent e) {        text.append("鼠标离开组件\n");    }}public class hello {    public static void main(string[] args) {        new mymouhandle();    }}

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

import javax.swing.*;import java.awt.event.mouadapter;import java.awt.event.mouevent;import java.awt.event.windowadapter;import java.awt.event.windowevent;class mymouhandle extends jframe{    private jtextarea text = new jtextarea();    public mymouhandle(){        super.ttitle("一");        jscro毕业证号查询llpane scr = new jscrollpane(text);        scr.tbounds(5,5,300,200);        super.add(scr);        text.addmoulistener(new mouadapter() {            @override            public void mouclicked(mouevent e) {                super.mouclicked(e);                int c = e.getbutton();                string mouinfo = null;                if(c==mouevent.button1){                    mouinfo="左键";                }el if (c==mouevent.button3){                    mouinfo="右键";                }el {                    mouinfo="滚轴";                }                text.append("鼠标单击"+mouinfo+"\n");            }        });        super.tsize(310,210);        super.tvisible(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 mymouhandle();    }}

鼠标拖拽事件及监听处理

moumotionlistener接口

方法作用void moudragged(mouevent e)在组件上按下并拖动时调用void moumoved(mouevent e)鼠标移动到组件时调用
import javax.swing.*;import java.awt.event.mouevent;import java.awt.event.moumotionlistener;import java.awt.event.windowadapter;import java.awt.event.windowevent;class mymoumotionhandle extends jframe{    public mymoumotionhandle(){        super.ttitle("一");        super.addmoumotionlistener(new moumotionlistener() {            @override            public void moudragged(mouevent e) {                system.out.println("鼠标拖拽到:x="+e.getx()+                        " y="+e.gety());            }            @override            public void moumoved(mouevent e) {                system.out.println("鼠标移动到窗体");            }        });        super.tsize(310,210);        super.tvisible(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 mymoumotionhandle();    }}

使用moumotionadapter类

super.addmoumotionlistener(new moumotionadapter() {    @override    public void moudragged(mouevent e) {        system.out.println("鼠标拖拽到:x="+e.getx()+                " y="+e.gety());    }});

到此这篇关于java 入门图形用户界面设计之事件处理的文章就介绍到这了,更多相关java 事件处理内容请搜索www.887551.com以前的塞翁失马焉知非福的故事文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!

本文发布于:2023-04-05 01:21:46,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/zuowen/5eea4d6ea40b88ef9be5e6cecc44c45c.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

本文word下载地址:Java 入门图形用户界面设计之事件处理下.doc

本文 PDF 下载地址:Java 入门图形用户界面设计之事件处理下.pdf

标签:鼠标   时调   按下   事件
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图