如何在Java JTextPane中禁用WordWrap?
我的程序有问题。。这是WordWrap已启用 没错,我想用linwrap,但我不想用wordwrap 我搜索了这个如何在Java JTextPane中禁用WordWrap?,java,swing,jtextpane,word-wrap,Java,Swing,Jtextpane,Word Wrap,我的程序有问题。。这是WordWrap已启用 没错,我想用linwrap,但我不想用wordwrap 我搜索了这个 JTextPane textPane = new JTextPane(); JPanel noWrapPanel = new JPanel( new BorderLayout() ); noWrapPanel.add( textPane ); JScrollPane scrollPane = new JScrollPane( noWrapPanel ); 当我使用此代码时,换行和
JTextPane textPane = new JTextPane();
JPanel noWrapPanel = new JPanel( new BorderLayout() );
noWrapPanel.add( textPane );
JScrollPane scrollPane = new JScrollPane( noWrapPanel );
当我使用此代码时,换行和换行都被禁用
我想使用换行
对不起,我糟糕的英语技能。。。
我相信你能理解我的意思
请帮帮我…这是:
//覆盖getScrollableTracksViewportWidth
//保留文本的全宽
公共布尔值getScrollableTracksViewportWidth(){
组件父级=getParent();
ComponentUI=getUI();
返回父项!=null?(ui.getPreferredSize(this)。宽度请尝试此操作
JTextPane textPane;
public void someMethod() {
textPane = new JTextPane(new DefaultStyledDocument());
textPane.setEditorKit(new ExtendedStyledEditorKit());
}
/** To enable no wrap to JTextPane **/
static class ExtendedStyledEditorKit extends StyledEditorKit {
private static final long serialVersionUID = 1L;
private static final ViewFactory styledEditorKitFactory = (new StyledEditorKit()).getViewFactory();
private static final ViewFactory defaultFactory = new ExtendedStyledViewFactory();
public Object clone() {
return new ExtendedStyledEditorKit();
}
public ViewFactory getViewFactory() {
return defaultFactory;
}
/* The extended view factory */
static class ExtendedStyledViewFactory implements ViewFactory {
public View create(Element elem) {
String elementName = elem.getName();
if (elementName != null) {
if (elementName.equals(AbstractDocument.ParagraphElementName)) {
return new ExtendedParagraphView(elem);
}
}
// Delegate others to StyledEditorKit
return styledEditorKitFactory.create(elem);
}
}
}
static class ExtendedParagraphView extends ParagraphView {
public ExtendedParagraphView(Element elem) {
super(elem);
}
@Override
public float getMinimumSpan(int axis) {
return super.getPreferredSpan(axis);
}
}
我相信你能理解我的意思,请帮帮我…
-不是真的。在Window7上使用JDK7我不会得到“word wrap”这是一个完整的单词在包装发生时被移到一个新的行,我认为这是正确的行为。也许我不理解单词包的意思。如果你想让文本在单词的中间包起来,那么你可以使用jTeTaReA。否则你可以发布一个演示你的问题的帖子。
JTextPane textPane;
public void someMethod() {
textPane = new JTextPane(new DefaultStyledDocument());
textPane.setEditorKit(new ExtendedStyledEditorKit());
}
/** To enable no wrap to JTextPane **/
static class ExtendedStyledEditorKit extends StyledEditorKit {
private static final long serialVersionUID = 1L;
private static final ViewFactory styledEditorKitFactory = (new StyledEditorKit()).getViewFactory();
private static final ViewFactory defaultFactory = new ExtendedStyledViewFactory();
public Object clone() {
return new ExtendedStyledEditorKit();
}
public ViewFactory getViewFactory() {
return defaultFactory;
}
/* The extended view factory */
static class ExtendedStyledViewFactory implements ViewFactory {
public View create(Element elem) {
String elementName = elem.getName();
if (elementName != null) {
if (elementName.equals(AbstractDocument.ParagraphElementName)) {
return new ExtendedParagraphView(elem);
}
}
// Delegate others to StyledEditorKit
return styledEditorKitFactory.create(elem);
}
}
}
static class ExtendedParagraphView extends ParagraphView {
public ExtendedParagraphView(Element elem) {
super(elem);
}
@Override
public float getMinimumSpan(int axis) {
return super.getPreferredSpan(axis);
}
}