刚好看到个例子,copy给你吧. CaretListenerLabel caretListenerLabel = new CaretListenerLabel("Caret Status"); ... text.addActionListener(caretListenerLabel); ... protected class CaretListenerLabel extends JLabel implements CaretListener { ... public void caretUpdate(CaretEvent e) { //Get the location in the text int dot = e.getDot(); int = e.getMark(); ... } } ----------------------- The CaretEvent class defines two useful methods: int getDot() Returns the current location of the caret. If text is selected, the caret s one end of the selection. int getMark() Returns the other end of the selection. If nothing is selected, the value returned by this method is equal to the value returned by getDot. Note that the dot is not guaranteed to be less than the .
请给个简单的完整的例子
请给个简单的完整的例子
CaretListenerLabel caretListenerLabel =
new CaretListenerLabel("Caret Status");
...
text.addActionListener(caretListenerLabel);
...
protected class CaretListenerLabel extends JLabel
implements CaretListener
{
...
public void caretUpdate(CaretEvent e) {
//Get the location in the text
int dot = e.getDot();
int = e.getMark();
...
}
}
-----------------------
The CaretEvent class defines two useful methods: int getDot()
Returns the current location of the caret. If text is selected, the caret s one end of the selection.
int getMark()
Returns the other end of the selection. If nothing is selected, the value returned by this method is equal to the value returned by getDot. Note that the dot is not guaranteed to be less than the .