JTextpane 不尊重 java 8 的 visibleEditorRect
JTextpane does not respect visibleEditorRect with java 8
运行 下面的代码都是 java 6 和 java 8,看看结果。在 java 6 上,换行符在 visibleEditorRect 的边界内工作,但在 java 8 上,字符串溢出边界。有没有解决这个问题的方法。
import java.awt.Rectangle;
import javax.swing.JFrame;
import javax.swing.JTextPane;
import javax.swing.plaf.basic.BasicTextPaneUI;
public class TextPaneBug {
public static void main(String[] args) {
JFrame f = new JFrame() ;
JTextPane text = new BugTextPane() ;
f.add(text);
text.setText("mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm");
f.setExtendedState(JFrame.MAXIMIZED_BOTH);
f.setVisible(true);
}
static class BugTextPane extends JTextPane
{
public BugTextPane() {
setUI(new BasicTextPaneUI(){
@Override
protected Rectangle getVisibleEditorRect() {
Rectangle r = super.getVisibleEditorRect() ;
Rectangle newr = new Rectangle(r.width / 2 - 300 , r.height/2 - 300 , 600 ,600) ;
return newr;
}
protected void paintSafely(java.awt.Graphics g) {
super.paintSafely(g);
Rectangle r = getVisibleEditorRect() ;
g.drawRect(r.x,r.y,r.width,r.height);
};
}
);
}
}
}
我通过将中断行为恢复为 java 6 功能解决了这个问题。
通过在 LabelView 的扩展中从 java 6 复制来覆盖 getBreakWeight
和 getBreakSpot
方法就足够了。
运行 下面的代码都是 java 6 和 java 8,看看结果。在 java 6 上,换行符在 visibleEditorRect 的边界内工作,但在 java 8 上,字符串溢出边界。有没有解决这个问题的方法。
import java.awt.Rectangle;
import javax.swing.JFrame;
import javax.swing.JTextPane;
import javax.swing.plaf.basic.BasicTextPaneUI;
public class TextPaneBug {
public static void main(String[] args) {
JFrame f = new JFrame() ;
JTextPane text = new BugTextPane() ;
f.add(text);
text.setText("mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm");
f.setExtendedState(JFrame.MAXIMIZED_BOTH);
f.setVisible(true);
}
static class BugTextPane extends JTextPane
{
public BugTextPane() {
setUI(new BasicTextPaneUI(){
@Override
protected Rectangle getVisibleEditorRect() {
Rectangle r = super.getVisibleEditorRect() ;
Rectangle newr = new Rectangle(r.width / 2 - 300 , r.height/2 - 300 , 600 ,600) ;
return newr;
}
protected void paintSafely(java.awt.Graphics g) {
super.paintSafely(g);
Rectangle r = getVisibleEditorRect() ;
g.drawRect(r.x,r.y,r.width,r.height);
};
}
);
}
}
}
我通过将中断行为恢复为 java 6 功能解决了这个问题。
通过在 LabelView 的扩展中从 java 6 复制来覆盖 getBreakWeight
和 getBreakSpot
方法就足够了。