2012-12-10 154 views
1

我想对齐复选框(红色高亮区域)根据其他各个字段。在对话框中对齐复选框

enter image description here

这是我使用产生这种

public class DialogTesting extends JFrame{ 

public static void main(String args[]) 
{ 

    JFrame frame = new JFrame(); 
    frame.setSize(320,250); 
    frame.setLocation(400,400); 

    JTextField txtUserName,txtHostName,txtPortNo,txtSID; 
    JPasswordField txtPassword; 
    JPanel mainPanel; 
    JCheckBox chkBoxSaveConnection; 

    mainPanel = new JPanel(); 
    mainPanel.setBorder(BorderFactory.createEmptyBorder()); 
    mainPanel.setPreferredSize(new Dimension(300, 210)); 
    mainPanel.setLayout(new FlowLayout()); 


    JLabel l_label = null; 
    txtUserName = new JTextField("K_USERNAME", 15); 
    txtUserName.putClientProperty("maxlength", 200); 
    l_label = new JLabel("User Name"); 
    l_label.setPreferredSize(new Dimension(100, 30)); 
    mainPanel.add(l_label); 
    mainPanel.add(txtUserName); 

    txtPassword = new JPasswordField("K_PASSWORD", 15); 
    txtUserName.putClientProperty("maxlength", 200); 
    l_label = new JLabel("Password"); 
    l_label.setPreferredSize(new Dimension(100, 30)); 
    mainPanel.add(l_label); 
    mainPanel.add(txtPassword); 

    txtHostName = new JTextField("K_HOSTNAME", 15); 
    txtHostName.putClientProperty("maxlength", 200); 
    l_label = new JLabel("Host Name"); 
    l_label.setPreferredSize(new Dimension(100, 30)); 
    mainPanel.add(l_label); 
    mainPanel.add(txtHostName); 

    txtPortNo = new JTextField("K_PORTNO", 15); 

    l_label = new JLabel("Port Number"); 
    l_label.setPreferredSize(new Dimension(100, 30)); 
    txtPortNo.putClientProperty("maxlength", 200); 
    mainPanel.add(l_label); 
    mainPanel.add(txtPortNo); 


    txtSID = new JTextField("K_SID", 15); 
    l_label = new JLabel("SID number"); 
    l_label.setPreferredSize(new Dimension(100, 30)); 
    txtPortNo.putClientProperty("maxlength", 200); 
    mainPanel.add(l_label); 
    mainPanel.add(txtSID); 

    chkBoxSaveConnection = new JCheckBox(); 
    l_label = new JLabel("chkBoxSaveConnection"); 
    l_label.setPreferredSize(new Dimension(150, 30)); 
    mainPanel.add(l_label); 
    mainPanel.add(chkBoxSaveConnection); 

    mainPanel.setVisible(true); 

    frame.add(mainPanel); 
    frame.setVisible(true); 

} 
} 

在这里,我想让复选框(红色突出显示区域)按照其他领域

我想这对准主要方法解决方案,使其正确对齐

mainPanel.setLayout(new GridLayout()); 
GridBagConstraints l_bag_constraints = new GridBagConstraints(); 
l_bag_constraints.fill = GridBagConstraints.HORIZONTAL; 
mainPanel.add(jlabel,FieldMapperHelper.getGridBagCompPosition(l_bag_constraints,0,0,10,1,0,10) 
      ); 
    mainPanel.add(txtUserName 
      ,FieldMapperHelper.getGridBagCompPosition(l_bag_constraints,0,1,10,1,0,10) 
      ); 

但在这种情况下它显示了我非常小的文本框。

请让我知道,如果你想除了它之外的任何东西。

+0

你是如何在其他领域做呢?只需对复选框执行相同的操作即可。可选地,你也可以使用['setLabelFor(Component)'](http://docs.oracle.com/javase/7/docs/api/javax/swing/JLabel.html#setLabelFor(java.awt.Component)) –

+2

请发布[SSCCE](http://sscce.org) –

+0

@Guillaume Polet我如何做复选框的同样的事情,因为这两个组件都有不同的属性(参考请检查,已更新我的文章)。如果我错了,请纠正我。 –

回答

2

好的,所以你的问题是你不使用适当的LayoutManager,你也不得不设置首选大小(你应该永远不要这样做)。

这里是你的代码的更新版本,它采用GridBagLayout而且应该很好地工作:

Result image

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

import javax.swing.JCheckBox; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JPasswordField; 
import javax.swing.JTextField; 
import javax.swing.SwingUtilities; 

public class DialogTesting { 

    protected void initUI() { 

     JFrame frame = new JFrame(); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     JTextField txtUserName, txtHostName, txtPortNo, txtSID; 
     JPasswordField txtPassword; 
     JPanel mainPanel; 
     JCheckBox chkBoxSaveConnection; 

     GridBagConstraints firstCol = new GridBagConstraints(); 
     firstCol.weightx = 1.0; 
     firstCol.anchor = GridBagConstraints.WEST; 
     firstCol.insets = new Insets(5, 20, 5, 5); 

     GridBagConstraints lastCol = new GridBagConstraints(); 
     lastCol.gridwidth = GridBagConstraints.REMAINDER; 
     lastCol.weightx = 1.0; 
     lastCol.fill = GridBagConstraints.HORIZONTAL; 
     lastCol.insets = new Insets(5, 5, 5, 20); 

     mainPanel = new JPanel(new GridBagLayout()); 

     JLabel l_label = null; 
     txtUserName = new JTextField("K_USERNAME", 15); 
     txtUserName.putClientProperty("maxlength", 200); 
     l_label = new JLabel("User Name"); 
     mainPanel.add(l_label, firstCol); 
     mainPanel.add(txtUserName, lastCol); 

     txtPassword = new JPasswordField("K_PASSWORD", 15); 
     txtUserName.putClientProperty("maxlength", 200); 
     l_label = new JLabel("Password"); 
     mainPanel.add(l_label, firstCol); 
     mainPanel.add(txtPassword, lastCol); 

     txtHostName = new JTextField("K_HOSTNAME", 15); 
     txtHostName.putClientProperty("maxlength", 200); 
     l_label = new JLabel("Host Name"); 
     mainPanel.add(l_label, firstCol); 
     mainPanel.add(txtHostName, lastCol); 

     txtPortNo = new JTextField("K_PORTNO", 15); 

     l_label = new JLabel("Port Number"); 
     txtPortNo.putClientProperty("maxlength", 200); 
     mainPanel.add(l_label, firstCol); 
     mainPanel.add(txtPortNo, lastCol); 

     txtSID = new JTextField("K_SID", 15); 
     l_label = new JLabel("SID number"); 
     txtPortNo.putClientProperty("maxlength", 200); 
     mainPanel.add(l_label, firstCol); 
     mainPanel.add(txtSID, lastCol); 

     chkBoxSaveConnection = new JCheckBox(); 
     l_label = new JLabel("chkBoxSaveConnection"); 
     mainPanel.add(l_label, firstCol); 
     mainPanel.add(chkBoxSaveConnection, lastCol); 

     frame.add(mainPanel); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 

    public static void main(String args[]) { 
     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       new DialogTesting().initUI(); 
      } 
     }); 
    } 
} 
+0

感谢您的回复。gridbaylayout中是否有任何Insets默认值。如果是,那么您是否还可以为它添加一些光线。 –

+1

@Sunil不,布局本身不会生成默认插入,因为您可以在'GridBagConstraint'上设置它们。 “GridBagConstraint”的缺省插入符是'new Insets(0,0,0,0)'。 –