创建一个文本字段,以动态调整其宽度 [英] Creating a text field that dynamically resizes its width

查看:81
本文介绍了创建一个文本字段,以动态调整其宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作基本上是一个包含搜索字段和一些按钮的工具栏.我希望当父容器变宽时(例如当用户调整拆分窗格时),搜索字段的大小(仅宽度)会增加.当前,文本字段和按钮将保持相同的大小,并且在容器变宽时在两边都添加了空格.我可以通过在容器中使用BorderLayout并将按钮放在LINE_END上并将文本字段放在CENTER中来实现这种增长的效果.我的问题是文本字段现在变得比标准文本字段高,而且看起来很丑.这种行为是有道理的,因为BorderLayout管理器会将所有额外的空间(包括垂直和水平空间)分配给CENTER文本字段.我试图通过在文本字段上放置一个最大尺寸来限制这种垂直增长,但是BorderLayout不会这样做.

I'm making what is basically a toolbar that contains a search field and some buttons. I'd like for the search field to grow in size (just the width) when the parent container gets wider, like when the user adjusts the split pane. Currently, the text field and the buttons will remain the same size and whitespace is added on either side as the container is widened. I can achieve this growing effect by using a BorderLayout in the container and putting the buttons on LINE_END and the text field in the CENTER. The problem I have with this is that the text field now becomes taller than a standard text field and it looks ugly. This behavior makes sense as the BorderLayout manager will give all the extra space (this includes vertical and hortizontal space) to the CENTER text field. I've tried to restrict this vertical growth by placing a maximum size on the text field, but BorderLayout will not honor it.

这就是我所拥有的:

final JTextField searchField = new JTextField("Enter your search terms");
searchField.setMaximumSize(new Dimension(Integer.MAX_VALUE, 25));
final JPanel controls = new JPanel(new BorderLayout());
controls.add(searchField, BorderLayout.CENTER);
controls.add(new JPanel(){{
    add(new JButton("Search")); add(new JButton("I'm Feeling Lucky"));
}}, BorderLayout.LINE_END);

这种行为似乎是普遍希望的,并且应该易于实现,但是在浏览了所有Oracle/Sun教程和Google搜索结果后,我感到不走运.

It would seem that this behavior is a commonly desired one and it should be easy to implement, but I've had no luck after looking through all the Oracle/Sun tutorials and Google search results.

有人对此有任何解决方案吗?我需要坚持使用标准的Java Swing组件-请不要使用第三方库.

Anybody have any solutions to this? I need to stick with standard Java Swing components - no third party libraries please.

谢谢!

推荐答案

我建议您使用GridBagLayout,它很复杂,但是它是最强大的布局.当您学习它时,就不会有任何布局问题.

I would suggest you to use GridBagLayout , it is complicated but it is the most powerful layout.. When you learn it, you would not have any layout issue.

这是这个问题的gridbaglayout的示例用法...

Here is the sample use of gridbaglayout for this question...

import java.awt.BorderLayout;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

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

public class GridBagLayoutExample extends JFrame {

    GridBagLayoutExample() {
        initUI();
    }


    private void initUI() {
        final JTextField searchField = new JTextField("Enter your search terms"); 
        final JPanel controls = new JPanel(new GridBagLayout()); 
        GridBagConstraints c = new GridBagConstraints();
        c.weightx=1.0;
        c.fill=GridBagConstraints.HORIZONTAL;
        controls.add(searchField,c); 
        controls.add(new JPanel(){
            {     
            add(new JButton("Search")); add(new JButton("I'm Feeling Lucky")); }}); 
        setLayout(new BorderLayout());
        add(controls, BorderLayout.NORTH);
        pack();
    }


    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new GridBagLayoutExample().setVisible(true);

            }
        });
    }
}

这篇关于创建一个文本字段,以动态调整其宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆