java - JTextPane 换行符

标签 java swing line word-wrap jtextpane

this question , 询问者说

The JTextPane do have word wrap when text exceeded width

好像不是这样的

scrollPane = new JScrollPane();
scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

this.contentPane.add(scrollPane);

txtrFghj = new JTextPane();
txtrFghj.setBorder(null);
txtrFghj.setContentType("text/html");
txtrFghj.setText(content);

scrollPane.setViewportView(txtrFghj);

在给定的代码摘录中,content 的内容根本不会换行,它只是超出了窗口的可见大小。如果窗口不够大,长句是看不全的。

如何实现换行?

我试过了

txtrFghj.setSize(50,50);

但这根本不会改变任何行为。

最佳答案

您的代码中一定有其他东西阻止它正常工作。

这是一个小的演示示例,使用相同的代码运行得很好:

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.SwingUtilities;

public class TestLineWrap {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new TestLineWrap().initUI();
            }
        });

    }

    protected void initUI() {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JTextPane editorPane = new JTextPane();
        editorPane.setContentType("text/html");
        editorPane
                .setText("<html>Some long text that cannot hold on a single line if the screen is too small</html>");
        JScrollPane scrollPane = new JScrollPane(editorPane);
        scrollPane
        .setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        frame.add(scrollPane);
        frame.setSize(200, 400);
        frame.setVisible(true);
    }
}

关于java - JTextPane 换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27677123/

相关文章:

java - 全文搜索类似

Java Swing 右边框不适用于 Microsoft Windows

java - 如何编译svn仓库?

java - 从 JButton 菜单中单击 JLabel 时,图像不出现 (java)

android - 您应该如何使用带有 PathShape 的 ShapeDrawable 在自定义 View 上绘制线条?

java - Java 的弱密码套件列表

java - 更新文本区域 Swing Java

java - 在 JTable 中反转选择

c# - 在鼠标经过的地方画一条线

php - 在PHP中绘制两点之间的曲线