博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java AWT TextField
阅读量:2543 次
发布时间:2019-05-11

本文共 3549 字,大约阅读时间需要 11 分钟。

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类,因此我们不需要单独创建一个框架对象。

翻译自:

转载地址:http://eyxzd.baihongyu.com/

你可能感兴趣的文章
“==”运算符与equals()
查看>>
单工、半双工和全双工的定义
查看>>
Hdu【线段树】基础题.cpp
查看>>
时钟系统
查看>>
BiTree
查看>>
5个基于HTML5的加载动画推荐
查看>>
水平权限漏洞的修复方案
查看>>
静态链接与动态链接的区别
查看>>
Android 关于悬浮窗权限的问题
查看>>
如何使用mysql
查看>>
linux下wc命令详解
查看>>
敏捷开发中软件测试团队的职责和产出是什么?
查看>>
在mvc3中使用ffmpeg对上传视频进行截图和转换格式
查看>>
python的字符串内建函数
查看>>
Spring - DI
查看>>
微软自己的官网介绍 SSL 参数相关
查看>>
Composite UI Application Block (CAB) 概念和术语
查看>>
ajax跨域,携带cookie
查看>>
阶段3 2.Spring_01.Spring框架简介_03.spring概述
查看>>
阶段3 2.Spring_02.程序间耦合_1 编写jdbc的工程代码用于分析程序的耦合
查看>>