gpt4 book ai didi

java - GridBagConstraints问题-向左移动且大小不一样

转载 作者:行者123 更新时间:2023-12-02 00:47:04 25 4
gpt4 key购买 nike

我在Java中有两个面板需要具有相同的布局,有我的初始化面板的函数。

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问题是第一个面板(initializetcp,图片看起来不一样,标签移到左侧,文本框又小又难看,它是不同的)。有人可以帮忙吗,我是 GridBagContsraints 的新手吗?

最佳答案

通常当我使用weightx或weighty时,我也会使用填充约束:gc.fill = GridBagConstraint.Horizo​​ntal;

例如,以下代码将使您的组件占用 50% 的可用水平空间。

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

我实际上无法测试你的代码,但是尝试一下,它应该可以解决你的问题。您还应该尝试在父构造函数的末尾调用 pack(); (我想是 JFrame)。

关于java - GridBagConstraints问题-向左移动且大小不一样,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4680764/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com