2012-06-15 60 views
1

我必须设置基本的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; 
} 
+0

将面板添加到帧后再次调用'pack()'。 – Boro

+0

在添加面板之后和setVisibile之前添加'pack()',但它不会帮助 – soupdiver

+0

尝试调用.revalidate();在顶层容器上,在显示了gui并添加了所有组件 – Michael

回答

3

你知道其他的事情,比打电话revalidate()repaint(),我会做的是,而不是调用add()到当前内容窗格只需设置一个新的窗格。

我认为你的问题涉及到向当前内容窗格添加组件的不正确方法。我认为这是这种情况,因此,使用setContentPane()方法。

+0

真棒......它的作品!但是在所有的教程中,我已经看到他们通过'getContentPane()。add()'方法使用了这种方式,为什么我必须以不同的方式来使用它! – soupdiver

+1

你可以这样做,但重要的是你已经忘记了在getContentPane()调用'add()'方法之前安装合适的布局管理器,例如'JPanel'的默认值,即' FlowLayout',或者在你的情况下可取的,因为你想交换整个当前视图来使用'BorderLayout'。 – Boro

+1

啊好的!我试图在添加之前将BorderLayout设置为LayoutManger,这也是可行的。谢谢男人:) – soupdiver

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(); 
    } 
    ... 
}