在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/