当我创建 JButtons 时,我得到了很大的延迟。这是我如何创建按钮的一些示例代码:
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class Scratch {
public static void main(String[] args) {
Runnable r = () -> {
JOptionPane.showMessageDialog(
null, new Scratch().getUI(new TileSet("Content/Graphics/tileSets/12x12x3 - tileSet.png", 12, 12, 3)));
JOptionPane.showMessageDialog(
null, new Scratch().getUI(new TileSet("Content/Graphics/tileSets/16x16x0 - tileSetItems.png", 12, 12, 3)));
JOptionPane.showMessageDialog(
null, new Scratch().getUI(new TileSet("Content/Graphics/tileSets/29x18x1 - roguelikeDungeon_transparent.png", 12, 12, 3)));
};
SwingUtilities.invokeLater(r);
}
public final JComponent getUI(TileSet tileSet) {
JPanel ui = new JPanel();
JPanel tilePanel = new JPanel();
tilePanel.setLayout(new GridLayout(12, 12, 5, 5));
long t1 = System.currentTimeMillis();
TileButton tileMenuButtons[] = new TileButton[tileSet.tileSet.length];
long tot = 0;
for (int i = 0; i < tileMenuButtons.length; i++) {
long t2 = System.currentTimeMillis();
tileMenuButtons[i] = new TileButton(i,tileSet);
long t3 = System.currentTimeMillis();
tot += (t3-t2);
System.out.println(String.format("It took : "+ tot +"ms for loading "+i+ ". Button "));
tilePanel.add(tileMenuButtons[i]);
}
long t4 = System.currentTimeMillis();
JScrollPane scrollPane = new JScrollPane();
scrollPane.getVerticalScrollBar().setUnitIncrement(16);
scrollPane.setOpaque(true);
scrollPane.setViewportView(tilePanel);
ui.add(scrollPane);
System.out.println(String.format("It took in total : "+ (t4-t1) +"ms for loading "+tileMenuButtons.length+ " TileButtons"));
return ui;
}
我可以过滤问题是由我的按钮类引起的,但我不明白在哪里以及为什么?
繁星coding
UYOU
相关分类