将JPanel添加到JFrame后不显示 [英] JPanel is not shown after adding it to JFrame

查看:223
本文介绍了将JPanel添加到JFrame后不显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须设置一个基本的JAVA应用程序,其中包括基于swing的GUI. 我的想法是创建一个JFrame和不同的JPanels,并根据用户输入更改显示的JPanel. 运行我的应用程序后,将显示JFrame,但不显示JPanel的内容. 我以为到这里应该向前推进……但似乎并非如此.希望你能给我一个提示:)

I have to setup a basic JAVA application including a GUI based on swing. My idea was to create one JFrame and different JPanels and based on the users input to change the shown JPanel. After running my application the JFrame is shown but not the content of the JPanel. I thought up to here it should be strait forward... but as it seems it isn't. Hope you can give me a hint :)

这是我的主要爱好:

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();
    }
}

我的MainWindow:

my MainWindow:

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();
    }
}

我的LoginView:

my LoginView:

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;
}

推荐答案

除了调用revalidate()repaint()之外,您还知道另一件事,而不是仅在当前内容窗格中调用add()设置一个新的.

You know the other thing, than calling revalidate() and repaint(), that I would do is instead of calling add() to the current content pane just set a new one.

我认为您的问题与向当前内容窗格添加组件的错误方式有关.我认为在这种情况下,因此请使用setContentPane()方法.

I think your issue is related to a incorrect way of adding component to the current content pane. I think this is the case here, therefore, use the setContentPane() method.

这篇关于将JPanel添加到JFrame后不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆