如何控制 Java Swing 中 JTextField 的宽度?

发布于 2024-09-02 17:05:24 字数 1408 浏览 7 评论 0原文

我试图在一行上有多个 JTextField,但我不希望它们具有相同的宽度。如何控制宽度并使其中一些比其他更宽?我希望它们一起占据总宽度的 100%,所以如果我可以使用某种加权就更好了。

我尝试过 .setColumns() 但它没有意义。

这是一个示例,其中我使用三行和三个字符串,这些字符串应显示在列中:

import java.awt.GridLayout;
import javax.swing.BoxLayout;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class RowTest extends JPanel {

    class Row extends JComponent {
        public Row(String str1, String str2, String str3) {
            this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));

            JTextField fld1 = new JTextField(str1);
            JTextField fld2 = new JTextField(str2);
            JTextField fld3 = new JTextField(str3);

            fld1.setColumns(5); // makes no sense

            this.add(fld1);
            this.add(fld2);
            this.add(fld3);
        }
    }

    public RowTest() {
    this.setLayout(new GridLayout(5,0));

    this.add(new Row("Short", "A long text that takes up more space",
        "Short again"));
    this.add(new Row("Longer but short", "Another long string", "Short"));
    this.add(new Row("Hello", "The long field again",
        "Some info"));
    }

    public static void main(String[] args) {
        new JFrame() {{ this.getContentPane().add(new RowTest());
                            this.pack(); this.setVisible(true); }};
    }

}

I am trying to have several JTextFields on a single row, but I don't want them to have the same width. How can I control the width and make some of them wider than others? I want that they together take up 100% of the total width, so it would be good if I could use some kind of weigthing.

I have tried with .setColumns() but it doesn't make sense.

Here is an example, where I am using three rows with three strings that should appear as in columns:

import java.awt.GridLayout;
import javax.swing.BoxLayout;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class RowTest extends JPanel {

    class Row extends JComponent {
        public Row(String str1, String str2, String str3) {
            this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));

            JTextField fld1 = new JTextField(str1);
            JTextField fld2 = new JTextField(str2);
            JTextField fld3 = new JTextField(str3);

            fld1.setColumns(5); // makes no sense

            this.add(fld1);
            this.add(fld2);
            this.add(fld3);
        }
    }

    public RowTest() {
    this.setLayout(new GridLayout(5,0));

    this.add(new Row("Short", "A long text that takes up more space",
        "Short again"));
    this.add(new Row("Longer but short", "Another long string", "Short"));
    this.add(new Row("Hello", "The long field again",
        "Some info"));
    }

    public static void main(String[] args) {
        new JFrame() {{ this.getContentPane().add(new RowTest());
                            this.pack(); this.setVisible(true); }};
    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

还给你自由 2024-09-09 17:05:24
yourTextField = new JTextField("", 20);

如果你像这样启动它,它会将文本字段设置为空并有 20 列

yourTextField = new JTextField("", 20);

if you start it like this it sets the textfield to be empty and to have 20 collumns

娇纵 2024-09-09 17:05:24

试试这个:

textfield.setPreferredSize(new Dimension(int width, int height));

Try this:

textfield.setPreferredSize(new Dimension(int width, int height));
雪化雨蝶 2024-09-09 17:05:24

所有 Swing 组件都有一个首选大小。文本组件的首选大小基于组件的文本。因此,通常组件会按照其首选尺寸进行绘制。

所以我没有看到你的代码片段有问题。每个文本字段应具有不同的首选大小。此外,当调整框架大小时,宽度也会随之调整,因为 BoxLayout 会尝试将每个组件的大小调整为其最大/最小尺寸。

如果您需要更多帮助,请发布您的 SSCCE,它实际上说明了您遇到的尺寸问题。

编辑:

根据最新要求,您可以使用相对布局

All Swing components have a preferred size. The preferred size of a text component is based on the text of the component. So generally the component is painted at its preferred size.

So I don't see the problem with your code snippet. Each text field should have a different preferred size. Also, as the frame is resized the width will be adjusted since the BoxLayout will try to resize each component up to its maximum/minimum size.

If you need more help post your SSCCE that actually demonstrates the sizing problem you are experiencing.

Edit:

Based on the latest requirement you can use the Relative Layout.

一枫情书 2024-09-09 17:05:24
// 1. create JTextField
yourTextField = new JTextField();
// 2. store default height
int defaultHeight = yourTextField.getSize().getHeight();
// 3. set custom width and default height
yourTextField.setSize(new Dimension(yourWidth, defaultHeight));
// 1. create JTextField
yourTextField = new JTextField();
// 2. store default height
int defaultHeight = yourTextField.getSize().getHeight();
// 3. set custom width and default height
yourTextField.setSize(new Dimension(yourWidth, defaultHeight));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文