简体   繁体   English

Java JTextField窗口大小

[英]Java JTextField window size

Im trying to display to the user some text: 我试图向用户显示一些文本:

JTextField warningComponent = new JTextField(VERY_LONG_TEXT_NOENTERS);
warningComponent.setEditable(false);

but the window size is changed according to the text size. 但是窗口大小根据文本大小而改变。 I want to set the window to be 30 X 40 all the time regardless of the warning text length. 我希望始终将窗口设置为30 X 40,而不考虑警告文本的长度。 And i want the warning text to be adjusted to the window size (maybe the user will have to scroll to see the end) How do i do it? 而且我希望将警告文本调整为窗口大小(也许用户必须滚动才能看到结尾),我该怎么做? Maybe i should use other swing component? 也许我应该使用其他秋千组件? I tried most of the methods in JTextField class. 我尝试了JTextField类中的大多数方法。

Thanks. 谢谢。

I add it to JPanel 我将其添加到JPanel

Then the default layout manager should be a FlowLayout which will respect the preferred size of the text field. 然后,默认布局管理器应为FlowLayout,它将遵循文本字段的首选大小。

To give a suggestion for the preferred size of the text field you need to do: 要为文本字段的首选大小提供建议,您需要执行以下操作:

JTextField warningComponent = new JTextField(VERY_LONG_TEXT_NOENTERS, 20);

The second parameter will give a suggestion on how to size the text field to make approximately 20 characters visible at one time. 第二个参数将建议如何调整文本字段的大小以一次显示大约20个字符。 You will then need to use the arrow keys to see the remaining characters. 然后,您将需要使用箭头键查看其余字符。

And i want the warning text to be adjusted to the window size 我希望将警告文本调整为窗口大小

If you want the textfield to resize according to the frame size and not the frame size following the dimension of the textfield, you may make use of specific layout to achieve that: 如果您希望文本框根据框架尺寸而不是根据文本框尺寸的尺寸调整大小,则可以使用特定的布局来实现:

在此处输入图片说明

Using BorderLayout: 使用BorderLayout:

class MainPanel extends JPanel{

    private JTextField txt;

    public MainPanel(){
        setLayout(new BorderLayout());
        setPreferredSize(new Dimension(30, 40));
        txt = new JTextField();
        txt.setHorizontalAlignment(JTextField.HORIZONTAL);
        add(txt);
    }
}

Runner class to run to codes: Runner类要运行的代码:

class TextFieldRunner{
    public static void main(String[] args){
       SwingUtilities.invokeLater(new Runnable(){
            @Override
            public void run(){
                JFrame frame = new JFrame("Text Runner");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       
                frame.add(new MainPanel());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);             
            }
        });
    }
}

You can try JScrollPane in your application, JScrollPane is scroll-able horizontally and vertically as you wish. 您可以在应用程序中尝试JScrollPaneJScrollPane可以根据需要水平和垂直滚动。 And add JTextArea to JScrollPane . 并将JTextArea添加到JScrollPane JTextField is not scroll-able. JTextField不可滚动。

Here is a small example: 这是一个小例子:

JScrollPane scrollPane = new JScrollPane();
JTextArea textArea = new JTextArea();
scrollPane.add(textArea);
scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

This is the values for the scroll policy: 这是滚动策略的值:

VERTICAL_SCROLLBAR_AS_NEEDED
VERTICAL_SCROLLBAR_NEVER
VERTICAL_SCROLLBAR_ALWAYS
HORIZONTAL_SCROLLBAR_AS_NEEDED
HORIZONTAL_SCROLLBAR_NEVER
HORIZONTAL_SCROLLBAR_ALWAYS

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

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