Posted By:
20 Feb, 2014 12:32 pm PST

How to create Text Boxes and Push Buttons in Java ?

Write a program to create two labels and two text fields for entering name and passwords. The password used by the user in the text field is hidden.

//text fields with labels

import java.awt.*;

import java.awt.event.*;

public class MyText extends Frame implements ActionListener {

       TextField name,pass;

       MyText()

       {

       //set layout to Flow layout

              setLayout(new FlowLayout());

              //CREATE 2 LABELS

              Label n=new Label("Name: ", Label.LEFT);

              Label p=new Label("Password: ", Label.LEFT);

              //create text fields for name and password

              name=new TextField(20);

              pass=new TextField(20);

              //hide the password by *

              pass.setEchoChar('*');

              //use background, foreground colors and font for name text field

              name.setBackground(Color.yellow);

              name.setForeground(Color.red);

              Font f=new Font("Arial",Font.BOLD,25);

              name.setFont(f);

              //add the labels and text fields to frame

              add(n);

              add(name);

              add(p);

              add(pass);

              //add action listener to text fields

              name.addActionListener(this);

              pass.addActionListener(this);

              //close the frame

              addWindowListener(new WindowAdapter()

              {

                     public void windowClosing(WindowEvent we)

                     {

                           System.exit(0);

                     }

                     });

              }// end of constructor

//this method is executed when enter is clicked

       //display the text entered into text fields

       public void actionPerformed(ActionEvent ae){

              //create graphics class object

              Graphics g= this.getGraphics();

              g.drawString("Name: "+ name.getText(),10,200);

              g.drawString("Password: "+pass.getText(),10,240);

       }

       public static void main(String args[])

       {

              //create a frame

              MyText t= new MyText();

              t.setTitle("Text Fields and Label");

              t.setSize(400,400);

              t.setVisible(true);

                                  }

                                  }

OUTPUT:

Write a program to create the push buttons, arrange them using FlowLayout manager and setting the background color for the frame depending on the button clicked by the user.

//push buttons

import java.awt.*;

import java.awt.event.*;

public class MyButton extends Frame implements ActionListener{

       Button b1,b2,b3;

       MyButton(){

       setLayout(new FlowLayout());

              b1=new Button("Yellow");

              b2=new Button("Blue");

              b3=new Button("Pink");

              add(b1);

              add(b2);

              add(b3);

              b1.addActionListener(this);;

              b2.addActionListener(this);;

              b3.addActionListener(this);

//close the frame

              addWindowListener(new WindowAdapter()

              {

                                  public void windowClosing(WindowEvent we)

                                  {

                                         System.exit(0);

                                  }

                                  });

                           }// end of constructor

              //this method is called when a button is clicked

       public void actionPerformed(ActionEvent ae)

       {

              //know the button clicked by user

              if(ae.getSource()==b1) setBackground(Color.yellow);

              if(ae.getSource()==b2)setBackground(Color.blue);

              if(ae.getSource()==b3)setBackground(Color.pink);

                     }

       public static void main(String args[])

       {

              //create a frame

              MyButton t= new MyButton();

              t.setTitle("Push Buttons");

              t.setSize(400,400);

              t.setVisible(true);

                                  }

}

OUTPUT:


Attachment: workspace.zip


setLayoutA layout manager is an object that implements the LayoutManager interface  and determines the size and position of the components within a container

Label classA Label object is a component for placing text in a container. A label displays a single line of read-only text. The text can be changed by the application, but a user cannot edit it directly

public class Label

extends Component

implements Accessible

Label(String text, int alignment):Constructs a new label that presents the specified string of text with the specified alignment.

Textfield class: A TextField object is a text component that allows for the editing of a single line of text.

add(Popupmenu Popup): Adds the specified popup menu to the component.

addActionListener(this): An ActionListener is a callback mechanism. Whenevera control is added, it fires an ActionEvent and the public void actionPerformed(ActionEvent e) method is invoked. this in the method takes the current class object as a parameter.

Font class: The Font class represents fonts, which are used to render text in a visible way.

WindowListener interface: The listener interface for receiving window events. The class that is interested in processing a window event either implements this interface (and all the methods it contains) or extends the abstract WindowAdapter class (overriding only the methods of interest). The listener object created from that class is then registered with a Window using the window's addWindowListener method. When the window's status changes by virtue of being opened, closed, activated or deactivated, iconified or deiconified, the relevant method in the listener object is invoked, and the WindowEvent is passed to it.

Attachment: MyText.java
Was it useful?  




Please login to reply to this problem.



Related Problems on Fixoncloud