欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-06-03 23:02:45
java程序设计 图形用户界面 【八】事件处理下动作事件及监听处理想让按钮变得有意义,就必须使用事件处理使用actionlistener接口处理按钮的动作事件方法作用void actionperfor...

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();
    }
}

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

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

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();
    }
}

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

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

键盘事件及监听处理

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();
    }
}

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

使用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();
    }
}

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

通过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();
    }
}

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

鼠标拖拽事件及监听处理

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();
    }
}

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

使用mousemotionadapter类

super.addmousemotionlistener(new mousemotionadapter() {
    @override
    public void mousedragged(mouseevent e) {
        system.out.println("鼠标拖拽到:x="+e.getx()+
                " y="+e.gety());
    }
});

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