java 在 JTextpane 中切换文本换行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4702891/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Toggling text wrap in a JTextpane
提问by GlassGhost
How would I go about toggling text wrap on a JTextpane
?
我将如何在 上切换文本换行JTextpane
?
public JFrame mainjFrame = new JFrame("Text Editor");
public JTextPane mainJTextPane = new JTextPane();
public JScrollPane mainJScrollPane = new JScrollPane(mainJTextPane);
mainjFrame.add(mainJScrollPane);
回答by camickr
See No Wrap Text Pane.
请参阅无换行文本窗格。
Edit:
编辑:
Well, if you want to toggle the behaviour, then you would also need to toggle the getScrollableTracksViewportWidth() value. See Scrollable Panel. You should be able to toggle between FIT and STRETCH.
好吧,如果您想切换行为,那么您还需要切换 getScrollableTracksViewportWidth() 值。请参阅可滚动面板。您应该能够在 FIT 和 STRETCH 之间切换。
回答by Stefanos Kalantzis
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);
}
}
I don't know any other way for what you are looking for..
我不知道您正在寻找的任何其他方式..
But this is working well.
但这运行良好。
( Based on camickr's answer.. +1 )
(基于camickr的回答..+1)