Welcome to Java Examples

Take a cup of tea and Let's Start programming

Write a complete program to create a frame for providing GUI to implement a stack for storing integer numbers. There are two buttons called PUSH & POP and a text field. Clicking of button PUSH pushes the number entered in the text field onto the stack. The click of button POP pops an element from the stack and displays that in the text field.


import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;
import java.lang.*;



public class stack extends Applet implements ActionListener
{
int j;

TextField txtData = new TextField("",20);
Button btnPush = new Button("PUSH");
Button btnPop = new Button("POP");
Label lbl1 = new Label("");
Label lbl2 = new Label("");
Label lbl3 = new Label("");
Label lbl4 = new Label("");
Label lbl5 = new Label("");
Label lblError = new Label("hi");
Label lblTop = new Label("<-- br="" top=""> boolean push,pop,txtNull;
String txtValue;

public void init()
{
j=0;

setLayout(null);
add(txtData);
add(btnPush);
add(btnPop);

txtData.setBounds(10,10,200,25);
btnPush.setBounds(220,10,50,25);
btnPop.setBounds(290,10,50,25);



add(lbl1);
add(lbl2);
add(lbl3);
add(lbl4);
add(lbl5);
add(lblError);
add(lblTop);

lbl1.setVisible(false);
lbl2.setVisible(false);
lbl3.setVisible(false);
lbl4.setVisible(false);
lbl5.setVisible(false);
lblError.setVisible(false);
lblTop.setVisible(true);


txtData.setBounds(10,10,50,20);
btnPush.setBounds(70,10,50,20);
btnPop.setBounds(140,10,50,20);
lbl1.setBounds(50,150,50,20);
lbl2.setBounds(50,130,50,20);
lbl3.setBounds(50,110,50,20);
lbl4.setBounds(50,90,50,20);
lbl5.setBounds(50,70,50,20);
lblError.setBounds(50,170,200,20);
lblTop.setBounds(70,150,50,20);



btnPush.addActionListener(this);
btnPop.addActionListener(this);
}

public void paint(Graphics g)
{
if(txtNull)
{
lblError.setVisible(true);
lblError.setText("enter integer values");
j--;
txtNull=false;
}

else
{
if(j==-1)
{
lblError.setVisible(true);

if(pop)
{
lblError.setText("Stack Underflow");
}
j++;
}


if(j==0)
{
lbl1.setVisible(false);
lbl2.setVisible(false);
lbl3.setVisible(false);
lbl4.setVisible(false);
lbl5.setVisible(false);
lblError.setVisible(false);
lblTop.setBounds(90,170,50,20);
}
if(j==1)
{
lbl1.setVisible(true);
lbl2.setVisible(false);
lbl3.setVisible(false);
lbl4.setVisible(false);
lbl5.setVisible(false);
lblError.setVisible(false);
lblTop.setBounds(100,150,50,20);
}
if(j==2)
{
lbl1.setVisible(true);
lbl2.setVisible(true);
lbl3.setVisible(false);
lbl4.setVisible(false);
lbl5.setVisible(false);
lblError.setVisible(false);
lblTop.setBounds(100,130,50,20);
}
if(j==3)
{
lbl1.setVisible(true);
lbl2.setVisible(true);
lbl3.setVisible(true);
lbl4.setVisible(false);
lbl5.setVisible(false);
lblError.setVisible(false);
lblTop.setBounds(100,110,50,20);
}
if(j==4)
{
lbl1.setVisible(true);
lbl2.setVisible(true);
lbl3.setVisible(true);
lbl4.setVisible(true);
lbl5.setVisible(false);
lblError.setVisible(false);
lblTop.setBounds(100,90,50,20);
}
if(j==5)
{
lbl1.setVisible(true);
lbl2.setVisible(true);
lbl3.setVisible(true);
lbl4.setVisible(true);
lbl5.setVisible(true);
lblError.setVisible(false);
lblTop.setBounds(100,70,50,20);
}

if(j==6)
{
lblError.setVisible(true);
if(push)
{
lblError.setText("Stack Overflow");
}

j--;
}
}


}

public void actionPerformed(ActionEvent ae)
{

if(ae.getSource() == btnPush)
{

push = true;
pop = false;
txtValue = txtData.getText();

j++;

if(txtValue.equals(""))
{
txtNull = true;
repaint();

}

else
{
try
{
int intValue =Integer.parseInt(txtValue);
}
catch(NumberFormatException nfe)
{
txtNull = true;
repaint();
}
}

if(j==6)
{
repaint();
}
else
{

if(j==1)
{
lbl1.setText(txtData.getText());
}
if(j==2)
{
lbl2.setText(txtData.getText());
}
if(j==3)
{
lbl3.setText(txtData.getText());
}
if(j==4)
{
lbl4.setText(txtData.getText());
}
if(j==5)
{
lbl5.setText(txtData.getText());
}
repaint();
}
}

if(ae.getSource() == btnPop)
{
if(j==0)
{
lblError.setVisible(true);
lblError.setText("Stack Underflow");
}
else
{
pop = true;
push = false;


j--;
repaint();
}
}


}

}

output

0 comments :

Post a Comment