阻止 Java 的 JTextField 扩展以适应面板?

发布于 2025-01-07 22:45:18 字数 1086 浏览 1 评论 0原文

我正在制作一款角色扮演游戏来练习我的新手编程能力。我想在 1x4 网格中创建一堆用于角色创建的选项,并在下面有一个确认按钮。

我的问题是,我希望 JTextField 不再看起来像它被设计为消耗世界上可能拥有的任何和所有文本。请参阅此处:

[好吧,我还不能发布图像。]

但它是一个巨大的文本字段,我相信这是它试图填充整个面板的方式。我只是希望它与旁边的 JLabel 的高度大致相同。

我已经尝试过 setBounds()、setSize()、setPreferredSize()...如何使该文本字段的大小适当?

编辑:抱歉没有发布相关代码:

package practice;

import java.awt.GridLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Main {

    /**
     * @param args
     */
    public static void main(String[] args) {
        JFrame failFrame = new JFrame();
        JPanel failPanel = new JPanel();
        failPanel.setLayout(new GridLayout(0, 4));

        failPanel.add(new JTextField());
        failPanel.add(new JPanel());
        failPanel.add(new JTextField());
        failPanel.add(new JPanel());

        failFrame.add(failPanel);
        failFrame.setSize(800, 800);
        failFrame.setLocationRelativeTo(null);
        failFrame.setVisible(true);
    }

}

这会导致文本字段比我想要的高得多。是否可以使用 gridLayout 并降低其高度?

I'm making an RPG to practice my novice programming abilities. I want to create a bunch of options for character creation in a 1x4 grid, with a confirm button underneath.

My problem here is that I want the JTextField to stop looking like it was designed to consume any and all text the world might possess. See here:

[Okay, well I can't post images yet.]

But it's a giant text field, which I believe is its way of trying to fill the entire panel. I just want to it be roughly the same height as the JLabel next to it.

I've tried setBounds(), setSize(), setPreferredSize()...how can I make that text field appropriately sized?

Edit: Sorry for not posting the relevant code:

package practice;

import java.awt.GridLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Main {

    /**
     * @param args
     */
    public static void main(String[] args) {
        JFrame failFrame = new JFrame();
        JPanel failPanel = new JPanel();
        failPanel.setLayout(new GridLayout(0, 4));

        failPanel.add(new JTextField());
        failPanel.add(new JPanel());
        failPanel.add(new JTextField());
        failPanel.add(new JPanel());

        failFrame.add(failPanel);
        failFrame.setSize(800, 800);
        failFrame.setLocationRelativeTo(null);
        failFrame.setVisible(true);
    }

}

This causes the text fields to be far taller than I want them to be. Is it possible to use a gridLayout and make their height lower?

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

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

发布评论

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

评论(1

山田美奈子 2025-01-14 22:45:18

也许布局管理器可以提供帮助。如果文本字段和标签应该具有相同的大小,您可以考虑网格布局。

看:
http://docs.oracle.com/javase/tutorial/uiswing/layout /视觉.html

Maybe a LayoutManager could help. If the textfield and the label should have the same size you could think about a gridlayout.

see:
http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文