开发者

GridBagConstraints problem-moved to left and size isn't the same

I have in Java two panels which need to have same layout, there is my functions for initializations panels.

private void InitializePanelCom(){
        pnlCom=new JPanel();
        pnlCom.setSize(300,160);
        pnlCom.setLocation(10, 60);
        add(pnlCom);
        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("Send Time : ");
        jLabelx50 = setJLabel(" x 50 ms (2 - 99)");
        jComboBoxcommPort = setJComboBox(commPortList);
        jComboBoxbaudRate = setJComboBox(bitRateList);
        jTextAreaPlcAddress = setJTextField("");
        jTextAreaSendTime = setJTextField("");

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

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

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


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

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


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

        gc.insets=new Insets(10,0,0,0);
        gc.ipadx=120;
        gc.weightx=1;
        gc.gridx=0;
        gc.gridy=3;
        gc.anchor=GridBagConstraints.EAST;
        pnlCom.add(jLabelsendTime,gc);


        gc.insets=new Insets(10,0,0,0);
        gc.ipadx=120;
        gc.weightx=1;
        gc.gridx=1;
        gc.gridy=3;
        gc.anchor=GridBagConstraints.EAST;
        pnlCom.add(jTextAreaSendTime,gc);

        gc.insets=new Insets(10,0,0,0);
        gc.ipadx=120;
        gc.weightx=1;
        gc.gridx=2;
        gc.gridy=3;
        gc.anchor=GridBagConstraints.EAST;
        pnlCom.add(jLabelx50,gc);
    }
    ![alt text][1]


private void InitializePanelTcp(){

        pnlTcp=new JPanel();
        pnlTcp.setSize(300,160);
        pnlTcp.setLocation(10, 60);
        add(pnlTcp);
        GridBagLayout gb=new GridBagLayout();
        GridBagConstraints gc=new GridBagConstraints();
        pnlTcp.setLayout(gb);


        lblIPAddress=setJLabel("IP Address : ");
        txtIPAddress=setJTextField("");
        lblPort=setJLabel("Port : ");
        txtPort=setJTe开发者_StackOverflow社区xtField("");

        cmbBaudRateTCP = setJComboBox(bitRateList);
        lblBaudRateTCP = setJLabel("Baud Rate : ");
        lblParityCheck=setJLabel("Parity check : ");
        txtParityCheck=setJTextField("");



        gc.insets = new Insets(10,0,0,0);
        //gc.ipadx = 20; 
        gc.weightx = 0.3;
        gc.gridx = 0;
        gc.gridy = 0;
        gc.anchor=GridBagConstraints.WEST;
        pnlTcp.add(lblIPAddress,gc);

        gc.insets = new Insets(10,0,0,0);
        //gc.ipadx = 80; 
        gc.weightx = 0.7;
        gc.gridx = 1;
        gc.gridy = 0;
        gc.anchor=GridBagConstraints.WEST;
        pnlTcp.add(txtIPAddress,gc);

        gc.insets=new Insets(10,0,0,0);
        //gc.ipadx=120;
        gc.weightx=0.3;
        gc.gridx=0;
        gc.gridy=1;
        gc.anchor=GridBagConstraints.WEST;
        pnlTcp.add(lblPort,gc);


        gc.insets=new Insets(10,0,0,0);
        //gc.ipadx=80;
        gc.weightx=0.7;
        gc.gridx=1;
        gc.gridy=1;
        gc.anchor=GridBagConstraints.WEST;
        pnlTcp.add(txtPort,gc); 

        gc.insets=new Insets(10,0,0,0);
        //gc.ipadx=120;
        gc.weightx=0.3;
        gc.gridx=0;
        gc.gridy=2;
        gc.anchor=GridBagConstraints.WEST;
        pnlTcp.add(lblBaudRateTCP,gc);


        gc.insets=new Insets(10,0,0,0);
        //gc.ipadx=0;
        gc.weightx=0.7;
        gc.gridx=1;
        gc.gridy=2;
        gc.anchor=GridBagConstraints.WEST;
        pnlTcp.add(cmbBaudRateTCP,gc);  

        gc.insets=new Insets(10,0,0,0);
        //gc.ipadx=120;
        gc.weightx=0.3;
        gc.gridx=0;
        gc.gridy=3;
        gc.anchor=GridBagConstraints.WEST;
        pnlTcp.add(lblParityCheck,gc);


        gc.insets=new Insets(10,0,0,0);
        //gc.ipadx=0;
        gc.weightx=1.7;
        gc.gridx=1;
        gc.gridy=3;
        gc.anchor=GridBagConstraints.WEST;
        pnlTcp.add(txtParityCheck,gc);





    }

GridBagConstraints problem-moved to left and size isn't the same

GridBagConstraints problem-moved to left and size isn't the same

Problem is that first panel (initializetcp, picture doesn't look the same, labels are moved at left, textboxes are small and ugly , it is different ). Can anybody help, I am new with GridBagContsraints at all ?


Usually when I'm using weightx or weighty, I'm also using the fill constraint: gc.fill = GridBagConstraint.Horizontal;

For example, the following code will make your component to take 50% of the available horizontal space.

gc.weightx = 0.5;
gc.fill = GridBagConstraint.Horizontal;

I can't test your code actually, but give it a try, it should fix your problem. You should also try to call pack(); at the end of the parent constructor (JFrame I suppose).


With a quick glance, the problematic panel has all it's ipadx value declarations made as comments. The panel that looks good doesn't. Remove the // markings and see if that helps :)


You have my deepest sympathies...

http://madbean.com/anim/totallygridbag/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜