根据您的布局,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));
}
}
}