我在试着让JTextPane自动换行。我搜索了这个网站和整个互联网,似乎默认情况下JTextPane应该是自动换行的--大多数人遇到的问题都是在JScrollPane中禁用换行或让换行正常工作。我尝试了TextPanes、ScrollPanes和JPanels的各种组合,但都没有用。下面是测试过的最简单的代码,它仍然有这个问题(没有包装)。
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);
}
}发布于 2013-02-01 06:36:46
根据布局的不同,JTextPane可能会包装,也可能不包装,这取决于它感知到的可用大小。
相反,将JTextPane添加到JScrollPane ...
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);
}
}使用其他示例更新了
试试这个吧。这对我很有效。
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));
}
}
}发布于 2020-01-30 00:36:53
至少在我的例子中,下面的代码是有效的。如果您将JTextPane添加到具有边框布局的JPane中,并将该窗格添加到JScrollPane中,则行将不会换行。
发布于 2021-03-17 04:58:53
我使用了JTextArea而不是JTextPane
然后使用方法:
textArea.setLineWrap(true);https://stackoverflow.com/questions/14636186
复制相似问题