在JButton监听器中替换两个JPanel时程序挂起

时间:2016-09-21 21:22:13

标签: java swing jpanel jbutton actionlistener

我有一个JFrame和两个JPanel。其中第一个有“继续”按钮,第二个有“返回”按钮。程序启动时,第一个面板将添加到框架中。当我按下“继续”按钮时,我想要更换面板 - 与“返回”相同。

public class Main extends JFrame {

    private final int width = 320;
    private final int height = 640;

    private JPanel firstPanel;
    private JPanel secondPanel;

    private JButton goOn;
    private JButton goBack;


    public Main() {

        firstPanel = new JPanel();
        firstPanel.setBackground(Color.yellow);
        firstPanel.setOpaque(true);
        firstPanel.setPreferredSize(new Dimension(width, height));
        goOn = new JButton("Go on");
        goOn.setPreferredSize(new Dimension(width/2, height/2));
        firstPanel.add(goOn);

        secondPanel = new JPanel();
        secondPanel.setBackground(Color.green);
        secondPanel.setOpaque(true);
        secondPanel.setPreferredSize(new Dimension(width, height));
        goBack = new JButton("Go back");
        goBack.setPreferredSize(new Dimension(width/2, height/2));
        secondPanel.add(goBack);

        showFirstPanel();

        setResizable(false);
        pack();

        setLocationRelativeTo(null);
        setVisible(true);

        addButtonsListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (e.getActionCommand().equals("Go on"))
                    showSecondPanel();
                else
                    showFirstPanel();
            }
        });
    }

    public void showFirstPanel() {
        remove(secondPanel);
        add(firstPanel);
    }

    public void showSecondPanel() {
        remove(firstPanel);
        add(secondPanel);
    }

    public void addButtonsListener(ActionListener listener) {
        goOn.addActionListener(listener);
        goBack.addActionListener(listener);
    }

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

问题是当我按下第一个按钮时,程序挂起。有时它会在10-20秒后起作用。 “返回”按钮也会出现同样的情况。

1 个答案:

答案 0 :(得分:0)

我建议你忘掉remove()并用setVisible()替换它 所以你的代码将是

    public class Main extends JFrame {

       private final int width = 320;
       private final int height = 640;

       private JPanel firstPanel;
       private JPanel secondPanel;

       private JButton goOn;
       private JButton goBack;


       public Main() {

           firstPanel = new JPanel();
           firstPanel.setBackground(Color.yellow);
           firstPanel.setOpaque(true);
           firstPanel.setPreferredSize(new Dimension(width, height));
           goOn = new JButton("Go on");
           goOn.setPreferredSize(new Dimension(width/2, height/2));
           firstPanel.add(goOn);

           secondPanel = new JPanel();
           secondPanel.setBackground(Color.green);
           secondPanel.setOpaque(true);
           secondPanel.setPreferredSize(new Dimension(width, height));
           goBack = new JButton("Go back");
           goBack.setPreferredSize(new Dimension(width/2, height/2));
           secondPanel.add(goBack);

           add(firstPanel);
           add(secondPanel);

           showFirstPanel();
           setResizable(false);
           pack();

           setLocationRelativeTo(null);
           setVisible(true);

           addButtonsListener(new ActionListener() {
              @Override
              public void actionPerformed(ActionEvent e) {
                    if (e.getActionCommand().equals("Go on"))
                       showSecondPanel();
                    else
                       showFirstPanel();
              }
          });
       }

       public void showFirstPanel() {
               secondPanel.setVisible(false);
               firstPanel.setVisible(true);
        }

        public void showSecondPanel() {
             firstPanel.setVisible(false);
             secondPanel.setVisible(false);
        }

        public void addButtonsListener(ActionListener listener) {
              goOn.addActionListener(listener);
              goBack.addActionListener(listener);
        }

        public static void main (String[] args) {
            new Main();
       }
     }
相关问题