Java 将JPanel添加到JFrame后,不会显示它
我必须设置一个基本的JAVA应用程序,包括一个基于swing的GUI。 我的想法是创建一个JFrame和不同的JPanel,并根据用户的输入更改显示的JPanel。 运行我的应用程序后,会显示JFrame,但不会显示JPanel的内容。 我想在这里它应该是向前的。。。但看起来并非如此。希望你能给我一个提示: 以下是我的主要观点:Java 将JPanel添加到JFrame后,不会显示它,java,swing,jframe,jpanel,Java,Swing,Jframe,Jpanel,我必须设置一个基本的JAVA应用程序,包括一个基于swing的GUI。 我的想法是创建一个JFrame和不同的JPanel,并根据用户的输入更改显示的JPanel。 运行我的应用程序后,会显示JFrame,但不会显示JPanel的内容。 我想在这里它应该是向前的。。。但看起来并非如此。希望你能给我一个提示: 以下是我的主要观点: public class Client { public static void main(String[] args) { MainWindo
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;
}
您知道,除了调用revalidate和repaint之外,我要做的另一件事是不调用add to the current content pane,而是设置一个新的内容窗格
我认为您的问题与将组件添加到当前内容窗格的错误方式有关。因此,我认为这里就是这种情况,请使用setContentPane方法。除了调用revalidate和repaint之外,我要做的另一件事是不调用add到当前内容窗格,而是设置一个新的内容窗格 我认为您的问题与将组件添加到当前内容窗格的错误方式有关。我认为这里就是这种情况,因此,请使用setContentPane方法。或者使用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();
}
...
}
或者,使用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();
}
...
}
在将面板添加到框架后再次调用pack。在添加面板后和设置Visibile之前添加了pack,但这无助于尝试调用。重新验证;在顶级容器上,在显示gui并添加所有组件后,在JPanel上调用Revalidate,但在window的内容窗格上也不调用它,这是将面板添加到框架后的顶级呼叫包。在添加面板后和设置Visibile之前添加了呼叫包,但这无助于尝试呼叫。重新验证;在顶级容器上,在显示gui并添加所有组件后,在JPanel上调用Revalidate,但在window的内容窗格上也没有效果调用它,这就是顶级容器。。。它起作用了!但在我看到的所有教程中,它们都是通过getContentPane使用的方式。添加为什么我必须以不同的方式使用它!?您可以这样做,但您忘记的重要一点是在调用getContentPane上的add方法之前安装一个适当的布局管理器,例如,JPanel的默认值,即FlowLayout,或者在您的情况下是preferrable,因为您希望将整个当前视图交换为使用BorderLayout。啊,好的!在添加之前,我尝试将BorderLayout设置为LayoutManager,这同样有效。谢谢男人:@trashgood谢谢你,伙计,这很有趣。我认为采用这种方法唯一的问题是,所有面板将在开始时创建,而不是在需要时创建,对吗?PS:+1正对着你:完全正确,但开销很小。根据上下文,模式登录对话框也可能是合适的。编辑:此外,OP不应该让GUI设计师支配设计。太棒了。。。它起作用了!但在我看到的所有教程中,它们都是通过getContentPane使用的方式。添加为什么我必须以不同的方式使用它!?您可以这样做,但您忘记的重要一点是在调用getContentPane上的add方法之前安装一个适当的布局管理器,例如,JPanel的默认值,即FlowLayout,或者在您的情况下是preferrable,因为您希望将整个当前视图交换为使用BorderLayout。啊,好的!在添加之前,我尝试将BorderLayout设置为LayoutManager,这同样有效。谢谢男人:@trashgood谢谢你,伙计,这很有趣。我认为采用这种方法唯一的问题是,所有面板将在开始时创建,而不是在需要时创建,对吗?PS:+1正对着你:完全正确,但开销很小。根据上下文,模式登录对话框也可能是合适的。编辑:此外,OP不应该让GUI设计者支配设计。