简体   繁体   English

阻止Java的JTextField扩展到适合面板?

[英]Stop Java's JTextField from expanding to fit panel?

I'm making an RPG to practice my novice programming abilities. 我正在制作RPG来练习我的新手编程能力。 I want to create a bunch of options for character creation in a 1x4 grid, with a confirm button underneath. 我想在1x4网格中创建一堆用于字符创建的选项,并在其下方有一个确认按钮。

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. 我在这里的问题是,我希望JTextField不再看起来像它设计来消耗世界可能拥有的所有文本。 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. 我只想使其高度与旁边的JLabel高度相同。

I've tried setBounds(), setSize(), setPreferredSize()...how can I make that text field appropriately sized? 我已经尝试过setBounds(),setSize(),setPreferredSize()...如何使文本字段的大小合适?

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? 是否可以使用gridLayout并降低其高度?

Maybe a LayoutManager could help. 也许LayoutManager可以提供帮助。 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 参见: http : //docs.oracle.com/javase/tutorial/uiswing/layout/visual.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM