欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java?入門(mén)圖形用戶(hù)界面設(shè)計(jì)之事件處理下

 更新時(shí)間:2022年02月16日 15:46:09   作者:小旺不正經(jīng)  
圖形界面(簡(jiǎn)稱(chēng)GUI)是指采用圖形方式顯示的計(jì)算機(jī)操作用戶(hù)界面。與早期計(jì)算機(jī)使用的命令行界面相比,圖形界面對(duì)于用戶(hù)來(lái)說(shuō)在視覺(jué)上更易于接受,本篇精講Java語(yǔ)言中關(guān)于圖形用戶(hù)界面的事件處理

Java程序設(shè)計(jì) 圖形用戶(hù)界面 【八】事件處理下

動(dòng)作事件及監(jiān)聽(tīng)處理

想讓按鈕變得有意義,就必須使用事件處理

使用ActionListener接口處理按鈕的動(dòng)作事件

方法作用
void actionPerformed(ActionEvent e)發(fā)生操作時(shí)調(diào)用

使用ActionListener監(jiān)聽(tīng)

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("請(qǐng)輸入信息");
        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){} //判斷觸發(fā)源是否為按鈕

用戶(hù)登錄操作

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("用戶(hù)名:");
    private JLabel passLab = new JLabel("密  碼:");
    private JLabel infoLab = new JLabel("用戶(hù)登錄");
    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("用戶(hù)登錄");
                }
            }
        });
        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

鍵盤(pán)事件及監(jiān)聽(tīng)處理

KeyListener接口對(duì)鍵盤(pán)的操作進(jìn)行監(jiān)聽(tīng)

方法作用
void keyTyped(KeyEvent e)鍵入某個(gè)鍵時(shí)調(diào)用
void keyPressed(KeyEvent e)鍵盤(pán)按下時(shí)調(diào)用
void keyReleased(KeyEvent e)鍵盤(pán)松開(kāi)時(shí)調(diào)用

通過(guò)KeyEvent取得鍵盤(pán)鍵入的內(nèi)容

方法作用
public char getKeyChar()返回鍵入的字符,只針對(duì)于keyTyped有意義
public int getKeyCode()返回輸入字符的鍵碼
public static String getKeyText(int keyCode)返回此鍵的信息

實(shí)現(xiàn)鍵盤(pán)監(jiān)聽(tīng)

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("輸入的內(nèi)容是:"+e.getKeyChar()+"\n");
    }

    @Override
    public void keyPressed(KeyEvent e) {
        text.append("鍵盤(pán)"+KeyEvent.getKeyText(e.getKeyCode())+"鍵按下\n");
    }

    @Override
    public void keyReleased(KeyEvent e) {
        text.append("鍵盤(pán)"+KeyEvent.getKeyText(e.getKeyCode())+"鍵松開(kāi)\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("輸入的內(nèi)容是:"+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();
    }
}

鼠標(biāo)事件及監(jiān)聽(tīng)處理

MouseListener接口

方法作用
void mouseClicked(MouseEvent e)鼠標(biāo)單擊時(shí)調(diào)用(按下并釋放)
void mousePressed(MouseEvent e)鼠標(biāo)按下時(shí)調(diào)用
void mouseReleased(MouseEvent e)鼠標(biāo)松開(kāi)時(shí)調(diào)用
void mouseEntered(MouseEvent e)鼠標(biāo)進(jìn)入到組件時(shí)調(diào)用

MouseEvent類(lèi)

方法作用
public static final int BUTTON1表示鼠標(biāo)左鍵的常量
public static final int BUTTON2表示鼠標(biāo)滾軸的常量
public static final int BUTTON3表示鼠標(biāo)右鍵的常量
public int getButton()以數(shù)字形式返回按下的鼠標(biāo)鍵
public int getClickCount()返回鼠標(biāo)的單擊次數(shù)
public static String getMouseModifiersText(int modifiers)以字符串形式返回鼠標(biāo)按下的鍵信息
public int getX()返回鼠標(biāo)操作的X坐標(biāo)
public int getY()返回鼠標(biāo)操作的Y坐標(biāo)
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("鼠標(biāo)單擊"+mouseInfo+"\n");
    }

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

    @Override
    public void mouseReleased(MouseEvent e) {
        text.append("鼠標(biāo)松開(kāi)\n");
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        text.append("鼠標(biāo)進(jìn)入組件\n");
    }

    @Override
    public void mouseExited(MouseEvent e) {
        text.append("鼠標(biāo)離開(kāi)組件\n");
    }
}
public class Hello {
    public static void main(String[] args) {
        new MyMouseHandle();
    }
}

image-20220212161416692

通過(guò)MouseAdapter實(shí)現(xiàn)對(duì)指定鼠標(biāo)操作的監(jiān)聽(tīng)

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("鼠標(biāo)單擊"+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

鼠標(biāo)拖拽事件及監(jiān)聽(tīng)處理

MouseMotionListener接口

方法作用
void mouseDragged(MouseEvent e)在組件上按下并拖動(dòng)時(shí)調(diào)用
void mouseMoved(MouseEvent e)鼠標(biāo)移動(dòng)到組件時(shí)調(diào)用
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("鼠標(biāo)拖拽到:X="+e.getX()+
                        " Y="+e.getY());
            }

            @Override
            public void mouseMoved(MouseEvent e) {
                System.out.println("鼠標(biāo)移動(dòng)到窗體");
            }
        });
        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類(lèi)

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

到此這篇關(guān)于Java 入門(mén)圖形用戶(hù)界面設(shè)計(jì)之事件處理的文章就介紹到這了,更多相關(guān)Java 事件處理內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論