<< 返回
import java.awt.Font;
import javax.swing.JLabel;
import javax.swing.text.BadLocationException;
@SuppressWarnings("serial")
public class Status extends JLabel {
private Notepad notepad = null;
private Font font = null;
private int pos = 0; //相对0行0列的位置
private int col = 0; //列
private int row = 0; //行
private String statusInfo = "第 1 行,第 1 列 ";
public Status(Notepad notepad) {
this.notepad = notepad;
this.font = new Font("微软雅黑", 0, 12);
this.setFont(this.font);
this.setText(this.statusInfo);
this.setHorizontalAlignment(JLabel.RIGHT);
}
public void updateStatus() throws BadLocationException {
this.pos = this.notepad.getTextArea().getCaretPosition();
this.col = this.pos - this.notepad.getTextArea().getText().substring(0, pos).lastIndexOf("\n");
this.row = this.notepad.getTextArea().getLineOfOffset(pos) + 1;
this.statusInfo = "第 " + this.row + " 行,第 " + this.col + " 列 ";
this.setText(this.statusInfo);
}
}
<< 返回