在 Gridbaglayout 中将标签和文本框移至左侧

发布于 2024-10-12 12:52:13 字数 2966 浏览 5 评论 0原文

你们中的任何好心人都可以帮助我吗,我只是无法将 txtIPAddress 和标签 ip 地址移至左侧,现在已被剪切。我尝试用weightx 0到1、ipadx进行实验,但没有帮助。这是我的红色面板功能。有什么帮助吗?

替代文本

pnlCom=new JPanel();
        pnlCom.setBackground(Color.red);
        pnlCom.setSize(460,160);
        pnlCom.setLocation(10, 60);

        add(pnlCom);
        //add(pnlTcp);
        add(pnlCommon);
        GridBagLayout gb=new GridBagLayout();
        GridBagConstraints gc=new GridBagConstraints();
        pnlCom.setLayout(gb);

        jLabelcommPort = setJLabel("Com Port : ");
        jLabelbaudRate = setJLabel("Baud Rate : ");
        jLabelplcAddress = setJLabel("Plc Address : ");
        jLabelsendTime = setJLabel("<html>Send Time : <br/>x 50 ms (2 - 99)</html>");
        jLabelx50 = setJLabel(" x 50 ms (2 - 99)");
        jComboBoxcommPort = setJComboBox(commPortList);
        jComboBoxbaudRate = setJComboBox(bitRateList);
        jTextAreaPlcAddress = setJTextField("");
        jTextAreaSendTime = setJTextField("");
        lblComType=setJLabel("Com type : ");
        cmbComType=setJComboBox(comType);
        lblIPAddress=setJLabel("IP Address : ");
        txtIPAddress=setJTextField("");



        gc.insets = new Insets(10,0,0,0);
        gc.ipadx = 80; 
        gc.weightx = 0.25;
        gc.gridx = 0;
        gc.gridy = 0;
        gc.anchor=GridBagConstraints.EAST;
        pnlCom.add(jLabelcommPort,gc);

        gc.insets = new Insets(10,0,0,0);
        gc.ipadx = 80; 
        gc.weightx = 0.25;
        gc.gridx = 1;
        gc.gridy = 0;
        gc.anchor=GridBagConstraints.EAST;
        pnlCom.add(jComboBoxcommPort,gc);




        gc.insets=new Insets(10,0,0,0);
        gc.ipadx=80;
        gc.weightx = 0.5;
        gc.gridx=0;
        gc.gridy=1;
        gc.anchor=GridBagConstraints.EAST;
        pnlCom.add(jLabelbaudRate,gc);


        gc.insets=new Insets(10,0,0,0);
        gc.ipadx=80;
        gc.weightx = 0.5;
        gc.gridx=1;
        gc.gridy=1;
        gc.anchor=GridBagConstraints.EAST;
        pnlCom.add(jComboBoxbaudRate,gc);


        gc.insets=new Insets(10,0,0,0);
        gc.ipadx=80;
        gc.weightx = 0.5;
        gc.gridx=0;
        gc.gridy=2;
        gc.anchor=GridBagConstraints.EAST;
        pnlCom.add(lblComType,gc);


        gc.insets=new Insets(10,0,0,0);
        gc.ipadx=80;
        gc.weightx = 0.25;
        gc.gridx=1;
        gc.gridy=2;
        gc.anchor=GridBagConstraints.EAST;
        pnlCom.add(cmbComType,gc);








        gc.insets = new Insets(10,0,0,0);
        gc.ipadx = 80; 
        gc.weightx = 0.25;
        gc.gridx = 2;
        gc.gridy = 0;
        gc.anchor=GridBagConstraints.WEST;
        pnlCom.add(lblIPAddress,gc);

        gc.insets = new Insets(10,0,0,0);
        gc.ipadx = 80; 
        gc.weightx = 0.25;
        gc.gridx = 3;
        gc.gridy = 0;
        gc.anchor=GridBagConstraints.WEST;
        pnlCom.add(txtIPAddress,gc);

Can anybody of you good people to help me, I just cannot to move txtIPAddress and label ip Address to left, it is cutted now. I tried to experiment with weightx 0 to 1, ipadx but it didn't help. This is my function for red panel. Any help please ?

alt text

pnlCom=new JPanel();
        pnlCom.setBackground(Color.red);
        pnlCom.setSize(460,160);
        pnlCom.setLocation(10, 60);

        add(pnlCom);
        //add(pnlTcp);
        add(pnlCommon);
        GridBagLayout gb=new GridBagLayout();
        GridBagConstraints gc=new GridBagConstraints();
        pnlCom.setLayout(gb);

        jLabelcommPort = setJLabel("Com Port : ");
        jLabelbaudRate = setJLabel("Baud Rate : ");
        jLabelplcAddress = setJLabel("Plc Address : ");
        jLabelsendTime = setJLabel("<html>Send Time : <br/>x 50 ms (2 - 99)</html>");
        jLabelx50 = setJLabel(" x 50 ms (2 - 99)");
        jComboBoxcommPort = setJComboBox(commPortList);
        jComboBoxbaudRate = setJComboBox(bitRateList);
        jTextAreaPlcAddress = setJTextField("");
        jTextAreaSendTime = setJTextField("");
        lblComType=setJLabel("Com type : ");
        cmbComType=setJComboBox(comType);
        lblIPAddress=setJLabel("IP Address : ");
        txtIPAddress=setJTextField("");



        gc.insets = new Insets(10,0,0,0);
        gc.ipadx = 80; 
        gc.weightx = 0.25;
        gc.gridx = 0;
        gc.gridy = 0;
        gc.anchor=GridBagConstraints.EAST;
        pnlCom.add(jLabelcommPort,gc);

        gc.insets = new Insets(10,0,0,0);
        gc.ipadx = 80; 
        gc.weightx = 0.25;
        gc.gridx = 1;
        gc.gridy = 0;
        gc.anchor=GridBagConstraints.EAST;
        pnlCom.add(jComboBoxcommPort,gc);




        gc.insets=new Insets(10,0,0,0);
        gc.ipadx=80;
        gc.weightx = 0.5;
        gc.gridx=0;
        gc.gridy=1;
        gc.anchor=GridBagConstraints.EAST;
        pnlCom.add(jLabelbaudRate,gc);


        gc.insets=new Insets(10,0,0,0);
        gc.ipadx=80;
        gc.weightx = 0.5;
        gc.gridx=1;
        gc.gridy=1;
        gc.anchor=GridBagConstraints.EAST;
        pnlCom.add(jComboBoxbaudRate,gc);


        gc.insets=new Insets(10,0,0,0);
        gc.ipadx=80;
        gc.weightx = 0.5;
        gc.gridx=0;
        gc.gridy=2;
        gc.anchor=GridBagConstraints.EAST;
        pnlCom.add(lblComType,gc);


        gc.insets=new Insets(10,0,0,0);
        gc.ipadx=80;
        gc.weightx = 0.25;
        gc.gridx=1;
        gc.gridy=2;
        gc.anchor=GridBagConstraints.EAST;
        pnlCom.add(cmbComType,gc);








        gc.insets = new Insets(10,0,0,0);
        gc.ipadx = 80; 
        gc.weightx = 0.25;
        gc.gridx = 2;
        gc.gridy = 0;
        gc.anchor=GridBagConstraints.WEST;
        pnlCom.add(lblIPAddress,gc);

        gc.insets = new Insets(10,0,0,0);
        gc.ipadx = 80; 
        gc.weightx = 0.25;
        gc.gridx = 3;
        gc.gridy = 0;
        gc.anchor=GridBagConstraints.WEST;
        pnlCom.add(txtIPAddress,gc);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

疯狂的代价 2024-10-19 12:52:13

尝试为所有 JLabels 设置 ipadx=0。另一件事是减少所有插图。您为所有控件保留了 inset=10。尝试将它们设置为 5。

Try to set ipadx=0 for all JLabels. One more thing is to reduce all the insets. You have left inset=10 for all controls. Try to set them to 5.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文