2011-01-13 2 views
0

Ich habe in Java zwei Panels, die das gleiche Layout haben müssen, gibt es meine Funktionen für Initialisierungen Panels.GridBagConstraints Problem-nach links bewegt und Größe ist nicht das gleiche

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=setJTextField(""); 

     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); 





    } 

alt text

alt text Problem ist, dass erste Platte (initializetcp, Bild links nicht gleich aussehen, sind Etiketten bewegt, Textfelder sind klein und hässlich, es ist anders). Kann jemand helfen, ich bin neu mit GridBagConstrains überhaupt?

Antwort

2

Normalerweise benutze ich, wenn ich Gewicht oder Gewicht benutze, auch die Füllbedingung: gc.fill = GridBagConstraint.Horizontal;

Zum Beispiel wird der folgende Code Ihre Komponente 50% des verfügbaren horizontalen Speicherplatzes nehmen.

Ich kann Ihren Code tatsächlich nicht testen, aber versuchen Sie es, es sollte Ihr Problem beheben. Sie sollten auch versuchen, pack(); am Ende des Elternkonstruktors (JFrame nehme ich an).

+0

Füllmenge einstellen und Gewicht einstellen. Genau. –

1

Mit einem kurzen Blick hat das problematische Panel alle seine ipadx Wert-Erklärungen als Kommentare gemacht. Das Panel, das gut aussieht, tut das nicht. Entfernen Sie die // Markierungen und sehen Sie, ob das hilft :)

+0

Dank für die schnelle Antwort. Ich probierte das, bevor ich Forum anbrachte (ich versuchte, Größe des Gremiums zu ändern, mischte ich, aber hilft nicht), Aufkleber sind wieder links. Wie positioniert man Etiketten wie in einem guten Panel? Was sind Tags dafür? – Damir