Java AWT TextField

The TextField class is used to create a GUI TextField. It displays an editable line of text. This means that the user has the ability to change the content displayed by the TextField. This class extends the TextComponent class, which is further inherited from the Component class.

TextField类用于创建GUI TextField 。 它显示一行可编辑的文本。 这意味着用户可以更改TextField显示的内容。 该类扩展了TextComponent类,该类进一步从Component类继承。

Whenever a key is pressed in a TextField, the AWT creates events. It could be either a key pressed event, a key released event, or a key typed event. A KeyEvent is passed to the registered KeyListener. A TextField can also generate an ActionEvent whenever the 'enter' key is pressed. Any class interested in this event needs to implement the ActionListener interface. We will study in detail how to handle events in later sections.

每当在TextField中按下键时,AWT都会创建事件。 它可以是按键事件,按键释放事件或按键键入事件。 KeyEvent传递给已注册的KeyListener 。 每当按下“ enter”键时, TextField也可以生成ActionEvent 。 任何对此事件感兴趣的类都需要实现ActionListener接口。 我们将在后面的部分中详细研究如何处理事件。

Consider the following code -

考虑以下代码-

import java.awt.*;
import javax.swing.*;

public class CreateTextField extends Frame{
	CreateTextField()
	{
		TextField t1 = new TextField();
		TextField t2 = new TextField("Java is the best!");
		TextField t3 = new TextField(5);

		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		setVisible(true);
		setSize(300,300);

		add(t1);
		add(t2);
		add(t3);

		if(t2.getText() == "Java is the best!");       
		{ 
			t1.setText("Changed Text");
		}
	}

	public static void main(String []args){
		CreateTextField ob = new CreateTextField();
	}
}

Output

输出量

Java AWT TextField

As seen in the code, we have created 3 TextField objects, initialized in different ways. In 't1', we have not passed any parameter for object initialization. Thus a blank TextField is created.

如代码所示,我们创建了3个TextField对象 ,它们以不同的方式初始化。 在't1'中 ,我们没有传递任何用于对象初始化的参数。 因此,将创建一个空白的TextField

In the second case, we have passed a string "Java is the best!" that is displayed on the screen at the time of TextField creation.

在第二种情况下,我们传递了一个字符串“ Java是最好的!” 创建TextField时在屏幕上显示的内容。

In the third case, we have passed an integer value. This decides the number of columns or size of the TextField that is created.

在第三种情况下,我们传递了一个整数值。 这决定了创建的TextField的列数或大小。

We have set the layout of the frame as BoxLayout (aligned along the y-axis). This places all the TextField one below the other and changes the size of the TextField to fit the frame size. That is why we cannot see the initial 5 column size of 't3'. BoxLayout is found in the swing package that has been imported in our code. We will learn more about layouts in later sections.

我们已将框架的布局设置为BoxLayout(沿y轴对齐)。 这会将所有TextField放在另一个下方,并更改TextField的大小以适合框架大小。 这就是为什么我们看不到't3'的初始5列大小的原因。 BoxLayout可以在我们的代码中导入的swing包中找到。 我们将在后面的部分中详细了解布局。

The getText() method of the TextField class returns the text that is present in the TextField, as a String object. As can be seen in the output, the text of any TextField can be edited by the user (shown by the selected text in TextField 1).

TextField类的getText()方法返回存在于的TextField,为字符串对象的文本。 从输出中可以看到,任何TextField的文本都可以由用户编辑(由TextField 1中的选定文本显示)。

The setText() method is used to set the text that is displayed on the screen. As can be seen in the output, the text of the t1 object is changed using this method.

setText()方法用于设置屏幕上显示的文本。 从输出中可以看出,使用此方法可以更改t1对象的文本。

All TextField objects are added in the frame using the add() method. Since the class extends the Frame class, we don't need to create a frame object separately.

使用add()方法所有TextField对象添加到框架中。 由于该类扩展了Frame类,因此我们不需要单独创建一个框架对象。

翻译自: https://www.includehelp.com/java/awt-textfield.aspx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java AWT TextField 是一个用户界面组件,用于在 GUI 应用程序中显示和接收单行文本输入。它是 AWT 包中的一部分,可以使用 java.awt.TextField 类来创建 TextField 对象。 以下是 TextField 的一些常见方法: - setText(String text):将 TextField 的文本设置为给定的字符串。 - getText():返回 TextField 当前显示的文本。 - setEditable(boolean editable):设置 TextField 是否可编辑。 - addActionListener(ActionListener listener):为 TextField 添加动作监听器,以便在用户输入文本时触发事件。 - setColumns(int columns):设置 TextField 的宽度,以显示给定数量的列。 示例代码: ```java import java.awt.*; import java.awt.event.*; public class TextFieldDemo extends Frame implements ActionListener { TextField textField; Label label; public TextFieldDemo() { setLayout(new FlowLayout()); label = new Label("Enter your name:"); add(label); textField = new TextField(20); add(textField); textField.addActionListener(this); setTitle("TextField Demo"); setSize(300, 100); setVisible(true); } public void actionPerformed(ActionEvent e) { String text = textField.getText(); label.setText("Hello, " + text + "!"); } public static void main(String[] args) { new TextFieldDemo(); } } ``` 这个例子创建了一个带有 Label 和 TextField 的窗口,用户可以在 TextField 中输入文本。当用户按下 Enter 键时,程序使用 addActionListener() 方法注册的 ActionListener 来更新 Label 的文本。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值