ubuntu java swing jtextfield 插入符号自 java8 以来不可见(setXORMode 错误?)
ubuntu java swing jtextfield caret invisible since java8 (setXORMode bug ?)
为什么插入符号在 java8(oracle 和 openjdk)及更高版本下变得不可见(按下按键或向左箭头后)? (它在 java7 下工作)。我在 ubuntu。它在 mac-os jdk1.8.0_51 和 windows jdk1.8.0_65 上运行良好。
似乎与此 bug 相关联。它是 JTextField 特有的(不会出现在 JTextArea 上)。
public class OverWriteCaret extends DefaultCaret {
protected static final int MIN_WIDTH = 8;
private static final Logger logger = Logger.getLogger(OverWriteCaret.class.getName());
public static void main(String[] args) {
JFrame f = new JFrame("Big caret");
JTextField tf = new JTextField(20);
tf.setCaret(new OverWriteCaret());
f.getContentPane().add(tf, "North");
f.pack();
f.setVisible(true);
}
@Override
protected synchronized void damage(Rectangle r) {
if (r == null)
return;
try {
JTextComponent comp = getComponent();
TextUI mapper = comp.getUI();
Rectangle r2 = mapper.modelToView(comp, getDot() + 1);
int largeur = r2.x - r.x;
if (largeur == 0)
largeur = MIN_WIDTH;
comp.repaint(r.x, r.y, largeur, r.height);
this.x = r.x;
this.y = r.y;
this.width = largeur;
this.height = r.height;
} catch (Exception e) {
logger.info(e);
}
}
@Override
public void paint(Graphics g) {
if (isVisible())
try {
JTextComponent comp = getComponent();
TextUI mapper = comp.getUI();
Rectangle r1 = mapper.modelToView(comp, getDot());
Rectangle r2 = mapper.modelToView(comp, getDot() + 1);
g = g.create();
g.setColor(comp.getForeground());
g.setXORMode(comp.getBackground());
int largeur = r2.x - r1.x;
if (largeur == 0)
largeur = MIN_WIDTH;
g.fillRect(r1.x, r1.y, largeur, r1.height);
g.dispose();
} catch (Exception e) {
logger.info(e);
}
}
}
找到另一个插入符,可以了
public class FancyCaret extends DefaultCaret {
private static final Logger logger = Logger.getLogger(FancyCaret.class.getName());
protected synchronized void damage(Rectangle r) {
if (r == null)
return;
x = r.x;
y = r.y;
height = r.height;
if (width <= 0)
width = getComponent().getWidth();
repaint(); // calls getComponent().repaint(x, y, width, height)
}
@Override
public void paint(Graphics g) {
JTextComponent comp = getComponent();
if (comp == null)
return;
int dot = getDot();
Rectangle r;
char dotChar;
try {
r = comp.modelToView(dot);
if (r == null)
return;
dotChar = comp.getText(dot, 1).charAt(0);
} catch (Exception e) {
logger.info(e);
return;
}
if ((x != r.x) || (y != r.y)) {
repaint(); // erase previous location of caret
x = r.x; // Update dimensions (width gets set later in this method)
y = r.y;
height = r.height;
}
if (dotChar == '\n') {
width = r.height / 2;
if (isVisible())
g.fillRect(r.x, r.y, width, r.height);
return;
}
g.setColor(comp.getCaretColor());
g.setXORMode(comp.getBackground()); // do this to draw in XOR mode
width = g.getFontMetrics().charWidth(dotChar);
if (isVisible())
g.fillRect(r.x, r.y, width, r.height);
}
}
为什么插入符号在 java8(oracle 和 openjdk)及更高版本下变得不可见(按下按键或向左箭头后)? (它在 java7 下工作)。我在 ubuntu。它在 mac-os jdk1.8.0_51 和 windows jdk1.8.0_65 上运行良好。 似乎与此 bug 相关联。它是 JTextField 特有的(不会出现在 JTextArea 上)。
public class OverWriteCaret extends DefaultCaret {
protected static final int MIN_WIDTH = 8;
private static final Logger logger = Logger.getLogger(OverWriteCaret.class.getName());
public static void main(String[] args) {
JFrame f = new JFrame("Big caret");
JTextField tf = new JTextField(20);
tf.setCaret(new OverWriteCaret());
f.getContentPane().add(tf, "North");
f.pack();
f.setVisible(true);
}
@Override
protected synchronized void damage(Rectangle r) {
if (r == null)
return;
try {
JTextComponent comp = getComponent();
TextUI mapper = comp.getUI();
Rectangle r2 = mapper.modelToView(comp, getDot() + 1);
int largeur = r2.x - r.x;
if (largeur == 0)
largeur = MIN_WIDTH;
comp.repaint(r.x, r.y, largeur, r.height);
this.x = r.x;
this.y = r.y;
this.width = largeur;
this.height = r.height;
} catch (Exception e) {
logger.info(e);
}
}
@Override
public void paint(Graphics g) {
if (isVisible())
try {
JTextComponent comp = getComponent();
TextUI mapper = comp.getUI();
Rectangle r1 = mapper.modelToView(comp, getDot());
Rectangle r2 = mapper.modelToView(comp, getDot() + 1);
g = g.create();
g.setColor(comp.getForeground());
g.setXORMode(comp.getBackground());
int largeur = r2.x - r1.x;
if (largeur == 0)
largeur = MIN_WIDTH;
g.fillRect(r1.x, r1.y, largeur, r1.height);
g.dispose();
} catch (Exception e) {
logger.info(e);
}
}
}
找到另一个插入符,可以了
public class FancyCaret extends DefaultCaret {
private static final Logger logger = Logger.getLogger(FancyCaret.class.getName());
protected synchronized void damage(Rectangle r) {
if (r == null)
return;
x = r.x;
y = r.y;
height = r.height;
if (width <= 0)
width = getComponent().getWidth();
repaint(); // calls getComponent().repaint(x, y, width, height)
}
@Override
public void paint(Graphics g) {
JTextComponent comp = getComponent();
if (comp == null)
return;
int dot = getDot();
Rectangle r;
char dotChar;
try {
r = comp.modelToView(dot);
if (r == null)
return;
dotChar = comp.getText(dot, 1).charAt(0);
} catch (Exception e) {
logger.info(e);
return;
}
if ((x != r.x) || (y != r.y)) {
repaint(); // erase previous location of caret
x = r.x; // Update dimensions (width gets set later in this method)
y = r.y;
height = r.height;
}
if (dotChar == '\n') {
width = r.height / 2;
if (isVisible())
g.fillRect(r.x, r.y, width, r.height);
return;
}
g.setColor(comp.getCaretColor());
g.setXORMode(comp.getBackground()); // do this to draw in XOR mode
width = g.getFontMetrics().charWidth(dotChar);
if (isVisible())
g.fillRect(r.x, r.y, width, r.height);
}
}