【问题标题】:Erase Swing Content Pane/Panel & display a new panel擦除 Swing 内容窗格/面板并显示新面板
【发布时间】:2011-02-08 23:58:41
【问题描述】:

我创建了一个小程序,当您按下“忘记密码”按钮时,我会删除小程序上的当前 JPanel 并创建一个新的 JPanel,以显示与检索/忘记密码相关的 JComponent。

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

在我创建了所有新的 JComponents 之后,我尝试放置 .invalidate(),但仍然没有刷新小程序?

如何在使用 .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
    }
}

【问题讨论】:

    标签: java swing


    【解决方案1】:

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

    【讨论】:

    • 使用 JRE7,我需要同时使用两者。一个简单的重绘给我留下了一个空的灰色窗格,一个简单的重新验证在旧的内容上绘制了新的内容。
    【解决方案2】:

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

    【讨论】:

      【解决方案3】:
      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
      

      【讨论】:

      • 请不要只发布代码作为答案,还要解释您的代码的作用以及它如何解决问题的问题。带有解释的答案通常更有帮助、质量更好,并且更有可能吸引投票。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-06-24
      • 2019-08-03
      • 2013-05-21
      • 2021-12-26
      • 1970-01-01
      • 2021-06-12
      • 1970-01-01
      相关资源
      最近更新 更多