繁体   English   中英

为什么 AWT 框架的文本字段没有出现?

[英]Why AWT frame's text field is not appearing?

import java.awt.*;
import java.awt.event.*;

public class example3 extends Frame implements ActionListener
{
TextField t;
Button b;
    example3()
    {
        t = new TextField();
        t.setBounds(20,20,170,20);
        add(t);

        b = new Button("click me");
        b.setBounds(100,120,80,30);
        add(b);
        b.addActionListener(this);
        
        setLayout(null);
        setSize(300,300);
        setVisible(true);
    }

    public void actionPerformed(ActionEvent e)
    {
        t.setText("welcome");
    }

    public static void main(String args[])
    {
        example3 obj = new example3();
    }
}

最初我向框架对象添加了一个文本字段,我还创建了一个按钮并将其添加到框架对象,但我没有得到正确的输出。

当我执行这个程序时,文本字段没有出现。 为什么?

我得到这样的输出:在此处输入图像描述

我只想建议第 11 行中的一个更改,根据您的要求增加 setBounds 的 x 坐标和 y 坐标值,因为您的代码正在运行,但该字段不在可见位置。

例如:- 您的代码:- t.setBounds(20,20,170,20); 更改后:- t.setBounds(50,50,170,20);

import java.awt.*;
import java.awt.event.*;

public class example3 extends Frame implements ActionListener
{
TextField t;
Button b;
    example3()
    {
        t = new TextField();

        
       // t.setBounds(20,20,170,20);
       //Changes has been done,here.
       t.setBounds(50,50,170,20);
        add(t);



        

        b = new Button("click me");
        b.setBounds(100,120,80,30);
        add(b);
        b.addActionListener(this);
        
        setLayout(null);
        setSize(300,300);
        setVisible(true);
    }

    public void actionPerformed(ActionEvent e)
    {
        t.setText("welcome");
    }

    public static void main(String args[])
    {
        example3 obj = new example3();
    }
}

我希望,它会有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM