如何在java中使我的textfield更大的GUI

时间:2014-08-26 19:26:13

标签: java swing jtextfield layout-manager grid-layout

我有一个文本字段,如emailid,城市和地址,

我想让地址文本字段应该更大,如图所示!

我使用BorderLayout作为默认选项并使用GridLayout作为选定面板

as shown in figure

top.setLayout(new GridLayout(8,4,10,10));
top.add(emailid);
top.add(temailid);
top.add(address);
top.add(taddress);
add(top, BorderLayout.CENTER);

5 个答案:

答案 0 :(得分:3)

不使用JTextField,而是使用JTextArea,它旨在处理多行文字。

有关详细信息,请参阅How to use text areas

在任何情况下,您都应该提供columns(如果是JTextArearows值。 API有一种机制,用于“猜测”根据当前字体属性满足这些要求所需的空间量。

这允许API向布局管理器API提供大小调整提示,然后可以使用它来确定如何最好地定位/调整组件的大小。

要允许文本区域跨越多列,我建议您使用GridBagLayout

有关详细信息,请参阅How to use GridBagLayout

虽然有许多布局管理器,GridBagLayout可能是核心API中最灵活的(MigLayout也是另一个考虑因素)。这为您提供了最大程度的控制,可以确定每个组件的位置和大小。

例如......

Form

import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test1 {

    public static void main(String[] args) {
        new Test1();
    }

    public Test1() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private JTextField email;
        private JTextField city;
        private JTextArea address;

        public TestPane() {
            email = new JTextField(10);
            city = new JTextField(10);
            address = new JTextArea(5, 20);
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.insets = new Insets(4, 4, 4, 4);
            gbc.anchor = GridBagConstraints.WEST;
            add(new JLabel("Email ID:"), gbc);
            gbc.gridx++;
            add(email, gbc);
            gbc.gridx++;
            add(new JLabel("City:"), gbc);
            gbc.gridx++;
            add(city, gbc);

            gbc.gridx = 0;
            gbc.gridy++;
            gbc.anchor = GridBagConstraints.NORTHWEST;
            add(new JLabel("Address:"), gbc);

            gbc.gridx++;
            gbc.gridwidth = 3;
            gbc.fill = GridBagConstraints.HORIZONTAL;
            add(new JScrollPane(address), gbc);
        }

    }

}

答案 1 :(得分:0)

假设您想要增加文本字段的宽度,则有不同的方法。

一种非常简单的方法使用JTextField's constructor

JTextField email = new JTextField(25); //indicate how many columns you need

但是,最好的方法是BorderLayout,其中地址文本字段居中:

JPanel northPane = new JPanel(new BorderLayout(5, 5)); //add this panel right above your text area
JPanel eastPane = new JPanel(new BorderLayout(5, 5));
JTextField email = new JTextField(); //will take all the available space, so you don't need arguments
JLabel cityLabel = new JLabel("City:");
JTextField city = new JTextField(8); //should take a few characters
northPane.add(email, BorderLayout.CENTER);
northPane.add(eastPane, BorderLayout.EAST);
eastPane.add(cityLabel, BorderLayout.WEST);
eastPane.add(city, BorderLayout.CENTER);

这样,您的地址字段将使用所有可用空间。

答案 2 :(得分:0)

组件的大小由LayoutManager确定,其中最小最大首选大小为组件(请参阅JComponent上的方法)。

某些布局管理器可以为每个组件提供布局提示,这些提示取决于所使用的布局管理器。

组件根据显示的信息计算首选大小,以及插图,边框,字体度量等装饰设置。设置组件的首选大小通常是不好的做法,这可能仅适用于特殊情况(例如滚动窗格或图像)。

回到你的案例:JTextField允许用字符数提示其首选宽度(或:列,用最宽泛的字符测量,例如'M'),使用边框计算实际首选宽度字体度量信息。

为此,您可以使用以下构造函数/方法:

JTextField(int columns)
JTextField(String text, int columns)
setColumns(int columns)

答案 3 :(得分:0)

只需在

中创建一个面板即可
            top.add(emailid);
            Panel p5 = new Panel();
            p5.setLayout(new GridLayout(1,3,1,1));
            p5.add(temailid);
            p5.add(city);
            p5.add(tcity);
            top.add(p5);
            top.add(address);
            Panel p6 = new Panel();
            p6.setLayout(new GridLayout());
            p6.add(taddress);
            top.add(p6);

因为它占据了布局中的整个空间..

感谢您的所有答案,但我找到了最简单的方法

答案 4 :(得分:-2)

使用MiGLayout。这是我最近使用的唯一布局。恕我直言,它取代了它在那里的所有其他东西,非常直观和自我记录。