java監聽滑鼠
Ⅰ java 滑鼠監聽器
JMenuItem againItem = startFile.add("再玩一次");//建立子菜單
JMenuItem heroItem = startFile.add("英雄榜");
JMenuItem howItem = helpFile.add("如何游戲");
JMenuItem writerItem = aboutFile.add("製作組成員");
JMenuItem callItem = aboutFile.add("聯系方式");
againItem.addActionListener(new ActionHandler());//建立滑鼠監聽器
}
}
class ActionHandler implements ActionListener{
public void actionPerformed(ActionEvent event){
JOptionPane.showMessageDialog(null,"Hello world!","Display",JOptionPane.INFORMATION_MESSAGE);
}
}
或者:
JMenuItem againItem = startFile.add("再玩一次");//建立子菜單
JMenuItem heroItem = startFile.add("英雄榜");
JMenuItem howItem = helpFile.add("如何游戲");
JMenuItem writerItem = aboutFile.add("製作組成員");
JMenuItem callItem = aboutFile.add("聯系方式");
againItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
JOptionPane.showMessageDialog(null,"Hello world!","Display",JOptionPane.INFORMATION_MESSAGE);
}
}
);//建立滑鼠監聽器
}
}
Ⅱ java中滑鼠監聽器的使用
import java.util.Date;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class MyTime extends JFrame {//刪除了集成的介面
JFrame frame = new JFrame();
JLabel label = null;
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JButton btn1 = new JButton(" 紅 色 ");
JButton btn2 = new JButton(" 綠 色 ");
JButton btn3 = new JButton(" 藍 色 ");
public MyTime() {
panel1.setBackground(Color.white);
panel2.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 10));
panel1.setBackground(Color.WHITE);
panel2.add(btn1);
panel2.add(btn2);
panel2.add(btn3);
add(panel1, BorderLayout.CENTER);
add(panel2, BorderLayout.SOUTH);// 創建一個監聽器對象
btn1.addActionListener(new TimeThread(this).new AL()); // 為按鈕注冊監聽器(使用了TimeThread類的AL內部類)
btn2.addActionListener(new TimeThread(this).new AL());
btn3.addActionListener(new TimeThread(this).new AL());
frame = new JFrame();
label = new JLabel();
label.setIcon(new ImageIcon("QQ拼音截圖未命名.jpg"));
label.setFont(new Font("幼圓", Font.LAYOUT_NO_LIMIT_CONTEXT, 76));
label.setForeground(Color.BLACK);
add(label);
setTitle("個性數字時鍾");
setResizable(false);// 鎖定窗口大小
setSize(320, 160);
setLocation(300, 300);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
}
MyTime mt = new MyTime();
new TimeThread(mt).start();
}
}
class TimeThread extends Thread {
private MyTime mt;
public TimeThread(MyTime mt) {
this.mt = mt;
}
public void run() {
while (true) {
String fullTime = new Date().toString();
String time = fullTime.substring(11, 19);
mt.label.setText(time);
mt.label.repaint();
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
class AL implements ActionListener { // 內部類
public void actionPerformed(ActionEvent e) {
if ((JButton) e.getSource() == mt.btn1)//btn1屬於MyTime類
mt.label.setForeground(Color.RED);
else if ((JButton) e.getSource() == mt.btn2)
mt.label.setForeground(Color.GREEN);
else if ((JButton) e.getSource() == mt.btn3)
mt.label.setForeground(Color.BLUE);
}
}
}
//修改的地方已經注釋
Ⅲ java監聽滑鼠事件的問題
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class test extends JFrame implements Runnable{
public static int x,y,direction=0;
public test(){
this.setSize(600,400);
this.setVisible(true);
x=this.getContentPane().getWidth()/2;
y=this.getContentPane().getHeight()/2;
this.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e){
switch(e.getKeyCode()){
case KeyEvent.VK_DOWN:
direction=1;break;
case KeyEvent.VK_LEFT:
direction=2;break;
case KeyEvent.VK_RIGHT:
direction=3;break;
case KeyEvent.VK_UP:
direction=4;break;
}
}
});
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String args[]){
test t=new test();
new Thread(t).start();
}
public void paint(Graphics g){
g.setColor(Color.white);
g.fillRect(0,0,this.getWidth(),this.getHeight());
g.setColor(Color.red);
g.drawRect(x-60,y-40,120,80);
}
public void run(){
while(true){
try{
Thread.sleep(100);
}catch(Exception e){}
switch(direction){
case 1: y =4;break;
case 2: x-=4;break;
case 3: x =4;break;
case 4: y-=4;break;
}
this.repaint();
}
}
}
Ⅳ Java中關於滑鼠監聽器的一點問題
1、我理解不存在拖動這一說,不管是點擊還是使用鍵盤,都是圖片坐標的問題。
2、宮格數是二維數組,我們初始化記錄空白塊的坐標,此時監聽滑鼠的活動事件,判斷目標位置坐標與空白塊坐標有相鄰一組的重合即運動塊的位置是合法的,然後我們交換空白塊與運動塊的坐標就可以實現所謂的「拖動」。
3、代碼
參考
Ⅳ Java中要監聽滑鼠事件,則實現監聽器類可以是使用的方式有哪幾種
,MouseWheelListener,MouseMotionListener
如上所示,監聽滑鼠事件只要使用MouseAdapter類就行了
Ⅵ java滑鼠監聽器問題
實現滑鼠監聽借口,並給相應的控制項進行監聽,然後滑鼠keyUp方法里進行f.setVisible(false);
Ⅶ Java 滑鼠監聽事件 mouseMoved(MouseEvent)
不需要實現MouseMotionListener介面,你已經用了addMouseMotionListener方法
MouseAdapter類已經是實現了MouseMotionListener介面的。
改成
可以運行成功
Ⅷ java 滑鼠監聽
//這是應用程序,跟java小程序的大體思路還是差不多的,你改改
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.Border;
/**
*建立個界面,可以載入本機中圖片。
*載入後可以通過滑鼠點擊獲得圖片上任意點坐標。
*/
public class MyPicture extends JFrame implements MouseListener{
private JLabel tipLabel;
/**
*main()
*/
public static void main(String[] args){
MyPicture frame = new MyPicture();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
/**
*constructor
*/
public MyPicture(){
setSize(800, 600);//根據要求調整大小
setLocation(100,100);
setTitle("獲得圖片上任意點坐標");
setResizable(false);
Container con=getContentPane();
ImageIcon bgIcon=new ImageIcon("bgpic.jpg");//注意圖片的路徑
ImagePanel backpicPanel=new ImagePanel(bgIcon);
backpicPanel.addMouseListener(this);
con.add(backpicPanel,BorderLayout.CENTER);
tipLabel=new JLabel("--------------------提示:坐標依次列印在屏幕上!--------------------");
con.add(tipLabel,BorderLayout.NORTH);
}
/**
*
*/
public void mousePressed(MouseEvent e){
int x=e.getX();
int y=e.getY();
String message="("+x+","+y+")";
tipLabel.setText(message);
System.out.println(message);
}
public void mouseReleased(MouseEvent e){
}
public void mouseEntered(MouseEvent e){
}
public void mouseExited(MouseEvent e){
}
public void mouseClicked(MouseEvent e){
}
}
/**
*類ImagePanel,用於添加背景圖片
*/
class ImagePanel extends JPanel{
private Image img;
public ImagePanel (ImageIcon imageIcon){
img=imageIcon.getImage();
}
public void paintComponent(Graphics g){
super.paintComponent(g);
g.drawImage(img,0,0,this);
}
}
Ⅸ 關於JAVA中的滑鼠監聽
this.addMouseListener(this);意味著本身就是一個MouseListener的子類,如果把this換成其他MouseListener的實現就不需要繼承了。
Ⅹ JAVA滑鼠監聽
樓主,你沒有將number定義成靜態成員吧。沒有將它定義為靜態成員,結果當然是零。