简体   繁体   中英

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.

as example:- your code:- t.setBounds(20,20,170,20); after changing:- 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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