8

我想用 JFrame 中的另一个 Jpanel 替换一个 Jpanel 我已经搜索并尝试了我的代码,但什么也没发生,这是我的代码:

public class Frame extends JFrame {

    private Container contain;
    private JPanel reChange,reChange2;
    private JButton reChangeButton;

    public Frame() {
        super("Change a panel");
        setSize(350, 350);
        setLayout(null);
        setLocationRelativeTo(null);
        setResizable(false);

        reChange = new JPanel(null);
        reChange.setBackground(Color.red);
        reChange.setSize(240, 225);
        reChange.setBounds(50, 50, 240, 225);
        add(reChange);

        reChangeButton = new JButton("Change It");
        reChangeButton.setBounds(20, 20, 100, 20);
        add(reChangeButton);

        reChangeButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //System.out.println("in");
                contain = getContentPane();
                contain.removeAll();
                //System.out.println("in2");

                reChange2 = new JPanel(null);
                reChange2.setBackground(Color.white);
                reChange2.setSize(240, 225);
                reChange2.setBounds(50, 50, 240, 225);
                //System.out.println("in3");

                contain.add(reChange2);
                validate();
                //System.out.println("in4");
                setVisible(true);
                //System.out.println("in5");
            }
        });

    }

    public static void main(String[] args) {
        Frame frame = new Frame();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

有人能帮我吗 ?非常感谢

4

6 回答 6

6
  1. 不要使用AbsoluteLayout

  2. 更改validate();actionPerformedcontain.validate();跟随contain.repaint();

  3. 将类名(保留的 Java 字或方法名)Framejava.awt.Frame)重命名为MyFrame(例如)

  4. 使用CardLayout而不是删除,然后JPanel在运行时添加一个新的

于 2013-02-14T12:20:09.930 回答
2

执行删除和添加操作后,您必须在包含面板上调用validate()然后。repaint()

contain.validate();
contain.repaint();
于 2013-02-14T12:16:26.580 回答
2

试试这个,下面的代码会将第二个 jPanel (NewOrder) 加载到第一个 jPanel(jpMain)

NewOrder no = new NewOrder(); //This is the object of Second JPanel

// jpMain - This is the First JPanel
jpMain.setLayout(new java.awt.BorderLayout());
jpMain.removeAll();
jpMain.add(no);
jpMain.revalidate();
于 2019-03-19T09:46:32.357 回答
1

您的代码有几个问题。这是固定版本:

public class Frame extends JFrame {

    private Container contain;
    private JPanel reChange,reChange2;
    private JButton reChangeButton;

    public Frame() {
        super("Change a panel");
        setSize(350, 350);
        getContentPane().setLayout(null); // Changed here
        setLocationRelativeTo(null);
        setResizable(false);

        reChange = new JPanel(null);
        reChange.setBackground(Color.red);
        reChange.setSize(240, 225);
        reChange.setBounds(50, 50, 240, 225);
        getContentPane().add(reChange); // Changed here

        reChangeButton = new JButton("Change It");
        reChangeButton.setBounds(20, 20, 100, 20);
        getContentPane().add(reChangeButton); // Changed here

        reChangeButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                contain = getContentPane();
                contain.removeAll();

                reChange2 = new JPanel(null);
                reChange2.setBackground(Color.white);
                reChange2.setSize(240, 225);
                reChange2.setBounds(50, 50, 240, 225);

                contain.add(reChange2);
                invalidate(); // Changed here
                repaint(); // Changed here
            }
        });
    }

    public static void main(String[] args) {
        Frame frame = new Frame();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}
于 2013-02-14T12:32:17.537 回答
1

你需要这样做:

     public void actionPerformed(ActionEvent e) {
        //System.out.println("in");
        contain = getContentPane();
        contain.removeAll();
        //System.out.println("in2");

        reChange2 = new JPanel(null);
        reChange2.setBackground(Color.white);
        reChange2.setSize(240, 225);
        reChange2.setBounds(50, 50, 240, 225);
        //System.out.println("in3");

        contain.add(reChange2);
        validate();
        repaint();
        //System.out.println("in4");
        setVisible(true);
        //System.out.println("in5");
    }
});
于 2013-02-14T12:20:32.570 回答
1

假设您有一个函数 generatePanel(),它返回一个 JPanel,您保存在 JPanel 类型的实例变量中:

private JPanel panelWithDynamicContent;

假设您已经将该 JPanel 放入容器中,可能位于该容器内的特定索引处,并且希望保留该容器内组件的顺序。与其使用 removeAll() 销毁所有内容,我更喜欢一种更精确的方法,只替换需要替换的组件:

private void replacePanel(){
   Container parent = this.panelWithDynamicContent.getParent();
   int index = parent.getComponentZOrder(this.panelWithDynamicContent);
   // remove the old edition of the panel
   parent.remove(this.panelWithDynamicContent);
   // generate the replacement panel
   this.panelWithDynamicContent = generatePanel();
   // place the replacement panel in the same relative location as the one it is replacing
   parent.add(this.panelWithDynamicContent, index);

   // must call both of these, in the correct order
   parent.validate();
   parent.repaint();
}
于 2018-04-24T22:33:52.827 回答