JTextPane和JTextArea,禁止编辑(setEditable(false))后,显示光标,并且鼠标为文本光标
JTextArea logTxtArea = new JTextArea();
//禁止编辑
logTxtArea.setEditable(false);
//设置鼠标为文本鼠标
logTxtArea.setCursor(new Cursor(Cursor.TEXT_CURSOR));
//禁止编辑后显示光标
logTxtArea.addFocusListener(new FocusListener() {
public void focusLost(FocusEvent e) {
JTextArea p = (JTextArea)e.getComponent();
p.getCaret().setVisible(false);
}
public void focusGained(FocusEvent e) {
JTextArea p = (JTextArea)e.getComponent();
p.getCaret().setVisible(true);
}
});
注意:上边的代码是以JTextArea来做实例的,如果用作JTextPane的话,仅需将JTextArea换为JTextPane即可。