【问题标题】:JPanel is not shown after adding it to JFrameJPanel 添加到 JFrame 后不显示
【发布时间】:2012-06-15 17:13:02
【问题描述】:

我必须设置一个基本的 JAVA 应用程序,包括一个基于 swing 的 GUI。 我的想法是创建一个 JFrame 和不同的 JPanel,并根据用户输入来更改显示的 JPanel。 运行我的应用程序后,会显示 JFrame,但不会显示 JPanel 的内容。 我认为到这里它应该是海峡前进......但似乎不是。希望你能给我一个提示:)

这是我的主要内容:

public class Client {
    public static void main(String[] args) {
        MainWindow window = new MainWindow();
        window.setVisible(true);

        LoginView pane = new LoginView();
        window.getContentPane().add(pane);

        window.invalidate();
        window.repaint();
    }
}

我的主窗口:

package client;

public class MainWindow extends javax.swing.JFrame {
    public MainWindow() {
        initComponents();
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    private void initComponents() {
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 352, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 250, Short.MAX_VALUE)
        );
        pack();
    }
}

我的登录视图:

package client.views;

public class LoginView extends javax.swing.JPanel {
    public LoginView() {
        initComponents();
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    private void initComponents() {
        jLabel1 = new javax.swing.JLabel();
        jLabel1.setText("Login");
        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jLabel1)
                .addContainerGap(345, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jLabel1)
                .addContainerGap(264, Short.MAX_VALUE))
        );
    }
    private javax.swing.JLabel jLabel1;
}

【问题讨论】:

  • 将面板添加到框架后再次致电pack()
  • 在添加面板之后和 setVisibile 之前添加了pack(),但它没有帮助
  • 尝试调用 .revalidate();在顶层容器上,显示 gui 并添加所有组件后
  • 试图在JPanel上调用revalidate()但没有效果
  • 在窗口的内容窗格中调用它,这是顶层

标签: java swing jframe jpanel


【解决方案1】:

除了调用revalidate()repaint() 之外,你知道的另一件事是,我不会调用add() 来调用当前的内容窗格,而是设置一个新的。

我认为您的问题与将组件添加到当前内容窗格的方式不正确有关。我认为这里就是这种情况,因此,请使用 setContentPane() 方法。

【讨论】:

  • 太棒了......它的工作原理!但是在我看到的所有教程中,他们都使用getContentPane().add() 的方式,为什么我必须以不同的方式使用它!?
  • 你可以像你一样做,但重要的是你忘记了在调用getContentPane()上的add()方法之前安装一个合适的布局管理器,例如JPanel的默认值,即FlowLayout,或者在您的情况下更可取,因为您想交换整个当前视图以使用BorderLayout
  • 啊好吧!我尝试在添加之前将 BorderLayout 设置为 LayoutManger,这也可以。谢谢人:)
  • @trashgod 谢谢伙计,这很有趣。我认为使用这种方法唯一的问题(argument-al)是所有面板都将在开始时创建,而不是在需要时创建,对吧? PS:+1 就在你身边 :)
  • 完全正确,但开销很小。根据上下文,模式登录对话框也可能是合适的。编辑:此外,OP 不应该让 GUI 设计师决定设计。
【解决方案2】:

作为替代方法,使用CardLayout 更改面板。

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Client {

    public static void main(String[] args) {
        JFrame window = new JFrame();
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        final CardLayout cl = new CardLayout();
        final JPanel cards = new JPanel(cl);
        cards.add(new LoginView(), "Login");
        cards.add(new MainView(), "Main");
        window.add(cards);
        JPanel control = new JPanel();
        control.add(new JButton(new AbstractAction("Login") {

            @Override
            public void actionPerformed(ActionEvent e) {
                cl.show(cards, "Main");
            }
        }));
        window.add(control, BorderLayout.SOUTH);
        window.pack();
        window.setLocationRelativeTo(null);
        window.setVisible(true);
    }
}

class MainView extends javax.swing.JPanel {

    public MainView() {
        initComponents();
    }
    ...
}

class LoginView extends javax.swing.JPanel {

    public LoginView() {
        initComponents();
    }
    ...
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-29
    • 1970-01-01
    • 1970-01-01
    • 2013-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多