擦除Swing内容窗格/面板&显示一个新面板

时间:2011-02-08 23:58:41

标签: java swing

我创建了一个applet,当你按下“忘记密码”按钮时,我删除applet上的当前JPanel&创建一个新的JPanel,显示与检索/忘记密码相关的JComponents。

我可以使用.removeAll();成功清除JPanel;在我创建了所有新的JComponents&之后将它们添加到内容窗格(主JPanel),applet只是灰色&没有显示新的JPanel&组件除非我调整小程序的大小,然后重新绘制&作品。

我在创建了所有新的JComponents之后尝试放入.invalidate()但是仍然没有刷新applet?

如何在使用.removeAll()&清除它之后让我的JPanel出现。添加不同的JComponents吗?

代码:

public class App extends JApplet
{
    JPanel mainPanel; 

    public void init()
    {
        SwingUtilities.invokeAndWait( new Runnable() {
            public void run()
            {
                showLoginPanel(); // this shows fine on loading
            }
        });

    }

    public void showForgotPassPanel()
    {
        mainPanel.removeAll();

        mainPanel = (JPanel) getContentPane();
        Box hBox  = Box.createHorizontalBox();
        Box vBox  = Box.createVerticalBox();
        mainPanel.setLayout( new BorderLayout() ); 

        ... create components

        ... add components to mainPanel

        mainPanel.invalidate(); // doesn't make new layout visible, not unless I resize the applet
    }
}

3 个答案:

答案 0 :(得分:5)

使用mainPanel.revalidate();和/或mainPanel.repaint();方法。

答案 1 :(得分:0)

另一个“干净”选项是用CardLayout交换视图。它在幕后为你做了所有肮脏的工作。

答案 2 :(得分:0)

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

import static javax.swing.JFrame.EXIT_ON_CLOSE;

import java.awt.HeadlessException;

public class PruebaVentana extends JFrame {
    JButton b1, b2;
    JPanel panel1 = new JPanel();
    JPanel panel2 = new JPanel();
    JPanel contenedor = new JPanel();

    public PruebaVentana() throws HeadlessException, InterruptedException {
        super("Abriendo nuevo JPanel por medio de un botón");
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setBounds(50, 50, 500, 300);

        iniciarPanel();
    }// fin de constructor PruebaVentana

    public void iniciarPanel() {
        this.getContentPane().add(contenedor);
        contenedor.setLayout(null);
        contenedor.add(this.panel1);
        contenedor.add(this.panel2);
        panel2.setBackground(new Color(0, 10, 150));
        panel1.setBackground(new Color(150, 0, 0));

        panel1.setBounds(0, 0, 500, 230);
        panel2.setBounds(0, 0, 500, 230);
        panel1.setLayout(null);
        panel2.setLayout(null);
        panel2.setVisible(false);

        b1 = new JButton("Abre Panel 2");
        b2 = new JButton("Abre Panel 1");

        b1.setBounds(10, 10, 150, 30);
        b1.setForeground(new Color(200, 100, 50));
        b2.setBounds(170, 10, 150, 30);
        b2.setForeground(new Color(50, 100, 200));

        panel1.add(b1);
        panel2.add(b2);

        b1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                panel2.setVisible(true);
                panel1.setVisible(false);
            }// fin de actionPerformed
        });// fin de actionListener

        b2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                panel1.setVisible(true);
                panel2.setVisible(false);
            }// fin de actionPerformed
        });// fin de actionListener

    }// fin de iniciarPanel

    public static void main(String[] args) throws HeadlessException, InterruptedException {
        PruebaVentana v = new PruebaVentana();
        v.setVisible(true);

    }// fin de método main

}// fin de clase PruebaVentana