你们中的任何人都可以帮助我,我只是不能将txtIPAddress和ip address标记向左移动,现在已切断。我尝试使用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);

最佳答案

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

关于java - 将标签和文本框在Gridbaglayout中向左移动,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4721971/

10-10 13:06