10
A
回答
10
Here是代碼
import java.awt.BorderLayout;
import javax.swing.*;
import javax.swing.event.*;
public class caretDemo extends JFrame {
// Two controls, one is the editor and the other is our little status bar at the bottom.
// When we update the editor, the change in caret will update the status text field.
private JTextArea editor;
private JTextField status;
// Start of our caretDemo class
public caretDemo() {
setTitle("Caret Demo");
setSize(500,500);
// Lets create a border layout to make positioning of items easy and quick.
setLayout(new BorderLayout());
setDefaultCloseOperation(EXIT_ON_CLOSE);
editor = new JTextArea();
// Add a caretListener to the editor. This is an anonymous class because it is inline and has no specific name.
editor.addCaretListener(new CaretListener() {
// Each time the caret is moved, it will trigger the listener and its method caretUpdate.
// It will then pass the event to the update method including the source of the event (which is our textarea control)
public void caretUpdate(CaretEvent e) {
JTextArea editArea = (JTextArea)e.getSource();
// Lets start with some default values for the line and column.
int linenum = 1;
int columnnum = 1;
// We create a try catch to catch any exceptions. We will simply ignore such an error for our demonstration.
try {
// First we find the position of the caret. This is the number of where the caret is in relation to the start of the JTextArea
// in the upper left corner. We use this position to find offset values (eg what line we are on for the given position as well as
// what position that line starts on.
int caretpos = editArea.getCaretPosition();
linenum = editArea.getLineOfOffset(caretpos);
// We subtract the offset of where our line starts from the overall caret position.
// So lets say that we are on line 5 and that line starts at caret position 100, if our caret position is currently 106
// we know that we must be on column 6 of line 5.
columnnum = caretpos - editArea.getLineStartOffset(linenum);
// We have to add one here because line numbers start at 0 for getLineOfOffset and we want it to start at 1 for display.
linenum += 1;
}
catch(Exception ex) { }
// Once we know the position of the line and the column, pass it to a helper function for updating the status bar.
updateStatus(linenum, columnnum);
}
});
// Add the fields to the layout, the editor in the middle and the status at the bottom.
add(editor, BorderLayout.CENTER);
status = new JTextField();
add(status, BorderLayout.SOUTH);
// Give the status update value
updateStatus(1,1);
}
// This helper function updates the status bar with the line number and column number.
private void updateStatus(int linenumber, int columnnumber) {
status.setText("Line: " + linenumber + " Column: " + columnnumber);
}
// Entry point to the program. It kicks off by creating an instance of our class and making it visible.
public static void main(String args[]) {
caretDemo caretDemoApp = new caretDemo();
caretDemoApp.setVisible(true);
}
}
輸出
2
你試過getCaretPosition?
您必須計數\n
才能知道您有哪一行,並且您必須計算當前插入符號位置與最後一次出現的\n
字符之間的差異。
8
需要使用Utilities.getRowStart
與插入位置沿着如下圖所示:
要獲取行號:
int caretPos = textArea.getCaretPosition();
int rowNum = (caretPos == 0) ? 1 : 0;
for (int offset = caretPos; offset > 0;) {
offset = Utilities.getRowStart(textArea, offset) - 1;
rowNum++;
}
System.out.println("Row: " + rowNum);
要得到列數:
int offset = Utilities.getRowStart(textArea, caretPos);
int colNum = caretPos - offset + 1;
System.out.println("Col: " + colNum);
相關問題
- 1. 編輯文本行號和當前行光標位置。
- 2. 找到文本框光標位置,行號。和列號。在asp.net
- 3. 當前行號或行號
- 4. 獲得當前行號列號
- 5. 將當前光標位置顯示爲當前行和當前列?
- 6. 獲得遊標的行號
- 7. phpQuery中的當前行/列號?
- 8. Lua中的當前行號
- 9. 在vi編輯器中從當前光標位置刪除給定的行號
- 10. 野牛和rec2c:獲取當前行號
- 11. 獲取當前行號?
- 12. DAX - 獲取當前行號
- 13. 進位標誌和減號問號
- 14. 當前位置在java中
- 15. 當前使用Ruby Ruby庫時的遊標位置
- 16. 使用位置的當前國家的緊急電話號碼
- 17. 在當前位置和單獨位置放置地標mkmapview
- 18. 如何計算UITextField中當前的插入符號位置?
- 19. format_currency和符號位置
- 20. 老和當前位置在單行
- 21. 在JAVA中找到REGEX(4位數和逗號)前面的逗號
- 22. Java JTextArea動態列和行編號
- 23. Emacs的:括號匹配當光標位於右括號
- 24. Orderedlist號位置
- 25. Vim的:如何改變當前行號
- 26. 需要獲取當前位置和目標位置PIN碼
- 27. iPhone撥號類型控制檢測當前位置?
- 28. GeoPoint和當前位置
- 29. 從java的DataInputStream獲取當前位置
- 30. 多行文本添加符號當前光標指針的Android
請學習Java命名約定並嚴格遵守。 – kleopatra 2012-08-22 07:47:48