ubuntu Java Swing jtextfield 插入符号自 java8 以来不可见(setXORMode bu



为什么在java8(oracle和openjdk(及更高版本下插入符号变得不可见(按下键或向左箭头后(?(它在 java7 下工作(。我在 ubuntu 上。它在mac-os jdk1.8.0_51和Windows jdk1.8.0_65上运行良好。 似乎与此错误有关。它是特定于 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);
}
}

相关内容

最新更新