简体   繁体   English

GridBagLayout中右边的1个组件

[英]1 component off to the right in GridBagLayout

Problem of the gui screen. gui屏幕问题。 Login component off to the side. 将组件登录到一边。 在此处输入图片说明

Resolution of the gui screen. gui屏幕的分辨率。 Had to make jtextfield's comp width 2 for some reason. 由于某种原因不得不使jtextfield的comp宽度为2。 在此处输入图片说明

I have a little thing thing that I have no idea what's been causing it... My login button is off to the right side. 我有一点小事,我不知道是什么原因造成的...我的登录按钮在右侧。 I know that it's probably caused by the jtextfields being so big and then positioning the 3rd cell next to it but is there any way to fix it? 我知道这可能是由于jtextfields太大,然后将第三个单元格放置在它旁边引起的,但是有什么办法可以解决? Or is it the JCheckBox somehow conflicting with the 3rd row components because it is too small? 还是JCheckBox太小而与第三行组件发生冲突? I need the login button to somehow move to the right more but I don't think it's possible because the 2nd x cell has 2 JTextField components that take up all the space. 我需要登录按钮以某种方式进一步向右移动,但我认为这是不可能的,因为第二个x单元具有2个JTextField组件,这些组件占据了所有空间。

        addComp(pLogin, new JLabel("Name: "), 0, 0, 1, 1, GridBagConstraints.LINE_END);
    addComp(pLogin, new JLabel("Password: "), 0, 1, 1, 1, GridBagConstraints.LINE_END);

    //these are going to be formatted fields. Remember johnny remove the new jtextfield soon
    addComp(pLogin, new JTextField(15), 1, 0, 1, 1, GridBagConstraints.LINE_START);
    addComp(pLogin, new JTextField(15), 1, 1, 1, 1, GridBagConstraints.LINE_START);

    addComp(pLogin, remember_User, 0, 2, 1, 1, GridBagConstraints.LINE_END);
    addComp(pLogin, new JLabel("Remember me"), 1, 2, 1, 1, GridBagConstraints.LINE_START);
    addComp(pLogin, login, 2, 2, 1, 1, GridBagConstraints.LINE_START);

    frame.setVisible(true);
}

    private void addComp(JPanel panel, JComponent comp, int xPos, int yPos, int compWidth, int compHeight, int place) {
        GridBagConstraints gbc = new GridBagConstraints();

        gbc.gridx = xPos;
        gbc.gridy = yPos;
        gbc.gridwidth = compWidth;
        gbc.gridheight = compHeight;
        gbc.anchor = place;
        gbc.insets = new Insets(5,5,5,5);

        panel.add(comp, gbc);
    }

Just change width of both text fields to 2 rather than 1 只需将两个文本字段的宽度更改为2而不是1

addComp(pLogin, new JTextField(15), 1, 0, 2, 1, GridBagConstraints.LINE_START);
addComp(pLogin, new JTextField(15), 1, 1, 2, 1, GridBagConstraints.LINE_START);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM