我在重新绘制包含数百JFrame
的{{1}}时遇到问题。我的应用程序draws a "tile based" map,每个图块都是JFrames
。当我尝试JFrame
并重新绘制revalidate
时,您可以看到随机图块出现。
为了解决这个问题,我创建了一个覆盖旧地图的JFrame
(百叶窗),然后删除旧地图。只有这样我才能重绘。然后我添加新地图并删除" blind"然后重新画画。
有没有更好的方法来做到这一点。一些pop-in仍然可见。
JPanel
答案 0 :(得分:3)
如果做得好,你的瓷砖更换应该有最小的延迟,但我不确定你的代码和解释目前能否告诉我们你被困在哪里。例如,运行此代码(我的minimal example program)以查看切片以最小延迟更改颜色并且没有工件。
import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.util.Random;
import javax.swing.*;
public class SquareGridTest extends JPanel {
public static final Color[] COLORS = { Color.blue, Color.red, Color.yellow,
Color.orange, Color.green, Color.cyan, Color.lightGray, Color.magenta,
Color.white, Color.black };
private static final Icon[] ICONS = new Icon[COLORS.length];
private static final int ROWS = 20;
private static final int COLS = 30;
private static final int BI_WIDTH = 20;
private static final int BI_HEIGHT = BI_WIDTH;
private static final int TIMER_DELAY = 15;
protected static final int NUMBER_TO_SWAP = 15;
private JLabel[][] grid = new JLabel[ROWS][COLS];
private Random random = new Random();
static {
for (int i = 0; i < ICONS.length; i++) {
BufferedImage img = new BufferedImage(BI_WIDTH, BI_HEIGHT, BufferedImage.TYPE_INT_ARGB);
Graphics g = img.getGraphics();
g.setColor(COLORS[i]);
g.fillRect(0, 0, BI_WIDTH, BI_HEIGHT);
g.dispose();
ICONS[i] = new ImageIcon(img);
}
}
public SquareGridTest() {
setLayout(new GridLayout(ROWS, COLS));
for (int row = 0; row < grid.length; row++) {
for (int col = 0; col < grid[row].length; col++) {
JLabel label = new JLabel();
int index = random.nextInt(COLORS.length);
label.setIcon(ICONS[index]);
add(label);
grid[row][col] = label;
}
}
new Timer(TIMER_DELAY, new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
for (int i = 0; i < NUMBER_TO_SWAP; i++) {
int row = random.nextInt(ROWS);
int col = random.nextInt(COLS);
int iconIndex = random.nextInt(ICONS.length);
grid[row][col].setIcon(ICONS[iconIndex]);
}
}
}).start();
}
private static void createAndShowGui() {
SquareGridTest mainPanel = new SquareGridTest();
JFrame frame = new JFrame("SquareGridTest");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
有关您的代码的详细帮助,为了帮助我们更好地了解您的问题,请考虑创建并发布您自己的minimal example program。