0
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();
    }
}

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

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

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

4

1 回答 1

0

我想建议仅对第 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();
    }
}

我希望,这会有所帮助。

于 2021-08-04T02:38:28.107 回答