public class Test extends JFrame {
private JPanel panel;
private JLabel label;
private JButton bot;
Test() {
this.setLayout(new GridLayout(1,1));
panel = new JPanel();
label = new JLabel("label");
bot = new JButton("button");
bot.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
pressedButton(evt);
}
});
panel.setLayout(new GridLayout(2,1));
panel.setAlignmentY(panel.CENTER_ALIGNMENT);
this.add(panel);
panel.add(label);
panel.add(bot);
this.pack();
}
public void pressedButton(ActionEvent e) {
Pnb pnbInst = new Pnb(); //instanciate the class that will provide the new JPanel
this.remove(panel); //remove the old panel from thr JFrame
panel = pnbInst.getPanel(); //set the panel to the new panel (from pnbInst)
panel.revalidate(); //revalidate?
this.add(panel); //add the new panel to the JFrame
}
public static void main(String[] args) {
new Test().setVisible(true);
}
}