Java GridBagConstraints问题向左移动,大小为';不一样
我在Java中有两个面板,它们需要有相同的布局,还有我的初始化面板的函数Java GridBagConstraints问题向左移动,大小为';不一样,java,swing,gridbaglayout,Java,Swing,Gridbaglayout,我在Java中有两个面板,它们需要有相同的布局,还有我的初始化面板的函数 private void InitializePanelCom(){ pnlCom=new JPanel(); pnlCom.setSize(300,160); pnlCom.setLocation(10, 60); add(pnlCom); GridBagLayout gb=new GridBagLayout(); GridB
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);
}
问题是第一个面板(初始化为Cp,图片看起来不一样,标签移到左边,文本框又小又难看,这是不同的)。有人能帮忙吗,我对GridBagContsraints一点都不熟悉?快速浏览一下,有问题的面板上的所有ipadx值声明都是作为注释进行的。看起来不错的面板没有。删除//标记并查看是否有帮助:)快速浏览一下,有问题的面板将其所有ipadx值声明作为注释。看起来不错的面板没有。删除//标记并查看是否有帮助:)通常,当我使用weightx或weighty时,我也使用填充约束:gc.fill=GridBagConstraint.Horizontal强> 例如,以下代码将使组件占用50%的可用水平空间
gc.weightx = 0.5;
gc.fill = GridBagConstraint.Horizontal;
实际上我不能测试你的代码,但是试试看,它会解决你的问题。您还应该尝试调用pack()在父构造函数的末尾(我想是JFrame)。通常当我使用weightx或weighty时,我也使用fill约束:gc.fill=GridBagConstraint.Horizontal强> 例如,以下代码将使组件占用50%的可用水平空间
gc.weightx = 0.5;
gc.fill = GridBagConstraint.Horizontal;
实际上我不能测试你的代码,但是试试看,它会解决你的问题。您还应该尝试调用pack()在父构造函数的末尾(我想是JFrame)。我对您深表同情
我对你深表同情
谢谢您的快速回复。我试过了,在我把论坛(我试图改变面板的大小,我混合,但并没有thelp),标签再次在左边。如何在良好面板中定位标签?这是什么标签?谢谢你的快速回复。我试过了,在我把论坛(我试图改变面板的大小,我混合,但并没有thelp),标签再次在左边。如何在良好面板中定位标签?标签是什么?设置填充和设置重量。完全正确。设置填充,设置重量。确切地