Java JTextPane换行符

Java JTextPane换行符,java,swing,line,word-wrap,jtextpane,Java,Swing,Line,Word Wrap,Jtextpane,《问询者》说,这是一个好消息 当文本超过宽度时,JTextPane确实具有换行符 情况似乎并非如此 scrollPane = new JScrollPane(); scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); this.contentPane.add(scrollPane); txtrFghj = new JTextPane(); txtrFghj.setBor

《问询者》说,这是一个好消息

当文本超过宽度时,JTextPane确实具有换行符

情况似乎并非如此

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);
    }
}
import javax.swing.JFrame;
导入javax.swing.JScrollPane;
导入javax.swing.JTextPane;
导入javax.swing.SwingUtilities;
公共类TestLineWrap{
公共静态void main(字符串[]args){
SwingUtilities.invokeLater(新的Runnable(){
@凌驾
公开募捐{
新的TestLineWrap().initUI();
}
});
}
受保护的void initUI(){
JFrame=新JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTextPane editorPane=新的JTextPane();
setContentType(“text/html”);
二吗啉
.setText(“如果屏幕太小,则无法在一行上保存的长文本”);
JScrollPane scrollPane=新的JScrollPane(editorPane);
滚动窗格
.setHorizontalScrollBarPolicy(JScrollPane.HorizontalScrollBar\u NEVER);
frame.add(滚动窗格);
框架设置尺寸(200400);
frame.setVisible(true);
}
}

您的代码中一定有其他东西阻止此功能正常工作

下面是一个小的演示示例,其代码与正常运行的代码相同:

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);
    }
}
import javax.swing.JFrame;
导入javax.swing.JScrollPane;
导入javax.swing.JTextPane;
导入javax.swing.SwingUtilities;
公共类TestLineWrap{
公共静态void main(字符串[]args){
SwingUtilities.invokeLater(新的Runnable(){
@凌驾
公开募捐{
新的TestLineWrap().initUI();
}
});
}
受保护的void initUI(){
JFrame=新JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTextPane editorPane=新的JTextPane();
setContentType(“text/html”);
二吗啉
.setText(“如果屏幕太小,则无法在一行上保存的长文本”);
JScrollPane scrollPane=新的JScrollPane(editorPane);
滚动窗格
.setHorizontalScrollBarPolicy(JScrollPane.HorizontalScrollBar\u NEVER);
frame.add(滚动窗格);
框架设置尺寸(200400);
frame.setVisible(true);
}
}

多亏了纪尧姆,我才弄明白,不过我花了三个小时才意识到:断线确实在这里起作用,但断线不起作用,正如问题中引用的

我的
JTextPane
内容如下所示:

<html>
   <body>
    <br>
    <font color="red">bla bla bla bla</font>\r\n
    <u>someVeeeeeeeeeeeeeryLooooongString__WithOUTanySpacesInBetweeeeeeeeeeeeeeeeeeeeeeeeen</u>
    <b>more text</b>
    // ........ a lot of more HTML
    Some funny SENTENCE which is longer than JTextPane.getSize().width usually allows. This sentence was NOT LINE WRAPPED which made me ask the question.
   </body>
</html>

多亏了纪尧姆,我明白了这一点,我花了三个小时才意识到:断线确实非常有效,但断线不起作用,正如问题中引用的

我的
JTextPane
内容如下所示:

<html>
   <body>
    <br>
    <font color="red">bla bla bla bla</font>\r\n
    <u>someVeeeeeeeeeeeeeryLooooongString__WithOUTanySpacesInBetweeeeeeeeeeeeeeeeeeeeeeeeen</u>
    <b>more text</b>
    // ........ a lot of more HTML
    Some funny SENTENCE which is longer than JTextPane.getSize().width usually allows. This sentence was NOT LINE WRAPPED which made me ask the question.
   </body>
</html>

txtrFghj.setPreferredSize(新尺寸(500,50))或诸如此类。如果需要更多包装逻辑的自定义,请参阅html的字母包装这可能很有用
txtrFghj.setPreferredSize(新维度(500,50))之类。如果您需要更多的包装逻辑自定义,请参阅html的字母包装,这可能会很有用