1

我必须设置一个基本的 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;
}
4

2 回答 2

3

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

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

于 2012-06-15T17:52:35.423 回答
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();
    }
    ...
}
于 2012-06-15T18:04:54.573 回答