我必须设置一个基本的JAVA应用程序,包括基于swing的GUI。 我的想法是创建一个JFrame和不同的JPanels,并根据用户输入来更改显示的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();
}
}
我的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:
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;
}
答案 0 :(得分:3)
除了调用revalidate()
和repaint()
之外,我知道的另一件事是,而不是只调用add()
到当前内容窗格,只需设置一个新内容。
我认为您的问题与向当前内容窗格添加组件的方式不正确有关。我认为这就是这种情况,因此,请使用setContentPane()
方法。
答案 1 :(得分: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();
}
...
}