我想根据其他相应的字段对齐复选框(红色突出显示区域)。
这是我用来生成这个的主要方法
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)
);
但在这种情况下,它向我展示了非常小的文本框。
如果您想要除此之外的任何其他内容,请告诉我。