MouseMotionListener不起作用

时间:2017-01-07 12:20:23

标签: java swing user-interface mousemotionlistener

我正在编写一款名为“Kulami”的游戏。这是一个两个玩家的棋盘游戏。棋盘是用不同大小的矩形板构建的。我可以签署我需要的所有印版,但我无法移动它们,MouseMotionListener不起作用。矩形板已签名,但没有接触按鼠标。我找不到错误。任何人都可以在我的代码上显示并帮助我吗? 谢谢!

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import Model.EFieldStateMarble;
import Model.EFieldStatePlate;


public class Plate extends JPanel {

    // Eigenschaften
    public String plate[][];

    // Konstruktor
    public Plate(int r, int c, EFieldStatePlate fieldStatePlate, EFieldStateMarble fieldStateMarble) {
        plate = new String[r][c];
        for (r = 0; r < plate.length; r++) { // waagerecht
            for (c = 0; c < plate[r].length; c++) { // senkrecht
                plate[r][c] = fieldStatePlate.toString() + fieldStateMarble.toString(); // Codierung Feld
            }

        }
    }

    // Methoden
    public void showPlate() { // Konsolenausgabe Spielfeldplatte
        for (int r = 0; r < plate.length; r++) {
            for (int c = 0; c < plate[r].length; c++) {
                System.out.print(plate[r][c]);
            }
            System.out.println();
        }
    }

    // Ändert Eintrage in r-ter Reihe und c-ter Spalte
    public void setEntryAt(int r, int c, EFieldStatePlate fieldStatePlate, EFieldStateMarble fieldStateMarble) {
        plate[r][c] = fieldStatePlate.toString() + fieldStateMarble.toString();
    }

    public String[][] getPlate() {
        return plate;
    }

    // dreht das Array um 90°
    public void turnPlate() {
        int m = plate.length;
        int n = plate[0].length;
        String[][] returnPlate = new String[n][m];
        for (int r = 0; r < plate.length; r++) {
            for (int c = 0; c < plate[0].length; c++) {
                returnPlate[c][m - 1 - r] = plate[r][c];
            }
        }plate = returnPlate;
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        drawPlate(g2d, 60, 60);

    }

    private void drawPlateField(Graphics2D g2d, int x, int y) {
        int posX = x;
        int posY = y;
        g2d.setColor(Color.BLACK);
        g2d.drawRect(posX, posY, 60, 60);
        g2d.setColor(new Color(242, 203, 130));
        g2d.fillRect(posX + 1, posY + 1, 59, 59);
        g2d.setColor(Color.BLACK);
        g2d.drawOval(x + 15, y + 15, 30, 30);
        g2d.setColor(new Color(242, 203, 130));
        g2d.fillOval(x + 16, y + 16, 29, 29);
    }

    private void drawPlate(Graphics g2d, int plateX, int plateY ) {

        try {
            for (int r = 0; r < plate.length; r++) {
                for (int c = 0; c < plate[0].length; c++) {
                    drawPlateField((Graphics2D) g2d, plateX + c * 60, plateY + r * 60);
                }

            }

        } catch (Exception e) {
            System.out.print(e);
            }

    }

    private class PlateDragged implements MouseListener, MouseMotionListener {

        //Plattengröße
        int platesizeX = plate.length;
        int platesizeY = plate[0].length;

        //Plattenkoordinaten
        int plateX = 60;
        int plateY = 60;

        //Position, von der die Maus die Platte zieht
        int dragFromX = 0;
        int dragFromY = 0;

        //bei "true" kann die Platte nach Mausklick gezogen werden
        boolean candrag = false;

        public void mousePressed(MouseEvent mmp) {

            new Thread() {
                @Override
                public void run() {
                    int x = mmp.getX();
                    int y = mmp.getY();

                    if (x >= plateX && x <= (plateX + platesizeY) && y >= plateY && y <= (plateY + platesizeX)) {

                        candrag = true;
                        dragFromX = x - plateX;
                        dragFromY = y - plateY;
                    }
                    else {
                        candrag = false;
                    }
                    System.out.println("Hallo");
                }
            }.start();

        }

        @Override
        public void mouseDragged(MouseEvent mmd) {

            new Thread() {
                @Override
                public void run() {
                    if (candrag) {


                        //Plattenposition wechseln
                        plateX = mmd.getX() - dragFromX;
                        plateY = mmd.getY() - dragFromY;

                        //Entferne die Platte nicht aus dem Fenster
                        plateX = Math.max(plateX, 0);
                        plateX = Math.min(plateX, getWidth() - platesizeY);

                        plateY = Math.max(plateY, 0);
                        plateY = Math.min(plateY, getHeight() - platesizeX);
                        repaint();
                    }
                }
            }.start();

        }

        public void mouseExited(MouseEvent mme) {

            candrag = false;

        }

        @Override
        public void mouseMoved(MouseEvent arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void mouseClicked(MouseEvent arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void mouseEntered(MouseEvent arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void mouseReleased(MouseEvent arg0) {
            // TODO Auto-generated method stub

        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

//          Plate plate = new Plate();
            @Override
            public void run() {
                JFrame f = new JFrame();
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);



                Plate g2d = new Plate(2, 3, EFieldStatePlate.B6, EFieldStateMarble.EMPTY);
                g2d.setPreferredSize(new Dimension(650, 650));

                f.add(g2d);
                f.pack();
                f.setLocation(200, 800);
                f.setVisible(true);

            }


        });

    }
}

1 个答案:

答案 0 :(得分:0)

您的代码中的任何位置都无法调用addMouseListener(...)addMouseMotionListener(...)。您需要向Swing组件添加一个侦听器才能使侦听器工作 - 换句话说,侦听器必须侦听某些内容。如,

// Konstruktor
public Plate(int r, int c, EFieldStatePlate fieldStatePlate,
        EFieldStateMarble fieldStateMarble) {
    plate = new String[r][c];
    for (r = 0; r < plate.length; r++) { 
        for (c = 0; c < plate[r].length; c++) { 
            plate[r][c] = fieldStatePlate.toString() + fieldStateMarble.toString(); 
        }
    }

    // ***** added *****
    PlateDragged plateDragged = new PlateDragged();
    addMouseListener(plateDragged);
    addMouseMotionListener(plateDragged);
}

坦率地提出这种性质的问题,关于MouseListener使用的基本问题表明你通过猜测而不是先学习本教程来编码。为了你自己,不要这样做。请查看MouseListener Tutorial

其他问题:为什么要从Swing事件线程创建一个线程并从中调用代码?