拥有JPopupMenu显着降低应用程序

时间:2015-11-20 15:17:39

标签: java performance swing jpopupmenu

我遇到了问题,请求你的帮助。 我添加了我自己的JPopupMenu以允许更改计算方法,但现在我的整个应用程序显着减慢了,我把它缩小到我所做的类:

class PopUpTimeCalcMethodChangeClickListener extends MouseAdapter {
    PopUpMenuTimeCalcMethodChange menu;
    public void mousePressed(MouseEvent e){
        if (e.isPopupTrigger())
            doPop(e);
    }

    public void mouseReleased(MouseEvent e){
        if (e.isPopupTrigger())
            doPop(e);
    }

    private void doPop(MouseEvent e){
        if(menu == null)
            menu = new PopUpMenuTimeCalcMethodChange();         
        menu.show(e.getComponent(), 0, 0);
    }
}

class PopUpMenuTimeCalcMethodChange extends JPopupMenu {
    public PopUpMenuTimeCalcMethodChange(){
        final String[] calctyps = {"Calculate by last download","Calculate by average speed"};
        for(int i = 0;i<calctyps.length;i++){
            final JCheckBox setCalcMethod = new JCheckBox(calctyps[i]);
            setCalcMethod.setIconTextGap(15);
            if(Main.TimeLeftCalculationMode == i)
                setCalcMethod.setSelected(true);
            setCalcMethod.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent arg0) {
                    for(Component c : getmenu().getComponents()){
                        if(c.getClass() == JCheckBox.class){
                            ((JCheckBox) c).setSelected(false);
                        }
                    }
                    setCalcMethod.setSelected(true);
                    System.out.println("lol we are running");
                    int index = -1;
                    for (int i=0;i<calctyps.length;i++) {
                        if (calctyps[i].equals(setCalcMethod.getText())) {
                            index = i;
                            break;
                        }
                    }

                    Main.TimeLeftCalculationMode = index;
                }
            });
            add(setCalcMethod);
        }
    }

    public JPopupMenu getmenu(){
        return this;
    }
}

主要的问题是代码在第一次执行后会减慢ui的速度,即使它不再可见。我假设它有关于actionlisteners的内容,但我不确定。

1 个答案:

答案 0 :(得分:0)

您确定减速是由您在问题中输入的课程引起的吗?如果我带你的PopUpMenuTimeCalcMethodChange课并添加一个简单的Main课程,那么应用程序对我来说并没有减慢速度。你注意到了多少减速?您可以在应用程序的某些点打印时间以进行一些测量。您可以添加更多代码以确保问题不在其他地方吗? (PopUpTimeCalcMethodChangeClickListener类似乎没有使用PopUpMenuTimeCalcMethodChange类。)

这是我尝试过的代码:

// Main class:

import javax.swing.*;

public class Main {
    protected static int TimeLeftCalculationMode;

    public static void main(final String[] arguments) {
        new Main().launchGui();
    }

    private void launchGui() {
        final JFrame frame = new JFrame("Stack Overflow");
        frame.setBounds(100, 100, 800, 600);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        final JPanel panel = new JPanel();
        panel.setComponentPopupMenu(new PopUpMenuTimeCalcMethodChange());
        frame.getContentPane().add(panel);

        frame.setVisible(true);
    }
}


// PopUpMenuTimeCalcMethodChange class:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class PopUpMenuTimeCalcMethodChange extends JPopupMenu {
    public PopUpMenuTimeCalcMethodChange(){
        final String[] calctyps = {"Calculate by last download",
                                   "Calculate by average speed"};
        for(int i = 0;i<calctyps.length;i++){
            final JCheckBox setCalcMethod = new JCheckBox(calctyps[i]);
            setCalcMethod.setIconTextGap(15);
            if(Main.TimeLeftCalculationMode == i)
                setCalcMethod.setSelected(true);
            setCalcMethod.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent arg0) {
                    for(Component c : getmenu().getComponents()){
                        if(c.getClass() == JCheckBox.class){
                            ((JCheckBox) c).setSelected(false);
                        }
                    }
                    setCalcMethod.setSelected(true);
                    System.out.println("lol we are running");
                    int index = -1;
                    for (int i=0;i<calctyps.length;i++) {
                        if (calctyps[i].equals(setCalcMethod.getText())) {
                            index = i;
                            break;
                        }
                    }

                    Main.TimeLeftCalculationMode = index;
                }
            });
            add(setCalcMethod);
        }
    }

    public JPopupMenu getmenu(){
        return this;
    }
}