下面的是鍵盤和鼠標(biāo)的各種事件,看一下是不是你要的!

創(chuàng)新互聯(lián)堅持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:做網(wǎng)站、網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時代的東區(qū)網(wǎng)站設(shè)計、移動媒體設(shè)計的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!
鼠標(biāo)監(jiān)聽器
鼠標(biāo)監(jiān)聽器mouseListener監(jiān)聽鼠標(biāo)事件MouseEvent。相應(yīng)事件和處理方法如下表:
鼠標(biāo)事件 處理方法
MOUSE_CLICKED MouseClicked (MouseEvent) 鼠標(biāo)點擊(單或雙)
MOUSE_PRESSED MousePressed (MouseEvent) 鼠標(biāo)按下
MOUSE_RELEASED MouseReleased(MouseEvent) 鼠標(biāo)松開
MOUSE_ENTERED MouseEntered (MouseEvent) 鼠標(biāo)進入(某組件區(qū)域)
MOUSE_EXITED MouseExited (MouseEvent) 鼠標(biāo)離開(某組件區(qū)域)
鼠標(biāo)事件MouseEvent常用方法
int getClickCount() 得到點擊次數(shù)1 OR 2;
int getX(), int getY() 得到鼠標(biāo)的(象素)位置。
對于鼠標(biāo)的移動和拖放,另外用鼠標(biāo)運動監(jiān)聽器mouseMotionListener。因為許多程序不需要監(jiān)聽鼠標(biāo)運動,把兩者分開可簡化程序。有兩個方法處理鼠標(biāo)運動事件:
MOUSE_MOVED MouseMoved (MouseEvent) 鼠標(biāo)在移動MOUSE_DRAGGED MouseDragged(MouseEvent) 鼠標(biāo)被拖動
下面的例程演示簡單的鼠標(biāo)監(jiān)聽,并在屏幕上輸出鼠標(biāo)操作的信息。
例2
下面是討論MouseMotionListener的使用時機,它提供的下面的兩個方法,可讓你隨時掌握鼠標(biāo)的坐標(biāo),并處理拖曳鼠標(biāo)的操作。
MouseMotionListener mouseDragged(MouseEvent e)
mouseMoved(MouseEvent e)
-----------------------------------------------------------------------
下面的范例讓你知道鼠標(biāo)在JFrame上的坐標(biāo),并拖曳出直線來。
MouseDemo3.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/*為了達到畫線的功能,我們分別implements MouseListener與MouseMotionListener.
*/
public class MouseDemo3 extends JFrame implements MouseListener,MouseMotionListener{
int flag;//flag=1代表Mouse Moved,flag=2代表Mouse Dragged
int x=0;
int y=0;
int startx,starty,endx,endy;//起始坐標(biāo)與終點坐標(biāo)
public MouseDemo3(){
Container contentPane=getContentPane();
contentPane.addMouseListener(this);
contentPane.addMouseMotionListener(this);
setSize(300,300);
show();
addWindowListener(
new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
);
}
/*由mousePressed(),mouseReleased()取得示拖曳的開始與結(jié)束坐標(biāo)*/
public void mousePressed(MouseEvent e){
startx=e.getX();
starty=e.getY();
}
public void mouseReleased(MouseEvent e){
endx=e.getX();
endy=e.getY();
}
public void mouseEntered(MouseEvent e){ }
public void mouseExited(MouseEvent e){ }
public void mouseClicked(MouseEvent e){ }
/*mouseMoved(),mouseDragged()取得鼠標(biāo)移動的每一個坐標(biāo),并調(diào)用repaint()方法*/
public void mouseMoved(MouseEvent e){
flag=1;
x=e.getX();
y=e.getY();
repaint();
}
public void mouseDragged(MouseEvent e){
flag=2;
x=e.getX();
y=e.getY();
repaint();
}
public void update(Graphics g){
g.setColor(this.getBackground());
g.fillRect(0,0,getWidth(),getHeight());
paint(g);
}
public void paint(Graphics g){
g.setColor(Color.black);
if (flag==1){
g.drawString("鼠標(biāo)坐標(biāo):("+x+","+y+";)",10,50);
g.drawLine(startx,starty,endx,endy);
}
if (flag==2){
g.drawString("拖曳鼠標(biāo)價坐標(biāo):("+x+","+y+";)",10,50);
g.drawLine(startx,starty,x,y);
}
}
public static void main(String[] args){
new MouseDemo3();
}
}
例3
實現(xiàn)一個簡單的鼠標(biāo)控制程序MouseController。程序功能很簡單:隨機移動鼠標(biāo)并點擊左鍵。
代碼如下:
import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.InputEvent;
import java.util.Random;
/**
*
*/
/**
* @Create date 2007-11-6
*/
public class MouseController implements Runnable {
private Dimension dim;
private Random rand;
private Robot robot;
private volatile boolean stop = false;
public MouseController() {
dim = Toolkit.getDefaultToolkit().getScreenSize();
rand = new Random();
try {
robot = new Robot();
} catch (AWTException ex) {
ex.printStackTrace();
}
}
public void run() {
while(!stop) {
int x = rand.nextInt(dim.width);
int y = rand.nextInt(dim.height);
robot.mouseMove(x, y);
robot.mousePress(InputEvent.BUTTON1_MASK);
try {
Thread.sleep(2000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
public synchronized void stop() {
stop = true;
}
public static void main(String[] args) {
MouseController mc = new MouseController();
Thre
$False$
ad mcThread = new Thread(mc);
System.out.println("Mouse Controller start");
mcThread.start();
try {
Thread.sleep(60000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
mc.stop();
System.out.println("Mouse Controller stoped");
}
}
例4 本例程演示鼠標(biāo)監(jiān)聽器,鼠標(biāo)點擊和運動的監(jiān)聽。
///
// MouseEvt.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class MyPanel extends JPanel implements MouseMotionListener{
public MyPanel() {
addMouseListener(new MouseAdapter() {
publicvoid mouseClicked(MouseEvent evt) {
if (evt.getClickCount() = 2)
System.out.println("\n雙擊鼠標(biāo)");
int x = evt.getX(); int y = evt.getY();
System.out.println("點擊鼠標(biāo)的位置\nX:" + x + "\ty: " + y);
}
});
addMouseMotionListener(this);
}
publicvoid mouseMoved(MouseEvent evt){
System.out.println("\n鼠標(biāo)正在移動");
}
publicvoid mouseDragged(MouseEvent evt){
System.out.println("\n鼠標(biāo)正在拖動");
}
}
class MyFrame extends JFrame{
public MyFrame(){
setTitle("鼠標(biāo)事件示例程序");
setSize(300, 200);
addWindowListener(new WindowAdapter(){
publicvoid windowClosing(WindowEvent e){
System.exit(0);
}
} );
Container contentPane = getContentPane();
contentPane.add(new MyPanel());
}
}
publicclass MouseEvt{
publicstaticvoid main(String[] args){
JFrame frame = new MyFrame();
frame.setVisible(true);
}
}
///
簡要說明
在MyPanel的構(gòu)建器中添加了鼠標(biāo)適配器來監(jiān)聽鼠標(biāo)點擊數(shù)和位置。也添加了運動監(jiān)聽器來處理移動和拖放操作。
鼠標(biāo)雙擊事件
鼠標(biāo)的單雙擊事件在很多時候?qū)ξ覀儙椭艽?但是在JAVA中卻沒有給出鼠標(biāo)雙擊事件.我們可以通過事件源e.getClickCount()==2來判斷鼠標(biāo)點擊次數(shù)來實現(xiàn)鼠標(biāo)雙擊事件,例如: public class MyMouseListener
extends java.awt.event.MouseAdapter ...{
public void mouseClicked(MouseEvent e) ...{
System.out.println("clicked");
int clickTimes = e.getClickCount();
if (clickTimes == 2) ...{
System.out.println("Doublc Clicked!");
}
}
}
但是這樣并沒有達到我們的要求,因為在每次觸發(fā)雙擊事件的同時會觸發(fā)單擊事件.所以我們試圖改進以上方案,不使用系統(tǒng)提供的e.getClickCount()方法.可以考慮當(dāng)?shù)谝淮螁螕羰髽?biāo)的時候讓鼠標(biāo)單擊事件延時0.2秒執(zhí)行,而在這段時間里等待第二次單擊,如果有第二次單擊,那么我們執(zhí)行雙擊事件任務(wù),取消單擊任務(wù);如果在這段時間沒有等到再次單擊,那么執(zhí)行單擊任務(wù).
下面是用定時器延時單擊事件實現(xiàn)鼠標(biāo)雙擊事件,單擊和雙擊事件互不影響!
public class MyMouseListener
extends java.awt.event.MouseAdapter ...{
private static boolean flag=false;//用來判斷是否已經(jīng)執(zhí)行雙擊事件
private static int clickNum=0;//用來判斷是否該執(zhí)行雙擊事件
public void mouseClicked(MouseEvent e) ...{
final MouseEvent me=e;//事件源
this.flag=false;//每次點擊鼠標(biāo)初始化雙擊事件執(zhí)行標(biāo)志為false
if (this.clickNum == 1) ...{//當(dāng)clickNum==1時執(zhí)行雙擊事件
this.mouseDoubleClicked(me);//執(zhí)行雙擊事件
this.clickNum=0;//初始化雙擊事件執(zhí)行標(biāo)志為0
this.flag=true;//雙擊事件已執(zhí)行,事件標(biāo)志為true
return;
}
//定義定時器
java.util.Timer timer=new java.util.Timer();
//定時器開始執(zhí)行,延時0.2秒后確定是否執(zhí)行單擊事件
timer.schedule(new java.util.TimerTask() ...{
private int n=0;//記錄定時器執(zhí)行次數(shù)
public void run() ...{
if(MyMouseListener.flag)...{//如果雙擊事件已經(jīng)執(zhí)行,那么直接取消單擊執(zhí)行
n=0;
MyMouseListener.clickNum=0;
this.cancel();
return;
}
if (n == 1) ...{//定時器等待0.2秒后,雙擊事件仍未發(fā)生,執(zhí)行單擊事件
mouseSingleClicked(me);//執(zhí)行單擊事件
MyMouseListener.flag = true;
MyMouseListener.clickNum=0;
n=0;
this.cancel();
return;
}
clickNum++;
n++;
}
},new java.util.Date(),500);
}
/** *//**
* 鼠標(biāo)單擊事件
* @param e 事件源參數(shù)
*/
public void mouseSingleClicked(MouseEvent e)...{
System.out.println("Single Clicked!");
}
/** *//**
* 鼠標(biāo)雙擊事件
* @param e 事件源參數(shù)
*/
public void mouseDoubleClicked(MouseEvent e)...{
System.out.println("Doublc Clicked!");
}
}
//Test.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Test extends JFrame{
public Test(){
super("test");
init();
this.setSize(800,600);
this.setVisible(true);
}
private void init(){
JButton b=new JButton("button");
b.setBounds(50,50,100,30);
this.getContentPane().setLayout(null);
this.getContentPane().add(b);
b.addMouseListener(new MyMouseListener());
}
public static void main(String args[]){
new Test();
}
}
鍵盤監(jiān)聽器
鍵盤監(jiān)聽器KeyListener用來監(jiān)聽鍵盤事件。鍵盤事件有三種:KEY_PRESSED鍵按下了,KEY_RELEASED鍵松開了,KEY_TYPED鍵按過了。每個鍵都有一個鍵碼,普通鍵的鍵碼就是ASCII碼。鍵碼可通過int getKeyCode()方法獲得。Java設(shè)置了一種“虛擬鍵碼”(Virtual Key Code),用“VK_”作為前綴,例如VK_G。下面是某些特殊鍵的虛擬鍵碼。
鍵碼 含義 鍵碼 含義
VK_LEFT/VK_RIGHT 左右方向鍵 VK_CONTROL Ctrl鍵
VK_KP_UP 小鍵盤向上 VK_ATL Alt鍵
VK_PAUSE 暫停鍵 VK_SHIFT Shift鍵
VK_NUMBER0 小鍵盤數(shù)字0 VK_F1 功能鍵F1
VK_0 數(shù)字鍵0 VK_B 字母鍵B
虛擬鍵碼對應(yīng)的是鍵位,不區(qū)分大小寫。要想知道大小寫還必須查看修飾鍵(modifier key)。這由輸入事件InputEvent的getModifere()方法得到,把返回值與常量SHIFT_MASK, CONTROL_MASK, ALT_MASK比較,用以判定哪個修飾鍵處于“同時按下”狀態(tài)。
監(jiān)聽器KeyListener有三個方法keyPressed(KeyEvent evt),keyReleased(KeyEvent evt),keyTyped(KeyEvent evt),分別用于相應(yīng)事件發(fā)生后的處理。下面的例程中給自己的鍵盤監(jiān)聽器建立了showKeyEventMsg方法來顯示按鍵信息。
除了getKeyCode()方法得到鍵碼外,還可用getKeyChar()方法得到輸入的字符,用getKeyText(code)方法得到輸入的字符串。用isShiftDown()判斷shift鍵是否被按下等。當(dāng)按下Control鍵時getKeyText返回的是“ctrl",Alt和Shift也類似。
下面的例子演示得到鍵碼和字符的方法,在命令行上顯示結(jié)果。
例1 本例程演示鍵盤監(jiān)聽器后鍵碼的用法。
///
// KeyEvt.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class MyKeyListener implements KeyListener{
publicvoid keyPressed(KeyEvent evt) {
System.out.println("\n按鍵被按下");
showKeyEventMsg(evt);
}
publicvoid keyReleased(KeyEvent evt){ }
publicvoid keyTyped(KeyEvent evt) { }
privatevoid showKeyEventMsg(KeyEvent evt){//顯示按鍵事件信息
//得到按鍵對應(yīng)的整型數(shù)
int code = evt.getKeyCode();
//返回按鍵事件所代表的字符
char c = evt.getKeyChar();
//得到代表按鍵的字符串
String szText = evt.getKeyText(code);
if (code != KeyEvent.VK_UNDEFINED)
System.out.println("\n按鍵對應(yīng)的整型數(shù):"+code);
if (c != KeyEvent.CHAR_UNDEFINED)
System.out.println("\n與按鍵相聯(lián)系的字符:"+c);
if (evt.isShiftDown())
System.out.println("\n按鍵Shift被按下");
System.out.println("\n按鍵本身的字符串:"+szText);
}
}
class ButtonPanel extends JPanel{
public ButtonPanel() {
//新建一個文本域組件
tf = new JTextField(20);
add(tf);
//指定用來處理該按鈕事件的監(jiān)聽器對象為JPanel本身
myListener = new MyKeyListener();
tf.addKeyListener(myListener);
}
private JTextField tf;
private MyKeyListener myListener;
}
class ButtonFrame extends JFrame{
public ButtonFrame() {
setTitle("鍵盤事件示例程序");
setSize(300, 200);
setLocation(100,100);
addWindowListener(new WindowAdapter() {
publicvoid windowClosing(WindowEvent e)
{ System.exit(0);
}
});
Container ctPane = getContentPane();
ctPane.add(new ButtonPanel());
}
}
publicclass KeyEvt{
publicstaticvoid main(String[] args) {
JFrame frame = new ButtonFrame();
frame.setVisible(true);
}
}
///簡要說明
程序建立了自己的鍵盤監(jiān)聽器MyKeyListener,定義了一個新方法showKeyEventMsg用來在標(biāo)準(zhǔn)輸出設(shè)備上顯示有關(guān)的鍵盤信息。
在面版ButtonPanel上建立文本框并加鍵盤監(jiān)聽器。把面版ButtonPanel放到窗口ButtonFrame中。
加入在frame中的按鈕名為sure
Button sure=new Button("確定");
sure.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
frame1.setVisible(false);
Frame frame2=new Frame();
frame2.setVisible(true);
}
});
if(temp==".")//判斷只能輸入一個小數(shù),這個放在actionPerformed里的\x0d\x0a{\x0d\x0a for(int i=0;i
回答于?2022-11-16
文章標(biāo)題:Java輸入按鈕代碼,java輸入操作
地址分享:http://chinadenli.net/article13/dsesegs.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供標(biāo)簽優(yōu)化、網(wǎng)站設(shè)計、外貿(mào)建站、網(wǎng)站改版、網(wǎng)站制作、響應(yīng)式網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)