1

This is an example code to show my question:

import java.awt.Frame;
import java.awt.Label;
import java.awt.TextField;
import java.awt.Button;
import java.awt.Color;
import java.awt.FlowLayout;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

@SuppressWarnings("serial")
public class _4_UsernamePassword extends Frame implements ActionListener
{
    Label lbname = new Label("NAME");
    TextField tfname = new TextField(10);
    Label lbuser = new Label("USERNAME");
    TextField tfuser = new TextField(10);
    Label lbpwd = new Label("PASSWORD");
    TextField tfpwd = new TextField(10);

    Button btok = new Button("OK");
    Button btreset = new Button("RESET");
    Button btcan = new Button("CANCEL");

    FlowLayout fl = new FlowLayout();

    @SuppressWarnings("deprecation")
    _4_UsernamePassword(String s) 
    {
        super(s);
        add(lbname);
        add(tfname);
        tfname.addActionListener(this);
        add(lbuser);
        add(tfuser);
        tfuser.addActionListener(this);
        add(lbpwd);
        add(tfpwd);
        tfpwd.addActionListener(this);
        add(btok);
        btok.addActionListener(this);
        add(btreset);
        btreset.addActionListener(this);
        add(btcan);
        btcan.addActionListener(this);

        setLayout(fl);
        setSize(550, 350);
        show();


    }

    public static void main(String[] args)
    {
        @SuppressWarnings("unused")
        _4_UsernamePassword obj = new _4_UsernamePassword("User Information");
    }

    public void actionPerformed(ActionEvent ae)
    {
        String s1 = null;

        if(ae.getSource() == btcan)
        {
            setBackground(Color.RED);
        }

        if(ae.getSource() == btreset)
        {
            setBackground(Color.gray);
            // uncomment to test
            //tfname.getText();
            //tfuser.getText();
            //tfpwd.getText();

            tfname.setText(s1);
            tfuser.setText(s1);
            tfpwd.setText(s1);

        }

        if(ae.getSource() == btok)
        {
            if("will".equals(tfuser.getText()) && "123".equals(tfpwd.getText()))
            {
                btok.setBackground(Color.green);
                System.out.println("\nlogin ok!\n");
            }
            else
            {
                btok.setBackground(Color.black);
                btok.setForeground(Color.white);
            }
        }
    }
}

[Running on Eclipse/Windows]

Writing all fields and clicking on RESET I want to be all fields cleared.

I think when I type on fields, the characters only exist on the window and the field isn't updated. So, if I put a getText() before the setText() it works.

So, what makes the fields updated in the code? What's the intelligent way to update fields with the words that I am seeing?

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
zewill
  • 181
  • 1
  • 8

0 Answers0