2013-04-18 52 views
1

我正在尝试在Java中实现GridBagLayout,以便为我正在构建的程序创建一个登录对话框。我正在寻求干净的Google登录。我遇到的主要问题是我使用GridBagConstraints设置的约束条件不起作用。这是我希望对话框的样子。在Java中实现GridBagLayout的问题

A simple sign in dialog using java

这里是因为我想实现对话框的代码。

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 

public class Login_Dialog extends JDialog{ 

    // SEtting up the required components for the sign in 

    /** 
    * 
    */ 
    private static final long serialVersionUID = 1L; 


    protected JLabel username_Label = new JLabel("Username"); 
    protected JLabel password_Label = new JLabel("Username"); 

    protected JTextField username_Field = new JTextField(30); 
    protected JTextField password_Field = new JTextField(30); 

    protected JButton sign_In = new JButton("Sign in!"); 

    public Login_Dialog() { 
     setSize(600,400); 
     setTitle("Sign in"); 

     setLayout(new GridBagLayout()); 

     GridBagConstraints c = new GridBagConstraints(); 

     c.fill = GridBagConstraints.HORIZONTAL; 
     c.gridx = 0; 
     c.gridy = 0; 

     add(username_Label); 

     c.fill = GridBagConstraints.HORIZONTAL; 
//  c.weightx = 0.5; 
     c.gridx = 1; 
     c.gridy = 0; 

     add(username_Field); 

     c.fill = GridBagConstraints.HORIZONTAL; 
     c.weightx = 0.5; 
     c.gridx = 0; 
     c.gridy = 2; 

     add(password_Label); 

     c.fill = GridBagConstraints.HORIZONTAL; 
     c.weightx = 0.5; 
     c.gridx = 0; 
     c.gridy = 4; 

     add(password_Field); 

     c.fill = GridBagConstraints.HORIZONTAL; 
     c.weightx = 0.5; 
     c.gridx = 0; 
     c.gridy = 5; 

     add(sign_In); 

     setVisible(true); 

    } 

} 

UPDATE:

我已经做了一些改变,似乎我达到我想要的结果。现在的问题是,一切都居中,按钮的长度太宽。另外,我希望textfield和label更大。

这里是更新为GridBagLayout

//cusotmization of buttons 
     Dimension signD = sign_In.getPreferredSize(); 
     signD.height = 50; 
     signD.width = 30; 

     sign_In.setPreferredSize(signD); 


     GridBagConstraints c = new GridBagConstraints(); 

     c.fill = GridBagConstraints.HORIZONTAL; 
     c.gridx = 0; 
     c.gridy = 0; 

     add(username_Label,c); 

     c.fill = GridBagConstraints.HORIZONTAL; 
//  c.weightx = 0.5; 
     c.gridx = 0; 
     c.gridy = 1; 

     add(username_Field,c); 

     c.fill = GridBagConstraints.HORIZONTAL; 
//  c.weightx = 0.5; 
     c.gridx = 0; 
     c.gridy = 2; 

     add(password_Label,c); 

     c.fill = GridBagConstraints.HORIZONTAL; 
//  c.weightx = 0.5; 
     c.gridx = 0; 
     c.gridy = 3; 

     add(password_Field,c); 
//  
//  c.fill = GridBagConstraints.HORIZONTAL; 
//  c.weightx = 0.5; 
     c.gridx = 0; 
     c.gridy = 5; 
//  
     add(sign_In,c); 

回答

7

您必须添加约束对象在add()

例子:

add(username_Field, c); 

哪里c是你的GridBagConstraints。

这是一个完整的例子:

package gui; 

import java.awt.Dimension; 
import java.awt.EventQueue; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import java.awt.Insets; 

import javax.swing.JButton; 
import javax.swing.JDialog; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JTextField; 
import javax.swing.WindowConstants; 

@SuppressWarnings("serial") 
public class LoginPanel extends JPanel 
{ 
    private static final Dimension TFIELD_SZ = new Dimension(120, 20); 

    public LoginPanel() 
    { 
     super(new GridBagLayout()); 

     final GridBagConstraints cst = new GridBagConstraints(); 
     cst.insets = new Insets(0, 5, 0, 5); 
     cst.weightx = 0.5; 
     cst.weighty = 0.5; 
     cst.anchor = GridBagConstraints.LINE_START; 

     // Username 
     cst.gridx = 0; 
     cst.gridy = 0; 
     this.add(new JLabel("Username:"), cst); 
     cst.gridy = 1; 
     cst.gridwidth = 2; 
     final JTextField uTField = new JTextField(); 
     uTField.setMinimumSize(TFIELD_SZ); 
     uTField.setPreferredSize(TFIELD_SZ); 
     this.add(uTField, cst); 

     // Password 
     cst.gridwidth = 1; 
     cst.gridy = 2; 
     this.add(new JLabel("Password:"), cst); 
     cst.gridy = 3; 
     cst.gridwidth = 2; 
     final JTextField pwTField = new JTextField(); 
     pwTField.setMinimumSize(TFIELD_SZ); 
     pwTField.setPreferredSize(TFIELD_SZ); 
     this.add(pwTField, cst); 

     // Buttons 
     cst.anchor = GridBagConstraints.CENTER; 
     cst.gridy = 4; 
     cst.gridwidth = 1; 
     this.add(new JButton("Sign in"), cst); 
     cst.gridx = 1; 
     this.add(new JButton("Sign up"), cst); 

    } 

    public static void main(final String[] args) 
    { 
     EventQueue.invokeLater(new Runnable() 
     { 
      @Override 
      public void run() 
      { 
       final JDialog myDialog = new JDialog(); 
       myDialog.setTitle("Sign in"); 
       myDialog.setSize(180, 170); 
       myDialog.setContentPane(new LoginPanel()); 
       myDialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); 
       myDialog.setVisible(true); 
      } 
     }); 
    } 
} 
+0

但将我所用'GridBagConstraints'使用的代码给我想要的结果吗? –

+0

我稍后会添加一个完整的示例,但我现在很忙。尝试一下! – phil

+0

好的,我会试试看,然后回复给你。 –