简体   繁体   中英

JPanel is not shown after adding it to JFrame

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 :)

Here is my main:

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

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

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

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.

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.

As an alternative, use CardLayout to change panels.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM