1

我正在尝试获取鼠标位置并在光标位于按钮上方时点亮按钮。但是鼠标位置没有正确更新。我认为 y 位置搞砸了,因为目前如果我在按钮上方很远,它就会亮起。

这是我不正确的代码:

public void mouseMoved(MouseEvent e) {
    Window.mse = new Point((e.getX()) + ((Frame.size.width - Window.myWidth)/2), (e.getY()) + (Frame.size.height - (Window.myHeight)/2));
}

这是窗口文件:

import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
import java.io.*;

public class Window extends JPanel implements Runnable {

    public Thread thread = new Thread(this);

    public static Image[] tileset_ground = new Image[100];
    public static Image[] tileset_air = new Image[100];
    public static Image[] tileset_resources = new Image[100];

    public static int myWidth, myHeight;

    public static boolean isFirst = true;

    public static Point mse = new Point(0, 0);

    public static Room room;
    public static Levels levels;
    public static Shop shop;

    public Window(Frame frame) {
        frame.addMouseListener(new KeyHandler());
        frame.addMouseMotionListener(new KeyHandler());

        thread.start();
    }

    public void define() {
        room = new Room();
        levels = new Levels();
        shop = new Shop();

        tileset_resources[0] = new ImageIcon("resources/cell.png").getImage();

        levels.loadLevels(new File("levels/level1.level"));

        for(int i=0;i<tileset_ground.length; i++) {
            tileset_ground[i] = new ImageIcon("resources/tileset_ground.png").getImage();
            tileset_ground[i] = createImage(new FilteredImageSource(tileset_ground[i].getSource(), new CropImageFilter(0, 32 * i, 32, 32)));
        }

        for(int i=0;i<tileset_air.length; i++) {
            tileset_air[i] = new ImageIcon("resources/tileset_air.png").getImage();
            tileset_air[i] = createImage(new FilteredImageSource(tileset_air[i].getSource(), new CropImageFilter(0, 32 * i, 32, 32)));
        }

    }

    public void paintComponent(Graphics g) {
        if(isFirst) {

            myWidth = getWidth();
            myHeight = getHeight();

            define();

            isFirst = false;
        }

        g.setColor(new Color(70, 70, 70));
        g.fillRect(0, 0, myWidth, myHeight);
        g.setColor(new Color(0, 0, 0));
        g.drawLine(room.block[0][0].x, room.block[room.worldHeight - 1][0].y + room.blockSize, room.block[0][room.worldWidth - 1].x + room.blockSize, room.block[room.worldHeight - 1][0].y + room.blockSize);
        g.drawLine(room.block[0][0].x, room.block[room.worldHeight - 1][0].y + 1 + room.blockSize, room.block[0][room.worldWidth - 1].x + room.blockSize, room.block[room.worldHeight - 1][0].y + 1 + room.blockSize);


        room.draw(g); //Draws room
        shop.draw(g); //Draws shop
    }

    public void run() { 
        while(true) {

            if(!isFirst) {
                room.physic();
            }

            repaint();

            try {
                Thread.sleep(1);
            } catch(Exception e) {
                e.printStackTrace();
            }

        }

    }

}
4

1 回答 1

1

只需将 MouseListener 添加到 JButton。或者更好的做法是在按钮的模型中添加一个 ChangeListener,并在其中调用isRollover()模型。

于 2012-05-13T06:10:26.760 回答