老鼠听众不工作

时间:2014-05-08 20:04:12

标签: java swing jpanel paint mouselistener

我的代码:

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正常工作?

1 个答案:

答案 0 :(得分:2)

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

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

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

替换

add(new Main());

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

编辑

HovercraftFullOfEels建议的更清洁的方法就是拥有 主要 JPanel自行侦听鼠标事件。为此,您可以在Main的构造函数中调用addMouseListener(),而不是在封闭的JFrame中调用:

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