簡體   English   中英

如何用另一個JPanel替換JPanel

[英]How to replace JPanel with another JPanel

我想在JFrame中替換另一個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);
    }
}

有人能幫我嗎 ? 非常感謝

  1. 不要使用AbsoluteLayout

  2. change validate(); in actionPerformed to contain.validate(); 並跟隨contain.repaint();

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

  4. 使用CardLayout而不是刪除,然后在運行時添加一個新的JPanel

執行刪除和添加操作后,必須在包含的面板上調用validate()然后repaint()

contain.validate();
contain.repaint();

你需要這樣做:

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

您的代碼有幾個問題。 這是固定版本:

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

假設你有一個函數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();
}

試試這個,下面的代碼將第二個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();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM