如何用另一个JPanel替换JPanel

9

我希望能够在 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);
    }
}

有人可以帮我吗?非常感谢。

6个回答

6
  1. 不要使用AbsoluteLayout

  2. actionPerformed中将validate();改为contain.validate();,并跟随contain.repaint();

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

  4. 在运行时使用CardLayout代替删除然后添加一个新的JPanel


虽然我建议使用revalidate()而不是validate(),但对于CardLayout和建议,我会给一个+1。@ArdyYonathan请参见这里以获取CardLayout的示例。 - David Kroukamp
2
@David Kroukamp并不适用于所有Java用户,大多数人仍在使用Java6和较低版本(错过了Windows操作系统的大部分限制)。 - mKorbel
+1 真的。哈哈,但也许我们的代码会迫使他们使用 Java 7(或最新版本) :) - David Kroukamp
1
aaaach :-) 这是直升机管理员用户在 Windows 中的错误访问,例如简单用户(内置访问)永远不会更新 JRE 等(大多数更新肯定可以很容易地配置,但为什么要麻烦呢,点)。 - mKorbel

3
尝试这样做, 下面的代码将第二个 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();

revalidate() 是关键。 - Charles Mosndup

2

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

注意:请保留HTML标签。
contain.validate();
contain.repaint();

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();
}

1

you need to do like this :

     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");
    }
});

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);
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接