Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/396.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 需要使用GridBagLayout的帮助吗_Java_Swing_Layout Manager_Gridbaglayout - Fatal编程技术网

Java 需要使用GridBagLayout的帮助吗

Java 需要使用GridBagLayout的帮助吗,java,swing,layout-manager,gridbaglayout,Java,Swing,Layout Manager,Gridbaglayout,我想在ID下面设置密码标签和文本字段。但我对GriBagLayout有点陌生 我希望你能帮助我 这是我的密码: class LoginPanel extends JPanel {//login components private JButton exitbtn = new JButton("Exit"); private JLabel idLabel = new JLabel("Staff ID : "); private JTextFie

我想在ID下面设置密码标签和文本字段。但我对GriBagLayout有点陌生

我希望你能帮助我

这是我的密码:

   class LoginPanel extends JPanel {//login components
        private JButton exitbtn = new JButton("Exit");
        private JLabel idLabel = new JLabel("Staff ID : ");
        private JTextField idJtf = new JTextField(10);
        private JLabel pwLabel = new JLabel("Password : ");
        private JPasswordField pwJtf = new JPasswordField(10);
        LoginPanel() {
           setOpaque(false);
           setLayout(new GridBagLayout());
           setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
           //add(new JLabel("Staff ID: ")); add(new JTextField(10));
           //add(new JLabel("Password: ")); add(new JPasswordField(10));
           add(idLabel);
           add(idJtf);
           add(pwLabel);
           add(pwJtf);
           //add(exitbtn);
        }      
}

它和阅读一样简单如果你熟悉使用表格创建HTML页面,GridBagLayout应该对你来说很容易

将控件一个放置在另一个控件之下,就像将它们放置到具有单列的表中一样。这意味着您需要将0设置为列号,[0..3]设置为行号:

GridBagConstraints c = new GridBagConstraints();
int rowNum = 0
c.gridx = 0;
c.gridy = rowNum;
add(idLabel, c);

c.gridy++;
add(idJtf, c);

c.gridy++;
add(pwLabel, c);

c.gridy++;
add(pwJtf, c);

但是,对于这种简单的布局,您可以使用其他布局管理器

如果您熟悉使用表格创建HTML页面,GridBagLayout对您来说应该很容易

将控件一个放置在另一个控件之下,就像将它们放置到具有单列的表中一样。这意味着您需要将0设置为列号,[0..3]设置为行号:

GridBagConstraints c = new GridBagConstraints();
int rowNum = 0
c.gridx = 0;
c.gridy = rowNum;
add(idLabel, c);

c.gridy++;
add(idJtf, c);

c.gridy++;
add(pwLabel, c);

c.gridy++;
add(pwJtf, c);

但是,对于这种简单的布局,您可以使用其他布局管理器,帮自己一个忙,停止使用GridBagLayout。GridBagLayout是前一个时代的layoutmanager。我会意识到这在所有“官方”Oracle Java文档中都存在,但大多数人现在都会避免使用它(这是有充分理由的)。GridBag要解决的复杂布局的一个很好的替代方案是Jgoodies FormLayout()。我向你保证,你会保持理智,你的生活质量会显著提高:-)

帮你自己一个忙,停止使用GridBagLayout。GridBagLayout是前一个时代的layoutmanager。我会意识到这在所有“官方”Oracle Java文档中都存在,但大多数人现在都会避免使用它(这是有充分理由的)。GridBag要解决的复杂布局的一个很好的替代方案是Jgoodies FormLayout()。我向你保证,你将保持理智,你的生活质量将显著提高:-)