這都沒關(guān)系。只是自己的習(xí)慣而已。只要能把業(yè)務(wù)處理好就可以了。
創(chuàng)新互聯(lián)公司長(zhǎng)期為1000+客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對(duì)不同對(duì)象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺(tái),與合作伙伴共同營(yíng)造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為唐縣企業(yè)提供專業(yè)的網(wǎng)站制作、做網(wǎng)站,唐縣網(wǎng)站改版等技術(shù)服務(wù)。擁有10多年豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開發(fā)。
下面的是鍵盤和鼠標(biāo)的各種事件,看一下是不是你要的!
鼠標(biāo)監(jiān)聽器
鼠標(biāo)監(jiān)聽器mouseListener監(jiān)聽鼠標(biāo)事件MouseEvent。相應(yīng)事件和處理方法如下表:
鼠標(biāo)事件 處理方法
MOUSE_CLICKED MouseClicked (MouseEvent) 鼠標(biāo)點(diǎn)擊(單或雙)
MOUSE_PRESSED MousePressed (MouseEvent) 鼠標(biāo)按下
MOUSE_RELEASED MouseReleased(MouseEvent) 鼠標(biāo)松開
MOUSE_ENTERED MouseEntered (MouseEvent) 鼠標(biāo)進(jìn)入(某組件區(qū)域)
MOUSE_EXITED MouseExited (MouseEvent) 鼠標(biāo)離開(某組件區(qū)域)
鼠標(biāo)事件MouseEvent常用方法
int getClickCount() 得到點(diǎn)擊次數(shù)1 OR 2;
int getX(), int getY() 得到鼠標(biāo)的(象素)位置。
對(duì)于鼠標(biāo)的移動(dòng)和拖放,另外用鼠標(biāo)運(yùn)動(dòng)監(jiān)聽器mouseMotionListener。因?yàn)樵S多程序不需要監(jiān)聽鼠標(biāo)運(yùn)動(dòng),把兩者分開可簡(jiǎn)化程序。有兩個(gè)方法處理鼠標(biāo)運(yùn)動(dòng)事件:
MOUSE_MOVED MouseMoved (MouseEvent) 鼠標(biāo)在移動(dòng)MOUSE_DRAGGED MouseDragged(MouseEvent) 鼠標(biāo)被拖動(dòng)
下面的例程演示簡(jiǎn)單的鼠標(biāo)監(jiān)聽,并在屏幕上輸出鼠標(biāo)操作的信息。
例2
下面是討論MouseMotionListener的使用時(shí)機(jī),它提供的下面的兩個(gè)方法,可讓你隨時(shí)掌握鼠標(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.*;
/*為了達(dá)到畫線的功能,我們分別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)與終點(diǎn)坐標(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)移動(dòng)的每一個(gè)坐標(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)價(jià)坐標(biāo):("+x+","+y+";)",10,50);
g.drawLine(startx,starty,x,y);
}
}
public static void main(String[] args){
new MouseDemo3();
}
}
例3
實(shí)現(xiàn)一個(gè)簡(jiǎn)單的鼠標(biāo)控制程序MouseController。程序功能很簡(jiǎn)單:隨機(jī)移動(dòng)鼠標(biāo)并點(diǎn)擊左鍵。
代碼如下:
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)點(diǎn)擊和運(yùn)動(dòng)的監(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("點(diǎn)擊鼠標(biāo)的位置\nX:" + x + "\ty: " + y);
}
});
addMouseMotionListener(this);
}
publicvoid mouseMoved(MouseEvent evt){
System.out.println("\n鼠標(biāo)正在移動(dòng)");
}
publicvoid mouseDragged(MouseEvent evt){
System.out.println("\n鼠標(biāo)正在拖動(dòng)");
}
}
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);
}
}
///
簡(jiǎn)要說明
在MyPanel的構(gòu)建器中添加了鼠標(biāo)適配器來監(jiān)聽鼠標(biāo)點(diǎn)擊數(shù)和位置。也添加了運(yùn)動(dòng)監(jiān)聽器來處理移動(dòng)和拖放操作。
鼠標(biāo)雙擊事件
鼠標(biāo)的單雙擊事件在很多時(shí)候?qū)ξ覀儙椭艽?但是在JAVA中卻沒有給出鼠標(biāo)雙擊事件.我們可以通過事件源e.getClickCount()==2來判斷鼠標(biāo)點(diǎn)擊次數(shù)來實(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!");
}
}
}
但是這樣并沒有達(dá)到我們的要求,因?yàn)樵诿看斡|發(fā)雙擊事件的同時(shí)會(huì)觸發(fā)單擊事件.所以我們?cè)噲D改進(jìn)以上方案,不使用系統(tǒng)提供的e.getClickCount()方法.可以考慮當(dāng)?shù)谝淮螁螕羰髽?biāo)的時(shí)候讓鼠標(biāo)單擊事件延時(shí)0.2秒執(zhí)行,而在這段時(shí)間里等待第二次單擊,如果有第二次單擊,那么我們執(zhí)行雙擊事件任務(wù),取消單擊任務(wù);如果在這段時(shí)間沒有等到再次單擊,那么執(zhí)行單擊任務(wù).
下面是用定時(shí)器延時(shí)單擊事件實(shí)現(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;//每次點(diǎn)擊鼠標(biāo)初始化雙擊事件執(zhí)行標(biāo)志為false
if (this.clickNum == 1) ...{//當(dāng)clickNum==1時(shí)執(zhí)行雙擊事件
this.mouseDoubleClicked(me);//執(zhí)行雙擊事件
this.clickNum=0;//初始化雙擊事件執(zhí)行標(biāo)志為0
this.flag=true;//雙擊事件已執(zhí)行,事件標(biāo)志為true
return;
}
//定義定時(shí)器
java.util.Timer timer=new java.util.Timer();
//定時(shí)器開始執(zhí)行,延時(shí)0.2秒后確定是否執(zhí)行單擊事件
timer.schedule(new java.util.TimerTask() ...{
private int n=0;//記錄定時(shí)器執(zhí)行次數(shù)
public void run() ...{
if(MyMouseListener.flag)...{//如果雙擊事件已經(jīng)執(zhí)行,那么直接取消單擊執(zhí)行
n=0;
MyMouseListener.clickNum=0;
this.cancel();
return;
}
if (n == 1) ...{//定時(shí)器等待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鍵按過了。每個(gè)鍵都有一個(gè)鍵碼,普通鍵的鍵碼就是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
虛擬鍵碼對(duì)應(yīng)的是鍵位,不區(qū)分大小寫。要想知道大小寫還必須查看修飾鍵(modifier key)。這由輸入事件InputEvent的getModifere()方法得到,把返回值與常量SHIFT_MASK, CONTROL_MASK, ALT_MASK比較,用以判定哪個(gè)修飾鍵處于“同時(shí)按下”狀態(tài)。
監(jiān)聽器KeyListener有三個(gè)方法keyPressed(KeyEvent evt),keyReleased(KeyEvent evt),keyTyped(KeyEvent evt),分別用于相應(yīng)事件發(fā)生后的處理。下面的例程中給自己的鍵盤監(jiān)聽器建立了showKeyEventMsg方法來顯示按鍵信息。
除了getKeyCode()方法得到鍵碼外,還可用getKeyChar()方法得到輸入的字符,用getKeyText(code)方法得到輸入的字符串。用isShiftDown()判斷shift鍵是否被按下等。當(dāng)按下Control鍵時(shí)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){//顯示按鍵事件信息
//得到按鍵對(duì)應(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按鍵對(duì)應(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() {
//新建一個(gè)文本域組件
tf = new JTextField(20);
add(tf);
//指定用來處理該按鈕事件的監(jiān)聽器對(duì)象為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)要說明
程序建立了自己的鍵盤監(jiān)聽器MyKeyListener,定義了一個(gè)新方法showKeyEventMsg用來在標(biāo)準(zhǔn)輸出設(shè)備上顯示有關(guān)的鍵盤信息。
在面版ButtonPanel上建立文本框并加鍵盤監(jiān)聽器。把面版ButtonPanel放到窗口ButtonFrame中。
打開Eclipse,找到菜單欄Window - Preferences
在打開的窗口左側(cè),找到General-Appearance-Colors and Fonts
在窗口的右側(cè),就可以對(duì)具體的某一個(gè)語言設(shè)置,以Java語言為例,展開Java,選中“Java Editor Text Font”,點(diǎn)擊右側(cè)的“Edit”按鈕
在打開的字體設(shè)置頁面,即可對(duì)字體大小和樣式進(jìn)行設(shè)置,設(shè)置完成后,點(diǎn)擊確定保存就可以了。
你的java和圖片放在一個(gè)目錄,
我都是放在C盤根目錄了,
給你稍微改了一下代碼:
import?java.awt.*;
import?javax.swing.*;
public?class?TestGra?extends?JFrame?{
Container?c?=?getContentPane();
JLabel?lb;
Image?image;
public?TestGra()?{
//?就改這里了
ImageIcon?img?=?new?ImageIcon(System.getProperty("user.dir")?+?"\\1.jpeg");
lb?=?new?JLabel(img);
add(lb,?BorderLayout.CENTER);
setSize(800,?600);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public?static?void?main(String?as[])?{
new?TestGra();
}
}
文章標(biāo)題:java靠左代碼 java左邊的不見了
鏈接地址:http://www.muchs.cn/article36/ddehhpg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供靜態(tài)網(wǎng)站、電子商務(wù)、服務(wù)器托管、用戶體驗(yàn)、網(wǎng)站設(shè)計(jì)公司、外貿(mào)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)