【问题标题】:JTextArea wrapping only once at the creation of the text areaJTextArea 仅在创建文本区域时换行一次
【发布时间】:2013-09-27 12:50:22
【问题描述】:

我有一个JTextArea

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

现在我遇到了一个问题,如果我启动包含其中一些 JTextAreaGUI,则文本会正确地包装成 3-4 行。现在我将GUI 的大小调整为右侧,文本正确展开并仅包含 1-2 行。现在我开始将GUI 重新调整到左侧,但JTextArea's 不会返回到旧状态。他们只是保持包裹到 1-2 行。

【问题讨论】:

    标签: java swing jtextarea word-wrap


    【解决方案1】:

    您使用的是哪种布局?您需要使用适合窗口大小的。

    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);
    }
    

    【讨论】:

    • @Vegas 我正在使用 GridBagLayout。
    • @Paul Vargas new JTextArea(int, int);
    猜你喜欢
    • 2014-12-01
    • 1970-01-01
    • 2012-02-06
    • 1970-01-01
    • 2017-03-16
    • 1970-01-01
    • 1970-01-01
    • 2014-10-01
    • 1970-01-01
    相关资源
    最近更新 更多