這個(gè)用MouseListener里的mouseEntered(MouseEvent
專注于為中小企業(yè)提供做網(wǎng)站、網(wǎng)站建設(shè)服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)鉛山免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動了成百上千家企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。
e)方法。
鼠標(biāo)進(jìn)入?yún)^(qū)域就產(chǎn)生事件反應(yīng)。
如果之前申明了點(diǎn)擊事件可以在entered時(shí)間里呼叫clicked事件。
你這樣不行啊,一閃一閃的根本看不到顯示效果或者更本沒有顯示,繪圖的方法要寫在paintComponent(Graphics g)里面才能長久保存。
我知道你想在mouseClicked里面做什么,但是在這里進(jìn)行繪圖,repaint沒有立即被執(zhí)行,所以有可能沒有任何顯示;如果repaint執(zhí)行了,也很有可能立刻被后面的繪圖所覆蓋。最好的辦法是在這里出發(fā)一個(gè)標(biāo)志,然后調(diào)用repaint函數(shù),在paintComponent函數(shù)中對這個(gè)標(biāo)志進(jìn)行繪制,決定到底該怎么畫。
手寫了一段代碼,沒有編譯,你可以參考一下:
package colin;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
public class JXFrame extends JFrame
{
boolean mouseCliked = false;
int x,y;
public void paintComponent(Graphics g)
{
if(mouseCliked )
{
g.setColor(255,255,0);//必須有這一步,否則畫筆顏色為默認(rèn)背景色,是根本看不出任何線條效果的。
g.drawLine(0,0,x,y);
}
}
public void setPoint(int x,int y)
{
this.x = x;
this.y = y;
}
}
package colin;
import java.awt.*;
import javax.swing.*;
class frame{
frame(){
JXFrame f = new JXFrame();
f.addMouseListener(new Mouseevent(f));
f.setSize(500,600);
f.setVisible(true);
}
public static void main(String args[]){
new frame();
}
}
package colin;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
public class Mouseevent implements MouseListener{
JXFrame parent;
public Mouseevnt(JXFrame parent){
this.parent = parent;
}
public void mousePressed(MouseEvent e){
}
public void mouseReleased(MouseEvent e){
this.parent.mouseCliked = false;
}
public void mouseEntered(MouseEvent e){
this.parent.mouseCliked = false;
}
public void mouseExited(MouseEvent e){
this.parent.mouseCliked = false;
}
public void mouseClicked(MouseEvent e){
this.parent.setPoint(e.getX(),e.getY());
this.parent.mouseCliked = true;
this.parent.repaint();
}
}
給你一個(gè)例子,太難講了
我自己寫的
package guidemo;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
/**
* pTitle: 圖形用戶界面/p
*
* pDescription: 簡單的圖形界面編程/p
*
* pCopyright: Copyright (c) 2006/p
*
* pCompany: /p
*
* @author vic
* @version 1.0
*/
public class ColorFrame extends Frame implements MouseListener {
Label L; //標(biāo)簽
TextField T; //文本域
Button B1, B2; //按鈕
public ColorFrame() {
this.setLayout(null); //想要手動指定各組件的的位置
L = new Label("輸入學(xué)號:"); //設(shè)定標(biāo)簽L內(nèi)容
L.setBounds(60, 50, 50, 25); //設(shè)定標(biāo)簽L外觀
this.add(L); //將標(biāo)簽L添加到窗口中
T = new TextField("請?jiān)谶@里輸入"); //設(shè)定文本域T的內(nèi)容
T.setBounds(125, 50, 90, 25); //設(shè)定文本域T的外觀
this.add(T); //將文本域T添加到窗口中
B1 = new Button("變紅!"); //設(shè)定按鈕B1的內(nèi)容
B1.setBounds(25, 90, 90, 25); //設(shè)定按鈕B1的外觀
B1.addMouseListener(this);//在B1上注冊鼠標(biāo)監(jiān)聽器
this.add(B1); //將按鈕B1添加到窗口中
B2 = new Button("變綠!");
B2.setBounds(125, 90, 90, 25);
B2.addMouseListener(this);
this.add(B2);
WindowDestroyer Listener = new WindowDestroyer(); //創(chuàng)建關(guān)閉窗口監(jiān)聽器
this.addWindowListener(Listener); //將監(jiān)聽器添加到窗口中
this.setBackground(Color.yellow); //設(shè)定窗口背景顏色
this.setTitle("This is Frame!"); //設(shè)定窗口標(biāo)題文字
this.setBounds(0, 0, 250, 220); //設(shè)定窗口位置和大小
this.setVisible(true); //顯示窗口
}
public void mouseClicked(MouseEvent e) {
if (e.getComponent() == B1) {//getComponent返回按鈕上面的字符串
this.setBackground(Color.red);
}
if (e.getComponent() == B2) {
this.setBackground(Color.green);
}
}
public void mouseExited(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public static void main(String[] args) {
new ColorFrame();
}
}
希望能解決您的問題。
使用組件的paint函數(shù)用于繪圖, 使用MouseListener來響應(yīng)鼠標(biāo)的點(diǎn)擊
效果圖
代碼
import?java.awt.Color;
import?java.awt.Graphics;
import?java.awt.event.*;
import?javax.swing.*;
public?class?DemoWin?extends?JFrame?{
public?DemoWin()?{
MyPanel?mp?=?new?MyPanel();
mp.addMouseListener(mp);
add(mp);
//?窗口屬性設(shè)置
setTitle("Demo");//?標(biāo)題
setSize(300,?280);//?窗口大小
setLocationRelativeTo(null);//?窗口居中
setDefaultCloseOperation(EXIT_ON_CLOSE);//?窗口點(diǎn)擊關(guān)閉時(shí),退出程序
}
public?static?void?main(String[]?args)?{
DemoWin?win?=?new?DemoWin();?//?創(chuàng)建窗口
win.setVisible(true);//?顯示窗口
}
class?MyPanel?extends?JPanel?implements?MouseListener?{
int?times;//?記錄點(diǎn)擊的次數(shù)
int?x;//?記錄鼠標(biāo)X軸的位置
int?y;//?記錄鼠標(biāo)Y軸的位置
@Override
public?void?paint(Graphics?g)?{
super.paint(g);
if?(times?==?0)?{
g.setColor(Color.BLUE);//?顏色
g.fillOval(150,?150,?50,?50);//?150,150代表位置?50,50代表寬高
}?else?if?(times?==?1)?{
g.setColor(Color.RED);
g.fillRect(150,?150,?50,?50);
}?else?{
g.setColor(Color.RED);
g.fillRect(x,?y,?50,?50);
}
repaint();
}
public?void?mouseClicked(MouseEvent?e)?{
//?if?(e.getButton()==MouseEvent.BUTTON1)?{//單擊左鍵時(shí)有效..
//?times++;//?記錄點(diǎn)擊的次數(shù)
//?x?=?e.getX();
//?y?=?e.getY();
//?}
}
public?void?mousePressed(MouseEvent?e)?{//?鼠標(biāo)按下就有效
times++;//?記錄點(diǎn)擊的次數(shù)
x?=?e.getX();
y?=?e.getY();
}
public?void?mouseReleased(MouseEvent?e)?{//?鼠標(biāo)釋放
}
public?void?mouseEntered(MouseEvent?e)?{//?鼠標(biāo)移入
}
public?void?mouseExited(MouseEvent?e)?{//?鼠標(biāo)移出
}
}
}
this.addMouseListener(new
MouseAdapter()
{
@Override
public
void
mousePressed(MouseEvent
e)
{
x
=
e.getX();
y
=
e.getY();
System.out.println("Xbefore:"+e.getX());
}
});
this.addMouseMotionListener(new
MouseMotionListener()
{
public
void
mouseDragged(MouseEvent
e)
{
JButton
btn
=
(JButton)
e.getSource();
setLocation(btn.getX()
+
e.getX()
-
x,
btn.getY()
+
e.getY()
-
y);
}
public
void
mouseMoved(MouseEvent
e)
{
}
});
上面的代碼是給JButton添加的鼠標(biāo)事件,移動的是按鈕。。。
public?class?MouseClickEvent?extends?JFrame{
public?MouseClickEvent()?{
Container?container?=?getContentPane();
container.addMouseListener(new?MouseListener()?{
@Override
public?void?mouseReleased(MouseEvent?arg0)?{
int?buttonFlag?=?arg0.getButton();
switch?(buttonFlag)?{
case?MouseEvent.BUTTON1:
System.out.println("釋放的是鼠標(biāo)左鍵!\n");
break;
case?MouseEvent.BUTTON2:
System.out.println("釋放的是鼠標(biāo)滾輪!\n");
break;
case?MouseEvent.BUTTON3:
System.out.println("釋放的是鼠標(biāo)右鍵!\n");
break;
}? ?
}
@Override
public?void?mousePressed(MouseEvent?arg0)?{
int?buttonFlag?=?arg0.getButton();
switch?(buttonFlag)?{
case?MouseEvent.BUTTON1:
System.out.println("按下的是鼠標(biāo)左鍵!\n");
break;
case?MouseEvent.BUTTON2:
System.out.println("按下的是鼠標(biāo)滾輪!\n");
break;
case?MouseEvent.BUTTON3:
System.out.println("按下的是鼠標(biāo)右鍵!\n");
break;
}
}
@Override
public?void?mouseExited(MouseEvent?arg0)?{
System.out.println("光標(biāo)移出應(yīng)用程序窗口了!\n");? ?
}
@Override
public?void?mouseEntered(MouseEvent?arg0)?{
System.out.println("光標(biāo)進(jìn)入應(yīng)用程序窗口了!\n");?
}
@Override
public?void?mouseClicked(MouseEvent?arg0)?{
int?buttonFlag?=?arg0.getButton();
switch?(buttonFlag)?{
case?MouseEvent.BUTTON1:
System.out.println("點(diǎn)擊的是鼠標(biāo)左鍵!\n");
break;
case?MouseEvent.BUTTON2:
System.out.println("點(diǎn)擊的是鼠標(biāo)滾輪!\n");
break;
case?MouseEvent.BUTTON3:
System.out.println("點(diǎn)擊的是鼠標(biāo)右鍵!\n");
break;
}
int?clickCount?=?arg0.getClickCount();
System.out.println("點(diǎn)擊次數(shù)為"+clickCount+"\n");
}
});
}
public?static?void?main(String[]?args)?{
MouseClickEvent?testFrame?=?new?MouseClickEvent();
testFrame.setTitle("鼠標(biāo)點(diǎn)擊事件測試程序!");
testFrame.setVisible(true);
testFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
testFrame.setBounds(0,?0,?500,?400);
}
}
新聞名稱:java鼠標(biāo)點(diǎn)擊事件代碼 java鼠標(biāo)點(diǎn)擊事件怎么寫
網(wǎng)頁URL:http://aaarwkj.com/article22/doodjcc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供搜索引擎優(yōu)化、、動態(tài)網(wǎng)站、面包屑導(dǎo)航、微信小程序、建站公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)