如何在运行时设置JTextField的宽度? [英] How to set the width of a JTextField at runtime?

查看:444
本文介绍了如何在运行时设置JTextField的宽度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮助我如何在运行时设置JTextField的宽度吗?我希望在运行时调整文本字段的大小.它将询问用户长度,然后输入将更改文本字段的宽度.

Can someone please help me how to set the width of a JTextField at runtime? I want my text field to be resized on runtime. It will ask the user for the length, then the input will change the width of the text field.

if(selectedComponent instanceof javax.swing.JTextField){
    javax.swing.JTextField txtField = (javax.swing.JTextField) selectedComponent;
    //txtField.setColumns(numInput); //tried this but it doesn't work
    //txtField.setPreferredSize(new Dimension(numInput, txtField.getHeight())); //also this
    //txtField.setBounds(txtField.getX(), txtField.getY(), numInput, txtField.getHeight()); 
    //and this
    txtField.revalidate();
}

由于我处于编辑模式,因此我正在使用null布局.

I am using null layout for this, since I'm on edit mode.

推荐答案

您只需要使用

You simply need to use jTextFieldObject.setColumns(int columnSize). This will let you increase it's size at runtime. The reason why you couldn't do it at your end is the null Layout. That is one of the main reasons why the use of null Layout/Absolute Positioning is discouraged. Here is a small example for trying your hands on :

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class JTextFieldExample
{
    private JFrame frame;
    private JPanel contentPane;
    private JTextField tfield;
    private JButton button;
    private int size = 10;

    private ActionListener action = new ActionListener()
    {
        public void actionPerformed(ActionEvent ae)
        {
            String input = JOptionPane.showInputDialog(
                                frame, "Please Enter Columns : "
                                                , String.valueOf(++size));
            tfield.setColumns(Integer.parseInt(input));                 
            contentPane.revalidate();
            contentPane.repaint();
        }
    };

    private void createAndDisplayGUI()
    {
        frame = new JFrame("JTextField Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);           

        contentPane = new JPanel();
        contentPane.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));

        tfield = new JTextField();
        tfield.setColumns(size); 

        JButton  button = new JButton("INC Size");
        button.addActionListener(action);

        contentPane.add(tfield);
        contentPane.add(button);

        frame.getContentPane().add(contentPane);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new JTextFieldExample().createAndDisplayGUI();
            }
        });
    }
}

对于绝对定位,您需要调用JTextField上使用rel ="nofollow noreferrer"> setSize() ,尽管您应始终牢记不鼓励使用此方法的原因,如

For absolute positioning you need to call setSize() on the JTextField in order to attain the result, though you should always keep in mind the reason why this approach is discouraged, as given in the Java Doc's first paragraph:

尽管可以不使用布局管理器,但应尽可能使用布局管理器.布局管理器使调整与外观依赖的组件外观,不同的字体大小,容器的变化大小以及不同的语言环境变得更加容易.布局管理器还可以很容易地被其他容器和其他程序重用.

Although it is possible to do without a layout manager, you should use a layout manager if at all possible. A layout manager makes it easier to adjust to look-and-feel-dependent component appearances, to different font sizes, to a container's changing size, and to different locales. Layout managers also can be reused easily by other containers, as well as other programs.

这篇关于如何在运行时设置JTextField的宽度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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