【发布时间】: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()但没有效果 -
在窗口的内容窗格中调用它,这是顶层