我的代码:

package a2048;

import javax.swing.JFrame;

public class GameWindow extends JFrame {

    public GameWindow() {

        add(new Main());

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(409, 435);
        setLocationRelativeTo(null);
        setTitle("2048");
        setResizable(true);
        setVisible(true);

    }

    public static void main(String[] args) {
        new GameWindow();
    }
}


那是一类,另一类:

    package a2048;

import java.util.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;

public class Main extends JPanel implements MouseListener
{
    int randx, randy, quit;
    int a1, a2, a3, a4, b1, b2, b3 ,b4, c1, c2, c3, c4, d1, d2, d3, d4;

    int GetRandom(int min, int max)
    {
       int range = (max - min) + 1;
       double rand = (Math.random() * range) + min;
       return (int) rand;

    }

    public Main()
    {
        setBackground(Color.BLACK);
        setDoubleBuffered(true);
    }

    public void paint(Graphics g)
    {
        quit = 0;

        a1 = a2 = a3 = a4 = b1 = b2 = b3 = b4 = c1 = c2 = c3 = c4 = d1 = d2 = d3 = d4 = 0;

        super.paint(g);

        Graphics2D g2d = (Graphics2D)g;

        g2d.setColor(Color.white);
        g2d.drawLine(0, 0, 500, 0);
        g2d.drawLine(0, 100, 500, 100);
        g2d.drawLine(0, 200, 500, 200);
        g2d.drawLine(0, 300, 500, 300);
        g2d.drawLine(0, 400, 500, 400);

        g2d.drawLine(0, 0, 0, 500);
        g2d.drawLine(100, 0, 100, 500);
        g2d.drawLine(200, 0, 200, 500);
        g2d.drawLine(300, 0, 300, 500);
        g2d.drawLine(400, 0, 400, 500);

        Font myFont = new Font("TimesRoman", Font.PLAIN, 75);
        g2d.setFont(myFont);

        while(quit == 0)
        {
            randx = GetRandom(1, 4);
            randy = GetRandom(1, 4);

            if(randx == 1)
            {
                if(randy == 1 && a1 == 0)
                {
                    a1 = 2;
                }
                else if(randy == 2 && a2 == 0)
                {
                    a2 = 2;
                }
                else if(randy == 3 && a3 == 0)
                {
                    a3 = 2;
                }
                else if(randy == 4 && a4 == 0)
                {
                    a4 = 2;
                }
            }

            else if(randx == 2)
            {
                if(randy == 1 && b1 == 0)
                {
                    b1 = 2;
                }
                else if(randy == 2 && b2 == 0)
                {
                    b2 = 2;
                }
                else if(randy == 3 && b3 == 0)
                {
                    b3 = 2;
                }
                else if(randy == 4 && b4 == 0)
                {
                    b4 = 2;
                }
            }

            else if(randx == 3)
            {
                if(randy == 1 && c1 == 0)
                {
                    c1 = 2;
                }
                else if(randy == 2 && c2 == 0)
                {
                    c2 = 2;
                }
                else if(randy == 3 && c3 == 0)
                {
                    c3 = 2;
                }
                else if(randy == 4 && c4 == 0)
                {
                    c4 = 2;
                }
            }

            else if(randx == 4)
            {
                if(randy == 1 && d1 == 0 && a1 == 0)
                {
                    d1 = 2;
                }
                else if(randy == 2 && d2 == 0 && a1 == 0)
                {
                    d2 = 2;
                }
                else if(randy == 3 && d3 == 0 && a1 == 0)
                {
                    d3 = 2;
                }
                else if(randy == 4 && d4 == 0 && a1 == 0)
                {
                    d4 = 2;
                }
            }

            if(a1 > 0)
                g2d.drawString(String.valueOf(a1), (randx * 100) - 70, (randy * 100) - 25);
            if(a2 > 0)
                g2d.drawString(String.valueOf(a2), (randx * 100) - 70, (randy * 100) - 25);
            if(a3 > 0)
                g2d.drawString(String.valueOf(a3), (randx * 100) - 70, (randy * 100) - 25);
            if(a4 > 0)
                g2d.drawString(String.valueOf(a4), (randx * 100) - 70, (randy * 100) - 25);
            if(b1> 0)
                g2d.drawString(String.valueOf(b1), (randx * 100) - 70, (randy * 100) - 25);
            if(b2 > 0)
                g2d.drawString(String.valueOf(b2), (randx * 100) - 70, (randy * 100) - 25);
            if(b3 > 0)
                g2d.drawString(String.valueOf(b3), (randx * 100) - 70, (randy * 100) - 25);
            if(b4 > 0)
                g2d.drawString(String.valueOf(b4), (randx * 100) - 70, (randy * 100) - 25);
            if(c1 > 0)
                g2d.drawString(String.valueOf(c1), (randx * 100) - 70, (randy * 100) - 25);
            if(c2 > 0)
                g2d.drawString(String.valueOf(c2), (randx * 100) - 70, (randy * 100) - 25);
            if(c3 > 0)
                g2d.drawString(String.valueOf(c3), (randx * 100) - 70, (randy * 100) - 25);
            if(c4 > 0)
                g2d.drawString(String.valueOf(c4), (randx * 100) - 70, (randy * 100) - 25);
            if(d1 > 0)
                g2d.drawString(String.valueOf(d1), (randx * 100) - 70, (randy * 100) - 25);
            if(d2 > 0)
                g2d.drawString(String.valueOf(d2), (randx * 100) - 70, (randy * 100) - 25);
            if(d3 > 0)
                g2d.drawString(String.valueOf(d3), (randx * 100) - 70, (randy * 100) - 25);
            if(d4 > 0)
                g2d.drawString(String.valueOf(d4), (randx * 100) - 70, (randy * 100) - 25);

            quit = 1;
        }

        Toolkit.getDefaultToolkit().sync();
        g.dispose();
    }

    public void mouseExited(MouseEvent e)
    {
        repaint();
    }

    public void mouseReleased(MouseEvent e)
    {
        repaint();
    }

    public void mousePressed(MouseEvent e)
    {
        repaint();
    }

    public void mouseEntered(MouseEvent e)
    {
        repaint();
    }

    public void mouseClicked(MouseEvent e)
    {
        a1 = 5;
        repaint();
    }
}


我不明白为什么单击窗口时mouseClicked方法没有执行任何操作。我要执行的操作是将a1设置为5,然后在屏幕上打印出来,但实际上它什么也没做。我做了什么更改才能使MouseListener正常工作?

最佳答案

MouseListener需要注册以监听的内容才能执行任何操作。

每当您在某物上单击鼠标时,它将生成一个MouseEvent并在正在监听它的任何东西上调用方法,并将该事件作为参数传递。

您似乎正在将侦听器实现为JPanel的一部分,因此,除了将其添加为GameWindow中的组件之外,还需要在窗口上将其注册为鼠标侦听器。

更换

add(new Main());




Main main = new Main();
addMouseListener(main);
add(main);




编辑

由HovercraftFullOfEels建议的一种更清洁的方法是让
Main JPanel侦听自身的鼠标事件。为此,您可以在Main的构造函数中调用addMouseListener()而不是在封闭的JFrame中调用:

public Main() {
    addMouseListener(this);
    setBackground(Color.BLACK);
    setDoubleBuffered(true);
}

07-26 04:07