1

我有以下内容:

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JLayeredPane;
import javax.swing.JFrame;
import javax.swing.BorderFactory;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import javax.swing.ImageIcon;
import java.awt.GridLayout;
import java.awt.Dimension;
import java.awt.Color;
import java.util.Random;

class Cell extends JLayeredPane
{
    private JLabel image1;
    private JLabel image2;
    private JLabel image3;

    private Random rand;

    public static int CELLHEIGHT = 22;
    public static int CELLWIDTH = 22;

    public Cell ()
    {
        setPreferredSize (new Dimension (CELLWIDTH, CELLHEIGHT));
        setOpaque (true);
        setBackground (Color.LIGHT_GRAY);
        setBorder (BorderFactory.createLineBorder (Color.BLACK, 1));
        setBounds (0, 0, CELLWIDTH, CELLHEIGHT);

        image1 = new JLabel (new ImageIcon (getClass ().getResource ("image1.png")));   //size is 20 x 20 pixels
        image2 = new JLabel (new ImageIcon (getClass ().getResource ("image2.jpg")));   //size is 20 x 20 pixels
        image3 = new JLabel (new ImageIcon (getClass ().getResource ("image3.jpg")));   //size is 20 x 20 pixels

        image1.setBounds (0, 0, 20, 20);
        image2.setBounds (0, 0, 20, 20);
        image3.setBounds (0, 0, 20, 20);

        add (image1, new Integer (0));
        add (image2, new Integer (1));
        add (image3, new Integer (2));
    }

    public void updateLayers ()
    {
        removeAll ();   //method from JLayeredPane

        add (image1, new Integer (2));
        add (image2, new Integer (1));
        add (image3, new Integer (0));

        repaint ();
    }
}

class MyPanel extends JPanel
{
    private Cell[][] cells;

    public MyPanel (int cellcount_rows, int cellcount_columns)
    {
        super ();

        setLayout (new GridLayout (cellcount_rows, cellcount_columns, 0, 0));

        cells = new Cell[cellcount_rows][cellcount_columns];    //results in about 500 Cell objects

        for (int i = 0; i < cellcount_rows; i++)
        {
            for (int j = 0; j < cellcount_columns; j++)
            {
                cells[i][j] = new Cell ();

                add (cells[i][j]);
            }
        }
    }
}

class MouseHandler implements MouseListener
{
    private MyPanel panel;

    public MouseHandler (MyPanel panel)
    {
        this.panel = panel;
    }

    public void mouseClicked (MouseEvent e)
    {
        Cell cell = (Cell) panel.getComponentAt (e.getX (), e.getY ());

        if (e.getButton () == MouseEvent.BUTTON1)
        {//some very fast (and irrelevant) cell modification goes here
            cell.updateLayers ();
        }
        else if (e.getButton () == MouseEvent.BUTTON3)
        {//some very fast (and irrelevant) cell modification goes here
            cell.updateLayers ();
        }
    }

    public void mouseEntered (MouseEvent e) { }
    public void mouseExited (MouseEvent e) { }
    public void mousePressed (MouseEvent e) { }
    public void mouseReleased (MouseEvent e) { }
}

public class GUI
{
    private JFrame mainframe;
    private MyPanel panel;

    private static final int ROWS = 20;
    private static final int COLS = 25;

    public GUI ()
    {
        mainframe = new JFrame ();
        mainframe.setSize (Cell.CELLWIDTH * COLS + 100, Cell.CELLHEIGHT * ROWS + 100);
        mainframe.setResizable (false);
        mainframe.setDefaultCloseOperation (JFrame.DISPOSE_ON_CLOSE);
        panel = new MyPanel (ROWS, COLS);

        panel.addMouseListener (new MouseHandler (panel));

        mainframe.setLayout (null);
        panel.setBounds (20, 20, Cell.CELLWIDTH * COLS, Cell.CELLHEIGHT * ROWS);

        mainframe.add (panel);

        mainframe.setVisible (true);    
    }

    public static void main (String[] args)
    {
        javax.swing.SwingUtilities.invokeLater (new Runnable ()
        {
            public void run ()
            {
                GUI t = new GUI ();
            }
        });
    }
}

所以基本上我有一个 JPanel 的子类,其中包含大约 500 个 Cell 类型的对象(它是 JLayeredPane 的子类)。

基本上,每次用户单击其中一个组件时,该组件都会重新组织其图层并重新绘制自己。

问题是它有点慢,我不知道为什么。在大约 50% 的情况下,用户必须多次单击才能使其工作。

也许重绘有问题,或者在 50% 的情况下 getComponentAt 失败。我不知道......而且我不知道如何解决它......请帮忙。

4

1 回答 1

1

如果您阅读了 javadoc,repaint() 函数会尽快安排重绘,这显然不是立即的。

修改单元格内容后立即编写此函数,如下所示:

cell.paintComponent(cell.getGraphics());

这应该立即绘制单元格的内容:)

于 2012-01-25T15:00:36.877 回答