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 事件處理內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: