gpt4 book ai didi

java - 从 JFrame 中删除 JPanel,太慢了

转载 作者:行者123 更新时间:2023-11-29 08:00:48 25 4
gpt4 key购买 nike

代码如下:

class Main extends JFrame {
public MyPanel panel;

public Main() {
//all the frame init stuff
panel = new MyPanel(this);
Panel badPanel = new Panel();//this makes the remove method go veryy slow
//add(badPanel, BorderLayout.SOUTH);//
JPanel goodPanel = new JPanel();
add(goodPanel, BorderLayout.SOUTH); // this fixes the slowness of the remove method in calculate()
add(panel, BorderLayout.CENTER);
}

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

class MyPanel extends JPanel {

Main main;

public MyPanel(Main main) {
this.main = main;
//init everything
}

public void calculate() {
MyPanel newPanel = new MyPanel(main);
//do some computation
main.remove(main.panel);
main.add(newPanel, BorderLayout.CENTER);
main.panel = newPanel;
main.revalidate();
}
}

所以一切正常,只是不知为何到了remove()方法的时候,执行时间太慢了,至少停顿5秒,然后完成剩下的线。我试着把它注释掉,所以我知道那是导致问题的那一行。

有人知道这是怎么回事吗?

编辑:所以这基本上是怎么回事。老实说,我不知道我还需要向您展示什么,代码中的任何其他内容都与我遇到的问题无关。如果我注释掉 remove 方法,一切都会很快,但是当它在那里时,它会非常缓慢。

最佳答案

CardLayout 的 SSCCE 速度测试显示 4 张图片(在一系列的 6 张图片中)。

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import java.text.DecimalFormat;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class CardLayoutAnimation {

public static void main(String[] args) throws Exception {
URL url1 = new URL("http://i.stack.imgur.com/XUmOD.png");
final Icon icon1 = new ImageIcon(url1);
URL url2 = new URL("http://i.stack.imgur.com/zKyiD.png");
final Icon icon2 = new ImageIcon(url2);
URL url3 = new URL("http://i.stack.imgur.com/4maMm.png");
final Icon icon3 = new ImageIcon(url3);
URL url4 = new URL("http://i.stack.imgur.com/wn9V5.png");
final Icon icon4 = new ImageIcon(url4);
Runnable r = new Runnable() {

@Override
public void run() {
final CardLayout cards = new CardLayout();
final JPanel gui = new JPanel(cards);
gui.setBorder(new EmptyBorder(100,300,100,300));
gui.setBackground(Color.WHITE);

gui.add(new JLabel(icon1), "label " + 1);
gui.add(new JLabel(icon2), "label " + 2);
gui.add(new JLabel(icon3), "label " + 3);
gui.add(new JLabel(icon4), "label " + 4);
gui.add(new JLabel(icon3), "label " + 5);
gui.add(new JLabel(icon2), "label " + 6);

ActionListener animate = new ActionListener(){

long lastTime = -1;
int frameCount = 0;
String timeString;
DecimalFormat format = new DecimalFormat("0.00");

@Override
public void actionPerformed(ActionEvent e) {
long time = System.nanoTime();
//if (frameCount%100==0) {
// System.out.println("animate! " + (time - lastTime) + " \t#: " + frameCount);
//}
if (lastTime<0) {
lastTime = time;
timeString = "00.00";
} else if(time-lastTime>1000) {
long duration = time-lastTime;
double fps = 1000000000d*(double)frameCount/(double)duration;
timeString = format.format(fps);
frameCount = 0;
lastTime = time;
System.out.println(timeString);
}
frameCount++;
cards.next(gui);
}
};
Timer timer = new Timer(5,animate);
timer.start();

JOptionPane.showMessageDialog(null, gui);
timer.stop();;
}
};
// Swing GUIs should be created and updated on the EDT
// http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
SwingUtilities.invokeLater(r);
}
}

FPS 结果

run:
54.82
123.43
556.57
170.96
170.80
170.18
170.84
171.09
169.93
169.03
173.09
170.05
170.75
171.20
170.35
170.91
170.17
146.58
170.44
170.61
171.01
170.73
170.14
171.13
126.81
208.12
170.40
169.97
170.83
171.55
170.39
..

关于java - 从 JFrame 中删除 JPanel,太慢了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14492848/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com