我如何在JTextpane
上切换文字换行?
public JFrame mainjFrame = new JFrame("Text Editor");
public JTextPane mainJTextPane = new JTextPane();
public JScrollPane mainJScrollPane = new JScrollPane(mainJTextPane);
mainjFrame.add(mainJScrollPane);
答案 0 :(得分:11)
编辑:
好吧,如果你想切换行为,那么你还需要切换getScrollableTracksViewportWidth()值。见Scrollable Panel。您应该能够在FIT和STRETCH之间切换。
答案 1 :(得分:9)
package test;
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
public class TestVisual extends JFrame {
private boolean wrapped;
private JButton toggleButton = null;
private JTextPane textPane = null;
private JPanel noWrapPanel = null;
private JScrollPane scrollPane = null;
public TestVisual() {
super();
init();
}
public void init() {
this.setSize(300, 200);
this.setLayout(new BorderLayout());
wrapped = false;
textPane = new JTextPane();
noWrapPanel = new JPanel( new BorderLayout() );
noWrapPanel.add( textPane );
scrollPane = new JScrollPane( noWrapPanel );
toggleButton = new JButton("wrap");
toggleButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
if (wrapped == true){
scrollPane.setViewportView(noWrapPanel);
noWrapPanel.add(textPane);
toggleButton.setText("wrap");
wrapped = false;
}else {
scrollPane.setViewportView(textPane);
toggleButton.setText("unWrap");
wrapped = true;
}
}
});
this.add(scrollPane, BorderLayout.CENTER);
this.add(toggleButton, BorderLayout.NORTH);
}
}
我不知道你正在寻找什么其他方式..
但这很有效。
(根据camickr的答案...... +1)