我想對齊複選框(紅色高亮區域)根據其他各個字段。在對話框中對齊複選框
這是我使用產生這種
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)
);
但在這種情況下它顯示了我非常小的文本框。
請讓我知道,如果你想除了它之外的任何東西。
你是如何在其他領域做呢?只需對複選框執行相同的操作即可。可選地,你也可以使用['setLabelFor(Component)'](http://docs.oracle.com/javase/7/docs/api/javax/swing/JLabel.html#setLabelFor(java.awt.Component)) –
請發佈[SSCCE](http://sscce.org) –
@Guillaume Polet我如何做複選框的同樣的事情,因爲這兩個組件都有不同的屬性(參考請檢查,已更新我的文章)。如果我錯了,請糾正我。 –