我必須設置基本的JAVA應用程序,包括基於swing的GUI。 我的想法是創建一個JFrame和不同的JPanels,並基於用戶輸入來更改顯示的JPanel。 運行我的應用程序後,將顯示JFrame,但不顯示JPanel的內容。 我認爲在這裏它應該是海峽前進......但它看起來不是。希望你能給我一個提示:)將JPanel添加到JFrame後未顯示
這裏是我的主:
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();
}
}
我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;
}
將面板添加到幀後再次調用'pack()'。 – Boro
在添加面板之後和setVisibile之前添加'pack()',但它不會幫助 – soupdiver
嘗試調用.revalidate();在頂層容器上,在顯示了gui並添加了所有組件 – Michael