gpt4 book ai didi

java - JTextField 大小随 JLabel 文本长度变化

转载 作者:行者123 更新时间:2023-12-02 05:16:49 24 4
gpt4 key购买 nike

我想连续创建 3 个 JTextField,且大小均相等。但是我的第一个文本字段比其他两个字段宽。
每次添加每个组件并查看导致问题的原因后,我意识到如果我为 JLabel 输入长文本,就会发生此问题。如果我减少 JLabel JTextFields 中文本的长度,则会达到我想要的大小。这是我的代码

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.Math;
public class Calculator1 extends JFrame {

JTextField value1=new JTextField("0");
JTextField value2=new JTextField("0");
JTextField value3=new JTextField("0");
JLabel label1=new JLabel("Label1 size of JTextfields chnge with Label length");
JTextField value4=new JTextField("0");
JTextField value5=new JTextField("0");
JTextField value6=new JTextField("0");
JLabel label2=new JLabel("Label2");
JLabel label3=new JLabel("Label3");
JTextField value7=new JTextField("0");
public Calculator1(){

super("Calculator");

setSize(500,200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel=new JPanel();
GridBagLayout bag=new GridBagLayout();
getContentPane().add(panel);
panel.setLayout(bag);
GridBagConstraints c=new GridBagConstraints();

c.insets=new Insets(5,5,5,5);
c.fill=GridBagConstraints.HORIZONTAL;


c.gridx=0;
c.gridy=0;
c.weightx=0.0;
c.weighty=0.5;
c.gridwidth=1;
panel.add(label1,c);


c.gridx=0;
c.gridy=1;
c.gridwidth=5;
c.weightx=0.5;
c.weighty=0.5;
panel.add(value1,c);


c.gridx=5;
c.gridy=1;
panel.add(value2,c);


c.gridx=10;
c.gridy=1;
panel.add(value3,c);

c.gridwidth=1;
c.weightx=0.0;
c.weighty=0.5;
c.gridx=0;
c.gridy=3;
panel.add(label2,c);

c.gridwidth=5;
c.weightx=0.5;
c.weighty=0.5;
c.gridx=0;
c.gridy=4;
c.insets=new Insets(5,5,5,5);
panel.add(value4,c);


c.gridx=5;
c.gridy=4;
panel.add(value5,c);


c.gridx=10;
c.gridy=4;
panel.add(value6,c);

c.gridwidth=1;
c.weightx=0.0;
c.weighty=0.5;
c.gridx=0;
c.gridy=5;
panel.add(label3,c);

c.gridwidth=5;
c.weightx=0.5;
c.weighty=0.5;
c.gridx=0;
c.gridy=6;
panel.add(value7,c);


setVisible(true);
}
public static void main (String args[]){
Calculator1 cal=new Calculator1();
}

}

当我更改 JLabel label1 的文本长度时,如何阻止 JTextFields 更改大小

最佳答案

JLabel 设置 gridwidth = 3,为 JTextField 设置 1,如下所示:

public Calculator() {

super("Calculator");

setSize(500, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
GridBagLayout bag = new GridBagLayout();
getContentPane().add(panel);
panel.setLayout(bag);
GridBagConstraints c = new GridBagConstraints();

c.insets = new Insets(5, 5, 5, 5);
c.fill = GridBagConstraints.HORIZONTAL;

c.gridx = 0;
c.gridy = 0;
c.weightx = 0.0;
c.weighty = 0.5;
c.gridwidth = 3;
panel.add(label1, c);

c.gridx = 0;
c.gridy = 1;
c.gridwidth = 1;
c.weightx = 0.5;
c.weighty = 0.5;
panel.add(value1, c);

c.gridx = 1;
c.gridy = 1;
panel.add(value2, c);

c.gridx = 2;
c.gridy = 1;
panel.add(value3, c);

c.gridwidth = 3;
c.weightx = 0.0;
c.weighty = 0.5;
c.gridx = 0;
c.gridy = 3;
panel.add(label2, c);

c.gridwidth = 1;
c.weightx = 0.5;
c.weighty = 0.5;
c.gridx = 0;
c.gridy = 4;
c.insets = new Insets(5, 5, 5, 5);
panel.add(value4, c);

c.gridx = 1;
c.gridy = 4;
panel.add(value5, c);

c.gridx = 2;
c.gridy = 4;
panel.add(value6, c);

c.gridwidth = 3;
c.weightx = 0.0;
c.weighty = 0.5;
c.gridx = 0;
c.gridy = 5;
panel.add(label3, c);

c.gridwidth = 1;
c.weightx = 0.5;
c.weighty = 0.5;
c.gridx = 0;
c.gridy = 6;
panel.add(value7, c);

setVisible(true);
}

enter image description here

另请阅读 GridBagLayout 的教程

关于java - JTextField 大小随 JLabel 文本长度变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26865935/

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