Java?入門圖形用戶界面設(shè)計之事件處理下
Java程序設(shè)計 圖形用戶界面 【八】事件處理下
動作事件及監(jiān)聽處理
想讓按鈕變得有意義,就必須使用事件處理
使用ActionListener接口處理按鈕的動作事件
方法 | 作用 |
---|---|
void actionPerformed(ActionEvent e) | 發(fā)生操作時調(diào)用 |
使用ActionListener監(jiān)聽
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(); } }
if(e.getSource()==but){} //判斷觸發(fā)源是否為按鈕
用戶登錄操作
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(); } }
鍵盤事件及監(jiān)聽處理
KeyListener接口對鍵盤的操作進(jìn)行監(jiān)聽
方法 | 作用 |
---|---|
void keyTyped(KeyEvent e) | 鍵入某個鍵時調(diào)用 |
void keyPressed(KeyEvent e) | 鍵盤按下時調(diào)用 |
void keyReleased(KeyEvent e) | 鍵盤松開時調(diào)用 |
通過KeyEvent取得鍵盤鍵入的內(nèi)容
方法 | 作用 |
---|---|
public char getKeyChar() | 返回鍵入的字符,只針對于keyTyped有意義 |
public int getKeyCode() | 返回輸入字符的鍵碼 |
public static String getKeyText(int keyCode) | 返回此鍵的信息 |
實(shí)現(xiàn)鍵盤監(jiān)聽
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("鍵盤"+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(); } }
使用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)聽處理
MouseListener接口
方法 | 作用 |
---|---|
void mouseClicked(MouseEvent e) | 鼠標(biāo)單擊時調(diào)用(按下并釋放) |
void mousePressed(MouseEvent e) | 鼠標(biāo)按下時調(diào)用 |
void mouseReleased(MouseEvent e) | 鼠標(biāo)松開時調(diào)用 |
void mouseEntered(MouseEvent e) | 鼠標(biāo)進(jìn)入到組件時調(diào)用 |
MouseEvent類
方法 | 作用 |
---|---|
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)松開\n"); } @Override public void mouseEntered(MouseEvent e) { text.append("鼠標(biāo)進(jìn)入組件\n"); } @Override public void mouseExited(MouseEvent e) { text.append("鼠標(biāo)離開組件\n"); } } public class Hello { public static void main(String[] args) { new MyMouseHandle(); } }
通過MouseAdapter實(shí)現(xiàn)對指定鼠標(biāo)操作的監(jiān)聽
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(); } }
鼠標(biāo)拖拽事件及監(jiān)聽處理
MouseMotionListener接口
方法 | 作用 |
---|---|
void mouseDragged(MouseEvent e) | 在組件上按下并拖動時調(diào)用 |
void mouseMoved(MouseEvent e) | 鼠標(biāo)移動到組件時調(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)移動到窗體"); } }); 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(); } }
使用MouseMotionAdapter類
super.addMouseMotionListener(new MouseMotionAdapter() { @Override public void mouseDragged(MouseEvent e) { System.out.println("鼠標(biāo)拖拽到:X="+e.getX()+ " Y="+e.getY()); } });
到此這篇關(guān)于Java 入門圖形用戶界面設(shè)計之事件處理的文章就介紹到這了,更多相關(guān)Java 事件處理內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot+Redisson自定義注解一次解決重復(fù)提交問題
項目中經(jīng)常會出現(xiàn)重復(fù)提交的問題,本文主要介紹了SpringBoot+Redisson自定義注解一次解決重復(fù)提交問題,具有一定的參考價值,感興趣的可以了解一下2024-03-03Java如何利用狀態(tài)模式(state pattern)替代if else
這篇文章主要給大家介紹了關(guān)于Java如何利用狀態(tài)模式(state pattern)替代if else的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11一文探索Apache HttpClient如何設(shè)定超時時間
Apache HttpClient是一個流行的Java庫,用于發(fā)送HTTP請求,這篇文章主要為大家介紹了Apache HttpClient如何設(shè)定超時時間,感興趣的小伙伴可以學(xué)習(xí)一下2023-10-10java實(shí)現(xiàn)創(chuàng)建臨時文件然后在程序退出時自動刪除文件
這篇文章主要介紹了java實(shí)現(xiàn)創(chuàng)建臨時文件然后在程序退出時自動刪除文件,從個人項目中提取出來的,小伙伴們可以直接拿走使用。2015-02-02