首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JTextPane不会包装

JTextPane不会包装
EN

Stack Overflow用户
提问于 2013-02-01 06:18:52
回答 3查看 1.7K关注 0票数 1

我在试着让JTextPane自动换行。我搜索了这个网站和整个互联网,似乎默认情况下JTextPane应该是自动换行的--大多数人遇到的问题都是在JScrollPane中禁用换行或让换行正常工作。我尝试了TextPanes、ScrollPanes和JPanels的各种组合,但都没有用。下面是测试过的最简单的代码,它仍然有这个问题(没有包装)。

代码语言:javascript
复制
public class Looseleaf extends JFrame{

    public Looseleaf(){
        this.setSize(200,200);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        JTextPane txtPane = new JTextPane();
        this.add(txtPane);
        this.setVisible(true);
    }
}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-02-01 06:36:46

根据布局的不同,JTextPane可能会包装,也可能不包装,这取决于它感知到的可用大小。

相反,将JTextPane添加到JScrollPane ...

代码语言:javascript
复制
public class Looseleaf extends JFrame{
    public Looseleaf(){
        this.setSize(200,200);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        JTextPane txtPane = new JTextPane();
        this.add(new JScrollPane(txtPane)); // <-- Add the text pane to a scroll pane....
        this.setVisible(true);
    }
}

使用其他示例更新了

试试这个吧。这对我很有效。

代码语言:javascript
复制
public class TestTextPaneWrap {

    public static void main(String[] args) {
        new TestTextPaneWrap();
    }

    public TestTextPaneWrap() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception ex) {
                }

                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);

            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new BorderLayout());
            JTextPane editor = new JTextPane();
            editor.setMinimumSize(new Dimension(0, 0));
            add(new JScrollPane(editor));
        }

    }
}
票数 3
EN

Stack Overflow用户

发布于 2020-01-30 00:36:53

至少在我的例子中,下面的代码是有效的。如果您将JTextPane添加到具有边框布局的JPane中,并将该窗格添加到JScrollPane中,则行将不会换行。

票数 0
EN

Stack Overflow用户

发布于 2021-03-17 04:58:53

我使用了JTextArea而不是JTextPane

然后使用方法:

代码语言:javascript
复制
textArea.setLineWrap(true);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14636186

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档