目录:
效果图:
- java源码
- 效果图
java源码:
package textfield;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.text.NavigationFilter;
import javax.swing.text.Position;
public class NavigationFilterExample {
public static void main(String args[]) throws Exception {
final String START_STRING = "Start\n";
final int START_STRING_LENGTH = START_STRING.length();
JFrame frame = new JFrame("Navigation Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTextArea textArea = new JTextArea(START_STRING);
textArea.setCaretPosition(START_STRING_LENGTH);
JScrollPane scrollPane = new JScrollPane(textArea);
frame.add(scrollPane, BorderLayout.CENTER);
NavigationFilter filter = new NavigationFilter() {
public void setDot(NavigationFilter.FilterBypass fb, int dot, Position.Bias bias) {
if (dot < START_STRING_LENGTH) {
fb.setDot(START_STRING_LENGTH, bias);
System.out.println("dot < START_STRING_LENGTH");
} else {
fb.setDot(dot, bias);
System.out.println("dot >= START_STRING_LENGTH");
}
}
public void moveDot(NavigationFilter.FilterBypass fb, int dot, Position.Bias bias) {
if (dot < START_STRING_LENGTH) {
fb.setDot(START_STRING_LENGTH, bias);
System.out.println("dot < START_STRING_LENGTH");
} else {
fb.setDot(dot, bias);
System.out.println("dot >= START_STRING_LENGTH");
}
}
};
textArea.setNavigationFilter(filter);
frame.setSize(250, 150);
frame.setVisible(true);
}
}
效果图:
1、
2、