简体   繁体   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();
    }
}

Initially I added a text field to the the frame object and I created a button also and added this to frame object but I am not getting correct output.最初我向框架对象添加了一个文本字段,我还创建了一个按钮并将其添加到框架对象,但我没有得到正确的输出。

When I executed this program the text field does not appear.当我执行这个程序时,文本字段没有出现。 Why?为什么?

I got output like this: enter image description here我得到这样的输出:在此处输入图像描述

I would like to suggest only one change in line number 11, that increase the x-coordinate and y-coordinate value of setBounds as per your requirement because your code is working but that field is not at a visible location.我只想建议第 11 行中的一个更改,根据您的要求增加 setBounds 的 x 坐标和 y 坐标值,因为您的代码正在运行,但该字段不在可见位置。

as example:- your code:- t.setBounds(20,20,170,20);例如:- 您的代码:- t.setBounds(20,20,170,20); after changing:- t.setBounds(50,50,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();
    }
}

I hope, it will be helpful.我希望,它会有所帮助。

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

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