简体   繁体   English

MouseListener-repaint()不起作用

[英]MouseListener - repaint() doesn't work

I'm trying to make a little board game in Java, the following is a little excerpt of it, just to show what my problem is. 我正在尝试用Java做一些棋盘游戏,以下是其中的一些摘录,目的只是为了说明我的问题。

I've got a JPanel with an array of JLabels to visualize a board. 我有一个带有一系列JLabel的JPanel来可视化一个板。 Every Label has its own MouseListener. 每个标签都有自己的MouseListener。 In the beginning, all Labels are red, if I click any of them, a I'd like all of them to turn green. 一开始,所有标签都是红色的,如果我单击其中的任何一个,我希望它们全部变为绿色。

I know it can be easily done using setBackground for every Label, but I'd like that to be done at the level of Panel, because it will be probably connected with simple board of ex chars, on which the whole game would develop, and Labels would be just visualizing that. 我知道可以为每个Label使用setBackground轻松完成此操作,但我希望在Panel级别完成此操作,因为它可能与简单的ex char板连接,整个游戏将在此板上开发,并且标签将只是可视化。

I have no idea, what I'm doing wrong. 我不知道我在做什么错。 I guess I haven't thought it well, but I don't know how to do that. 我想我想得还不错,但是我不知道该怎么做。 Could anyone please help me? 谁能帮我吗?

Please don't suggest the paintComponent overriden, it's just effect of my desperation and it doesn't work of course. 请不要建议将paintComponent重写,这只是我绝望的结果,它当然不起作用。

Main class: 主班:

import java.awt.EventQueue;

public class Main {
    public static void main( String args[] ) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Window();

            }
        });
    }
}

Window class: 窗口类:

import java.awt.FlowLayout;
import java.awt.Dimension;
import javax.swing.JFrame;

public class Window extends JFrame {

    public Window() {
        setMinimumSize( new Dimension(500, 600) );
        setLocationRelativeTo( null );
        setDefaultCloseOperation( EXIT_ON_CLOSE );
        setLayout( new FlowLayout() );

        PanelBoard b = new PanelBoard();
        add( b );
        setVisible( true );
    }
}

Panel class: 小组课:

import javax.swing.JPanel;
import java.awt.FlowLayout;
import java.awt.Dimension;
import java.awt.Color;

public class PanelBoard extends JPanel {

    private Field fields[][] = new Field[6][6];

    public PanelBoard() {
        setLayout( new FlowLayout( FlowLayout.CENTER, 0, 0) );
        setPreferredSize( new Dimension( 60*6, 60*6 ) );

        addFields( Color.red );

    }

    public void addFields( Color c ) {
        for( int i=0; i<6; i++)
            for( int j=0; j<6; j++ ) {
                fields[i][j] = new Field( this, c );
                add( fields[i][j] );
            }
    }
}

Field class: 领域课:

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import java.awt.Color;
import java.awt.Dimension;

public class Field extends JLabel {

    public Field( PanelBoard pb, Color c) {
        setBackground( c );
        setBorder( BorderFactory.createLineBorder( Color.black ) );
        setOpaque( true );
        setPreferredSize( new Dimension( 60, 60 ) );

        addMouseListener( new Mouse( pb, this ) );
    }
}

MouseListener class: MouseListener类:

import java.awt.Color;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.BorderFactory;

public class Mouse implements MouseListener {

    PanelBoard pb;
    Field f;

    public Mouse( PanelBoard pb, Field f ) {
        this.pb = pb;
        this.f = f;
    }

    @Override
    public void mouseReleased( MouseEvent arg ) {
        pb.addFields( Color.green );
        pb.revalidate();
        pb.repaint();

    }

    @Override
    public void mouseClicked( MouseEvent arg ) {}
    @Override
    public void mousePressed( MouseEvent arg ) {}
    @Override
    public void mouseExited( MouseEvent arg ) {}
    @Override
    public void mouseEntered( MouseEvent arg ) {}
}

Just add this method to PanelBoard : 只需将此方法添加到PanelBoard

public void modifyFields( Color c ) {
    for( int i=0; i<6; i++)
        for( int j=0; j<6; j++ ) {
          fields[i][j].setBackground(c);
        }
}

and modify mouseReleased method of class Mouse like this: 并修改Mouse类的mouseReleased方法,如下所示:

public void mouseReleased( MouseEvent arg ) {
    pb.modifyFields( Color.green );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM