【问题标题】:how to remove JPanel correctly如何正确删除JPanel
【发布时间】:2018-09-24 18:48:06
【问题描述】:

我已经检查过How can I remove a JPanel from a JFrame?,但是当我尝试做frame.remove(mainScreen) 时,整个事情就僵住了。我也试过mainScreen.setVisible(false);但这显示了球,但它们是静止的(它们应该移动)。

// important variables
        FlowLayout fL = new FlowLayout();

        // create frame
        JFrame frame = new JFrame("Ball");
        frame.setTitle("GaoMolecules");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(500,500);
        frame.setResizable(false);

        Rectangle fDim = frame.getBounds(); // frameDimensions



        // add panel to frame
        JPanel mainScreen = new JPanel();
        mainScreen.setLayout(fL);
        fL.setAlignment(FlowLayout.TRAILING);


        frame.add(mainScreen);

        // other panel components   
        JTextField numBallsField = new JTextField(4);
        JLabel numBallsLabel = new JLabel("Enter the number of balls to generate");
        JButton startButton = new JButton("START");

        mainScreen.add(numBallsLabel);
        mainScreen.add(numBallsField);
        mainScreen.add(startButton);    


        // add listener to button
        startButton.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                //System.out.println("Button Working!");
                mainScreen.setVisible(false);
                frame.add(new DrawManager());
                start = true;
            }
        });

        // this needs to be the very LAST
        frame.setVisible(true); 

【问题讨论】:

    标签: jframe jpanel jbutton


    【解决方案1】:

    任何时候你执行 remove(),你都应该调用

      frame.validate();
      frame.repaint();
    

    【讨论】:

      猜你喜欢
      • 2014-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-03
      • 2011-01-30
      • 2012-10-03
      相关资源
      最近更新 更多