我对 Caret 有疑问,如果没有 focusGained(参见 Swing Action 中的代码)到第二个,Caret 不会闪烁。JTextField 并回到第一个。JTextField
如何正确覆盖 DefaultCaret#setBlinkRate()
(没有覆盖插入符号)默认情况下是文档末尾的插入符号并在第 1 次闪烁。获得焦点
在 win7_32b、Java7.011/025 / Java6 上测试
使用一些标准 L&F 进行了测试,也是自定义的,每个都导致相同的问题
有关更多详细信息,请参阅我对焦点丢失时如何在 JTextField 中保留选定文本的问题的回答?以及@kleopatra 可能的解决方法
我的SSCCE
import java.awt.*;
import java.awt.event.ActionEvent;
import javax.swing.*;
import javax.swing.text.DefaultCaret;
import javax.swing.text.DefaultHighlighter;
import javax.swing.text.Highlighter;
public class TestTextComponents {
private static final long serialVersionUID = 1L;
private Timer timer;
private JTextField jTextField0 = new JTextField();
private JTextField jTextField1 = new JTextField();
private JTextField jTextField2 = new JTextField();
private JFrame frame = new JFrame("Default Caret");
private JPanel panel = new JPanel();
public TestTextComponents() {
jTextField0.setText("jTextField0");
jTextField1.setText("jTextField1");
jTextField2.setText("jTextField2");
jTextField1.setCaret(new HighlightCaret());
jTextField2.setCaret(new HighlightCaret());
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
panel.add(new JLabel("Please skip between text fields and watch persistent selection: "));
panel.add(jTextField0);
panel.add(jTextField1);
panel.add(jTextField2);
frame.add(panel);
frame.setTitle("Text component persistent selection");
frame.pack();
frame.setVisible(true);
/*timer = new javax.swing.Timer(250, updateCol());
timer.setRepeats(false);
timer.start();*/
}
private Action updateCol() {
return new AbstractAction("Hello World") {
private static final long serialVersionUID = 1L;
@Override
public void actionPerformed(ActionEvent e) {
jTextField2.grabFocus();
jTextField2.requestFocusInWindow();
jTextField1.grabFocus();
jTextField1.requestFocusInWindow();
}
};
}
private class HighlightCaret extends DefaultCaret {
private static final long serialVersionUID = 1L;
private final Highlighter.HighlightPainter unfocusedPainter = new DefaultHighlighter.DefaultHighlightPainter(Color.RED);
private final Highlighter.HighlightPainter focusedPainter = new DefaultHighlighter.DefaultHighlightPainter(Color.ORANGE);
private boolean isFocused;
@Override
protected Highlighter.HighlightPainter getSelectionPainter() {
return isFocused ? focusedPainter /*super.getSelectionPainter()*/ : unfocusedPainter;
}
@Override
public void setSelectionVisible(boolean hasFocus) {
super.repaint();
super.setBlinkRate(500);
if (hasFocus != isFocused) {
isFocused = hasFocus;
super.setSelectionVisible(false);
super.setSelectionVisible(true);
}
}
}
public static void main(String args[]) {
/*try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
try {
for (UIManager.LookAndFeelInfo laf : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(laf.getName())) {
UIManager.setLookAndFeel(laf.getClassName());
}
}
} catch (Exception e) {
e.printStackTrace();
}*/
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new TestTextComponents();
}
});
}
}