实例方法:

public void setSize (int); 
public int getSize ();  
public void setText (String); 
public String getText ();
public void setEchoChar (char); 
public char getEchoChar ();
public void setEditable (boolean);
public boolean getEditable(); 
public boolean isEditable ();
public void addTextListener (TextListener); 
public void removeTextListener (TextListener);

写一个说明文本菲尔德的概念的Java程序?

回答:

import java.awt.*;
 import java.awt.event.*;
class Textfield extends Frame
{
    Label l1, l2, l3; 
    TextField tf1, tf2, tf3; 
    Button b1, b2, b3, b4; 
    Textfield ()
    {
        setTitle ("Operations"); 
        setSize (200, 200);
        setLayout (new FlowLayout ()); 
        l1=new Label ("ENTER FIRST NUMBER"); 
        l2=new Label ("ENTER SECOND NUMBER");
        l3=new Label ("ENTER THIRD NUMBER");
        tf1=new TextField (15);
        tf2=new TextField (15);
        tf3=new TextField (15);
        b1=new Button ("Sum");
        b2=new Button ("Sub"); 
        b3=new Button ("Mul");
        b4=new Button ("Exit"); 
        add (l1);
        add (l2);
        add (l3);
        add (tf1);
        add (tf2);
        add (tf3);
        add (b1);
        add (b2);
        add (b3);
        add (b4);
        b1.addActionListener (new al());
        b2.addActionListener (new al()); 
        b3.addActionListener (new al()); 
        b4.addActionListener (new al()); 
        setVisible (true);
    }
    class al implements ActionListener
    {
        public void actionPerformed (ActionEvent ae)
        {
            String cap=ae.getActionCommand (); 
            if (cap.equalsIgnoreCase ("Sum"))
            {
                String s1=tf1.getText (); 
                String s2=tf2.getText ();  
                int n1=Integer.parseInt (s1); 
                nt n2=Integer.parseInt (s2); 
                int n3=n1+n2;
                String s=String.valueOf (n3); 
                tf3.setText (s);
            }
            if (cap.equalsIgnoreCase ("Sub"))
            {
                String s1=tf1.getText (); 
                String s2=tf2.getText ();  
                int n1=Integer.parseInt (s1); 
                int n2=Integer.parseInt (s2); 
                int n3=n1-n2;
                String s=String.valueOf (n3); 
                tf3.setText (s);
            }
            if (cap.equalsIgnoreCase ("Mul"))
            {
                String s1=tf1.getText (); 
                String s2=tf2.getText ();
                int n1=Integer.parseInt (s1);
                int n2=Integer.parseInt (s2);
                int n3=n1*n2;
                String s=String.valueOf (n3); 
                tf3.setText (s);
            }
            if (cap.equalsIgnoreCase ("Exit"))
            {
                System.exit (0);
            }
        }
    }
};
class TextfieldDemo
{
    public static void main (String [] args)
    {
        new Textfield ();
    }
};

构造函数:

TextField ();
TextField (int);//int represents size of the text field
TextField (String);//data in the text field 
TextField (String, int);
AWT文本字段

TextField文本框是GUI交互式组件,允许单行输入数据。
无论数据如何,我们输入文本字段,默认情况下,数据将被视为字符串。

如果我们能够读取文本字段的数据,那么该文本字段称为可编辑或者普通文本字段。

如果我们无法读取文本字段的数据,那么该文本字段称为echo文本字段,可用的字符称为echo字符。

创建文本框只不过是创建TextField类的对象。

文本字段API:

Java AWT文本框示例

import java.awt.*;
import java.awt.event.*; 
class CloseWin extends Frame
{
    CloseWin ()
    {
        setTitle ("Java Example"); 
        setBackground (Color.green); 
        setSize (200, 200);
        this.addWindowListener (new WinAdap ()); 
        setVisible (true);
    }
    class WinAdap extends WindowAdapter
    {
        public void windowClosing (WindowEvent we)
        {
            System.exit (0);
        }
    }
};
class CloseWinDemo
{
    public static void main (String [] args)
    {
        new CloseWin ();
    }
};
日期:2020-04-11 23:04:26 来源:oir作者:oir