简体   繁体   English

JTextArea在创建文本区域时只包装一次

[英]JTextArea wrapping only once at the creation of the text area

I have a JTextArea with 我有一个JTextArea

text.setLineWrap(true);
text.setWrapStyleWord(true);

Now I have the problem, that if I start the GUI containing some of this JTextArea the text is correctly wrapped into 3-4 lines. 现在,我遇到的问题是,如果启动包含某些JTextAreaGUI ,则文本将正确地包装为3-4行。 Now I resize the GUI to the rigth and the text is correctly expanded and just wrapped down to 1-2 lines. 现在,我将GUI调整为正确的大小,并且文本已正确扩展,并且仅缩减为1-2行。 Now I start resizing the GUI back to the left, but the JTextArea's don't wrap back to the old state. 现在,我开始将GUI调整为左侧,但是JTextArea's状态不会退回到原来的状态。 They just stay wrapped to the 1-2 lines. 他们只是停留在1-2行。

What kind of layout you are using? 您正在使用哪种布局? You need to use one that fits on the size of the window. 您需要使用一个适合窗口大小的窗口。

public static void main(String[] args) {
    StringBuilder sb = new StringBuilder();
    Locale[] locales = Locale.getAvailableLocales();
    for (int i = 0; i < locales.length; i++) {
        sb.append(locales[i].getDisplayCountry()).append(' ');
    }

    JTextArea textArea = new JTextArea(sb.toString());
    textArea.setLineWrap(true);
    textArea.setWrapStyleWord(true);

    JScrollPane scrollPane = new JScrollPane();
    scrollPane.setViewportView(textArea);

    JFrame frame = new JFrame("All installed locales");
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.add(scrollPane);
    frame.pack();
    frame.setVisible(true);
}

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

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