2

这是代码的样子:

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 秒,然后完成其余的行。我尝试将其注释掉,所以我知道这是导致问题的原因。

有谁知道发生了什么?

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

4

2 回答 2

4

SSCCE 速度测试,用于CardLayout显示 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
..
于 2013-01-24T03:22:03.947 回答
1

我没有问题...

public class BadPaint10 {

    public static void main(String[] args) {
        new BadPaint10();
    }

    public BadPaint10() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception ex) {
                }

                final JPanel panel = new JPanel();
                panel.setBackground(Color.RED);

                final JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(panel);

                JButton change = new JButton("Switch");
                change.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        long start = System.currentTimeMillis();
                        frame.remove(panel);
                        long end = System.currentTimeMillis();
                        JPanel newPanel = new JPanel();
                        newPanel.setBackground(Color.BLUE);
                        frame.add(newPanel);
                        frame.validate();
                    }
                });
                frame.add(change, BorderLayout.SOUTH);
                frame.setSize(100, 100);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }
}

我认为您需要提供一个示例来说明您的问题

于 2013-01-24T02:32:48.623 回答