简体   繁体   中英

Replacing JPanel with JPanel in a JFrame

I have a class that extends JFrame, and it has a BorderLayout. It has two private instance variables of type JPanel. They represent panels of buttons and are called flipButton and confidenceButtons. When you click on the button, the panel of buttons is replaced by the other panel of buttons. That is, if you click on a button in flipButton, flipButton is replaced by confidenceButtons. I tried to do it like this:

private class FlipListener implements ActionListener{
    public void actionPerformed(ActionEvent e){
      remove(flipButton); 
      add(confidenceButtons,BorderLayout.SOUTH);
      validate();
      ...
    }
  } 
  private class ColorListener implements ActionListener{
    ...
    public void actionPerformed(ActionEvent e){
      ...
      remove(confidenceButtons); 
      add(flipButton,BorderLayout.SOUTH);
      validate();
    }
  }

The buttons in flipButton have FlipListeners and the ones in confidenceButtons have ColorListeners. When the program is run, clicking on a button will remove the panel, but nothing is added to replace it. What am I doing wrong?

CardLayout turned out to be a simple and easy solution. It turns out that the above code does work; the problem was a typo in another section of my code. >.< However, I've always had trouble using these methods, and CardLayout, I find, simplifies it for me. Thanks.

Use a CardLayout , as shown here .

游戏画面高分视图

revalidate() + repaint() should be trick, example here

EDIT:

feel that you have got problem with that, examples for that here and here and again example by trashgod , feel free to built your question based on code again

another way is look at excelent example added by Andrew Thompson :-) +1

try using getContentPane() to call remove() ,add() methods ect..:

getContentPane().remove(flipButton); 
getContentPane().add(confidenceButtons,BorderLayout.SOUTH);   
getContentPane().revalidate();
getContentPane().repaint();

Edit: this code bellow work for me:

import java.awt.BorderLayout;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class Frame extends JFrame {
JPanel flipButton =new JPanel();
JPanel confidenceButtons =new JPanel();



    public Frame() throws HeadlessException {
    super();
    this.setLayout(new BorderLayout());
    JButton b1=new JButton("flip");
    b1.addActionListener(new FlipListener());
    flipButton.add(b1);

    JButton b2=new JButton("color");
    b2.addActionListener(new ColorListener());
    confidenceButtons.add(b2);
    this.getContentPane().add(flipButton,BorderLayout.SOUTH);
    this.setSize(250,250);
    this.pack();
    this.setVisible(true);

}
    private class FlipListener implements ActionListener{
        public void actionPerformed(ActionEvent e){
          remove(flipButton); 
          add(confidenceButtons,BorderLayout.SOUTH);
          validate();
          repaint();

        }
      } 
      private class ColorListener implements ActionListener{

        public void actionPerformed(ActionEvent e){

          remove(confidenceButtons); 
          add(flipButton,BorderLayout.SOUTH);
          validate();
          repaint();
        }
      }
    /**
     * @param args
     */
    public static void main(String[] args) {
        new Frame();

    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM