簡體   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