下面列出了怎么用java.awt.TextField的API类实例代码及写法,或者点击链接到github查看源代码。
public static void main(String[] args) throws Exception {
SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
Frame frame = new Frame();
frame.setSize(300, 200);
TextField textField = new TextField();
frame.add(textField);
frame.setVisible(true);
toolkit.realSync();
textField.requestFocus();
toolkit.realSync();
// Check that the system assertion dialog does not block Java
Robot robot = new Robot();
robot.setAutoDelay(50);
robot.keyPress(KeyEvent.VK_A);
robot.keyRelease(KeyEvent.VK_A);
toolkit.realSync();
frame.setVisible(false);
frame.dispose();
}
static void testFocus() throws Exception {
System.out.println("Focus Test!");
Robot robot = new Robot();
robot.setAutoDelay(50);
Frame frame = new Frame();
frame.setSize(100, 100);
String test = "123";
TextField textField = new TextField(test);
frame.add(textField);
frame.setVisible(true);
robot.waitForIdle();
robot.keyPress(KeyEvent.VK_A);
robot.keyRelease(KeyEvent.VK_A);
robot.keyPress(KeyEvent.VK_B);
robot.keyRelease(KeyEvent.VK_B);
robot.waitForIdle();
frame.dispose();
if(!textField.getText().equals("ab")){
throw new RuntimeException("Focus is lost!");
}
}
private static void createAndShowGUI() {
frame = new Frame("Test Frame");
textField1 = new TextField(5);
button = new Button("Go");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
textField1.requestFocusInWindow();
startUpdate();
}
});
frame.setLayout(new FlowLayout());
frame.setSize(400, 200);
frame.add(textField1);
frame.add(new TextField(5));
frame.add(new TextField(5));
frame.add(button);
frame.setVisible(true);
}
public static void addSaveAsFileField( final GenericDialogPlus dialog, final String label, final String defaultPath, final int columns) {
dialog.addStringField( label, defaultPath, columns );
final TextField text = ( TextField ) dialog.getStringFields().lastElement();
final GridBagLayout layout = ( GridBagLayout ) dialog.getLayout();
final GridBagConstraints constraints = layout.getConstraints( text );
final Button button = new Button( "Browse..." );
final ChooseXmlFileListener listener = new ChooseXmlFileListener( text );
button.addActionListener( listener );
button.addKeyListener( dialog );
final Panel panel = new Panel();
panel.setLayout( new FlowLayout( FlowLayout.LEFT, 0, 0 ) );
panel.add( text );
panel.add( button );
layout.setConstraints( panel, constraints );
dialog.add( panel );
}
@Override
public void init() {
tf = new TextField(20);
tf.setText("0123456789");
tf.select(0, 6);
final TextArea ta = new TextArea("INSTRUCTIONS:\n"
+ "The text 012345 should be selected in the TextField.\n"
+ "If this is what you observe, then the test passes.\n"
+ "Otherwise, the test fails.", 40, 5,
TextArea.SCROLLBARS_NONE);
ta.setEditable(false);
ta.setPreferredSize(new Dimension(300, 70));
final Panel panel = new Panel();
panel.setLayout(new FlowLayout());
panel.add(tf);
setLayout(new BorderLayout());
add(ta, BorderLayout.CENTER);
add(panel, BorderLayout.PAGE_END);
}
public static void main(String[] args) throws Exception {
SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
Frame frame = new Frame();
frame.setSize(300, 200);
TextField textField = new TextField();
frame.add(textField);
frame.setVisible(true);
toolkit.realSync();
textField.requestFocus();
toolkit.realSync();
// Check that the system assertion dialog does not block Java
Robot robot = new Robot();
robot.setAutoDelay(50);
robot.keyPress(KeyEvent.VK_A);
robot.keyRelease(KeyEvent.VK_A);
toolkit.realSync();
frame.setVisible(false);
frame.dispose();
}
@Override
public void init() {
tf = new TextField(20);
tf.setText("0123456789");
tf.select(0, 6);
final TextArea ta = new TextArea("INSTRUCTIONS:\n"
+ "The text 012345 should be selected in the TextField.\n"
+ "If this is what you observe, then the test passes.\n"
+ "Otherwise, the test fails.", 40, 5,
TextArea.SCROLLBARS_NONE);
ta.setEditable(false);
ta.setPreferredSize(new Dimension(300, 70));
final Panel panel = new Panel();
panel.setLayout(new FlowLayout());
panel.add(tf);
setLayout(new BorderLayout());
add(ta, BorderLayout.CENTER);
add(panel, BorderLayout.PAGE_END);
}
@Override
public void init() {
tf = new TextField(20);
tf.setText("0123456789");
tf.select(0, 6);
final TextArea ta = new TextArea("INSTRUCTIONS:\n"
+ "The text 012345 should be selected in the TextField.\n"
+ "If this is what you observe, then the test passes.\n"
+ "Otherwise, the test fails.", 40, 5,
TextArea.SCROLLBARS_NONE);
ta.setEditable(false);
ta.setPreferredSize(new Dimension(300, 70));
final Panel panel = new Panel();
panel.setLayout(new FlowLayout());
panel.add(tf);
setLayout(new BorderLayout());
add(ta, BorderLayout.CENTER);
add(panel, BorderLayout.PAGE_END);
}
public static void main(String[] args) throws Exception {
SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
Frame frame = new Frame();
frame.setSize(300, 200);
TextField textField = new TextField();
frame.add(textField);
frame.setVisible(true);
toolkit.realSync();
textField.requestFocus();
toolkit.realSync();
// Check that the system assertion dialog does not block Java
Robot robot = new Robot();
robot.setAutoDelay(50);
robot.keyPress(KeyEvent.VK_A);
robot.keyRelease(KeyEvent.VK_A);
toolkit.realSync();
frame.setVisible(false);
frame.dispose();
}
@Override
public void init() {
tf = new TextField(20);
tf.setText("0123456789");
tf.select(0, 6);
final TextArea ta = new TextArea("INSTRUCTIONS:\n"
+ "The text 012345 should be selected in the TextField.\n"
+ "If this is what you observe, then the test passes.\n"
+ "Otherwise, the test fails.", 40, 5,
TextArea.SCROLLBARS_NONE);
ta.setEditable(false);
ta.setPreferredSize(new Dimension(300, 70));
final Panel panel = new Panel();
panel.setLayout(new FlowLayout());
panel.add(tf);
setLayout(new BorderLayout());
add(ta, BorderLayout.CENTER);
add(panel, BorderLayout.PAGE_END);
}
/**
* Adds the directory field.
*
* @param label
* the label
* @param defaultPath
* the default path
* @param columns
* the columns
*/
public void addDirectoryField(String label, String defaultPath, int columns) {
addStringField(label, defaultPath, columns);
if (isHeadless())
return;
TextField text = (TextField) stringField.lastElement();
GridBagLayout layout = (GridBagLayout) getLayout();
GridBagConstraints constraints = layout.getConstraints(text);
Button button = new Button("Browse...");
DirectoryListener listener = new DirectoryListener("Browse for " + label, text);
button.addActionListener(listener);
button.addKeyListener(this);
Panel panel = new Panel();
panel.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
panel.add(text);
panel.add(button);
layout.setConstraints(panel, constraints);
add(panel);
}
public static void main(String[] args) throws Exception {
SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
Frame frame = new Frame();
frame.setSize(300, 200);
TextField textField = new TextField();
frame.add(textField);
frame.setVisible(true);
toolkit.realSync();
textField.requestFocus();
toolkit.realSync();
// Check that the system assertion dialog does not block Java
Robot robot = new Robot();
robot.setAutoDelay(50);
robot.keyPress(KeyEvent.VK_A);
robot.keyRelease(KeyEvent.VK_A);
toolkit.realSync();
frame.setVisible(false);
frame.dispose();
}
public static void main(String[] args) throws Exception {
SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
Frame frame = new Frame();
frame.setSize(300, 200);
TextField textField = new TextField();
frame.add(textField);
frame.setVisible(true);
toolkit.realSync();
textField.requestFocus();
toolkit.realSync();
// Check that the system assertion dialog does not block Java
Robot robot = new Robot();
robot.setAutoDelay(50);
robot.keyPress(KeyEvent.VK_A);
robot.keyRelease(KeyEvent.VK_A);
toolkit.realSync();
frame.setVisible(false);
frame.dispose();
}
static void testFocus() throws Exception {
System.out.println("Focus Test!");
Robot robot = new Robot();
robot.setAutoDelay(50);
Frame frame = new Frame();
frame.setSize(100, 100);
String test = "123";
TextField textField = new TextField(test);
frame.add(textField);
frame.setVisible(true);
robot.waitForIdle();
robot.keyPress(KeyEvent.VK_A);
robot.keyRelease(KeyEvent.VK_A);
robot.keyPress(KeyEvent.VK_B);
robot.keyRelease(KeyEvent.VK_B);
robot.waitForIdle();
frame.dispose();
if(!textField.getText().equals("ab")){
throw new RuntimeException("Focus is lost!");
}
}
@Override
public void init() {
tf = new TextField(20);
tf.setText("0123456789");
tf.select(0, 6);
final TextArea ta = new TextArea("INSTRUCTIONS:\n"
+ "The text 012345 should be selected in the TextField.\n"
+ "If this is what you observe, then the test passes.\n"
+ "Otherwise, the test fails.", 40, 5,
TextArea.SCROLLBARS_NONE);
ta.setEditable(false);
ta.setPreferredSize(new Dimension(300, 70));
final Panel panel = new Panel();
panel.setLayout(new FlowLayout());
panel.add(tf);
setLayout(new BorderLayout());
add(ta, BorderLayout.CENTER);
add(panel, BorderLayout.PAGE_END);
}
public static void main(String[] args) throws Exception {
SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
Frame frame = new Frame();
frame.setSize(300, 200);
TextField textField = new TextField();
frame.add(textField);
frame.setVisible(true);
toolkit.realSync();
textField.requestFocus();
toolkit.realSync();
// Check that the system assertion dialog does not block Java
Robot robot = new Robot();
robot.setAutoDelay(50);
robot.keyPress(KeyEvent.VK_A);
robot.keyRelease(KeyEvent.VK_A);
toolkit.realSync();
frame.setVisible(false);
frame.dispose();
}
@Override
public void init() {
tf = new TextField(20);
tf.setText("0123456789");
tf.select(0, 6);
final TextArea ta = new TextArea("INSTRUCTIONS:\n"
+ "The text 012345 should be selected in the TextField.\n"
+ "If this is what you observe, then the test passes.\n"
+ "Otherwise, the test fails.", 40, 5,
TextArea.SCROLLBARS_NONE);
ta.setEditable(false);
ta.setPreferredSize(new Dimension(300, 70));
final Panel panel = new Panel();
panel.setLayout(new FlowLayout());
panel.add(tf);
setLayout(new BorderLayout());
add(ta, BorderLayout.CENTER);
add(panel, BorderLayout.PAGE_END);
}
public static void main(String[] args) throws Exception {
SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
Frame frame = new Frame();
frame.setSize(300, 200);
TextField textField = new TextField();
frame.add(textField);
frame.setVisible(true);
toolkit.realSync();
textField.requestFocus();
toolkit.realSync();
// Check that the system assertion dialog does not block Java
Robot robot = new Robot();
robot.setAutoDelay(50);
robot.keyPress(KeyEvent.VK_A);
robot.keyRelease(KeyEvent.VK_A);
toolkit.realSync();
frame.setVisible(false);
frame.dispose();
}
static void testFocus() throws Exception {
Robot robot = new Robot();
robot.setAutoDelay(50);
Frame frame = new Frame();
frame.setSize(100, 100);
String test = "123";
TextField textField = new TextField(test);
textField.selectAll();
frame.add(textField);
frame.setVisible(true);
robot.waitForIdle();
robot.keyPress(KeyEvent.VK_A);
robot.keyRelease(KeyEvent.VK_A);
robot.keyPress(KeyEvent.VK_B);
robot.keyRelease(KeyEvent.VK_B);
robot.waitForIdle();
frame.dispose();
if (!textField.getText().equals("ab")) {
throw new RuntimeException("Focus is lost!");
}
}
public StringRequester(Frame parent, String title,
String lines, String posText_, String negText) {
super(parent, title, true);
setLayout(new GridBagLayout());
posText = posText_;
add(new MultiLineLabel(lines), Awt.constraints(true));
text = new TextField(30);
add(text, Awt.constraints(true, 10, 4, GridBagConstraints.HORIZONTAL));
Button pos = new Button(posText);
add(pos, Awt.constraints(false, 10, 4, GridBagConstraints.HORIZONTAL));
pos.addActionListener(this);
Button neg = new Button(negText);
add(neg, Awt.constraints(true, 10, 4, GridBagConstraints.HORIZONTAL));
neg.addActionListener(this);
pack();
}
@Override
public boolean checkComponent(Component comp) {
if (comp instanceof TextField) {
if (((TextField) comp).getText() != null) {
return (comparator.equals(((TextField) comp).getText(),
label));
}
}
return false;
}
/**
* Maps {@code TextField.setColumns(int)} through queue
*/
public void setColumns(final int i) {
runMapping(new MapVoidAction("setColumns") {
@Override
public void map() {
((TextField) getSource()).setColumns(i);
}
});
}
/**
* Maps {@code TextField.getEchoChar()} through queue
*/
public char getEchoChar() {
return (runMapping(new MapCharacterAction("getEchoChar") {
@Override
public char map() {
return ((TextField) getSource()).getEchoChar();
}
}));
}
public void testDispose() throws InvocationTargetException,
InterruptedException {
SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
frame = new JFrame("Test");
textField = new TextField("editable textArea");
textField.setEditable(true);
// textField.setEditable(false); // this testcase passes if textField is non-editable
frame.setLayout(new FlowLayout());
frame.add(textField);
frame.pack();
frame.setVisible(true);
}
});
toolkit.realSync();
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
frame.dispose();
}
});
toolkit.realSync();
}
public static void main(String[] args) throws Exception {
Frame frame = new Frame();
frame.setSize(300, 200);
TextField textField = new TextField(TEXT + LAST_WORD, 30);
Panel panel = new Panel(new FlowLayout());
panel.add(textField);
frame.add(panel);
frame.setVisible(true);
SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
toolkit.realSync();
Robot robot = new Robot();
robot.setAutoDelay(50);
Point point = textField.getLocationOnScreen();
int x = point.x + textField.getWidth() / 2;
int y = point.y + textField.getHeight() / 2;
robot.mouseMove(x, y);
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
toolkit.realSync();
robot.mousePress(InputEvent.BUTTON1_MASK);
int N = 10;
int dx = textField.getWidth() / N;
for (int i = 0; i < N; i++) {
x += dx;
robot.mouseMove(x, y);
}
robot.mouseRelease(InputEvent.BUTTON1_MASK);
toolkit.realSync();
if (!textField.getSelectedText().endsWith(LAST_WORD)) {
throw new RuntimeException("Last word is not selected!");
}
}
private static double getFieldDouble(
TextField theField
) {
StringTokenizer fieldTokenizer
= new StringTokenizer(theField.getText());
String fieldString =
fieldTokenizer.nextToken();
return (new Double(fieldString)).doubleValue();
}
/**
* Maps {@code TextField.getColumns()} through queue
*/
public int getColumns() {
return (runMapping(new MapIntegerAction("getColumns") {
@Override
public int map() {
return ((TextField) getSource()).getColumns();
}
}));
}
public void testDispose() throws InvocationTargetException,
InterruptedException {
SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
frame = new JFrame("Test");
textField = new TextField("editable textArea");
textField.setEditable(true);
// textField.setEditable(false); // this testcase passes if textField is non-editable
frame.setLayout(new FlowLayout());
frame.add(textField);
frame.pack();
frame.setVisible(true);
}
});
toolkit.realSync();
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
frame.dispose();
}
});
toolkit.realSync();
}
/**
* Maps {@code TextField.addActionListener(ActionListener)} through queue
*/
public void addActionListener(final ActionListener actionListener) {
runMapping(new MapVoidAction("addActionListener") {
@Override
public void map() {
((TextField) getSource()).addActionListener(actionListener);
}
});
}
public static void main(String[] args) throws Exception {
Frame frame = new Frame();
frame.setSize(300, 200);
TextField textField = new TextField(TEXT + LAST_WORD, 30);
Panel panel = new Panel(new FlowLayout());
panel.add(textField);
frame.add(panel);
frame.setVisible(true);
SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
toolkit.realSync();
Robot robot = new Robot();
robot.setAutoDelay(50);
Point point = textField.getLocationOnScreen();
int x = point.x + textField.getWidth() / 2;
int y = point.y + textField.getHeight() / 2;
robot.mouseMove(x, y);
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
toolkit.realSync();
robot.mousePress(InputEvent.BUTTON1_MASK);
int N = 10;
int dx = textField.getWidth() / N;
for (int i = 0; i < N; i++) {
x += dx;
robot.mouseMove(x, y);
}
robot.mouseRelease(InputEvent.BUTTON1_MASK);
toolkit.realSync();
if (!textField.getSelectedText().endsWith(LAST_WORD)) {
throw new RuntimeException("Last word is not selected!");
}
}