You are on page 1of 15

import javax.swing.

*;

class Stack

int top=-1;

int size=0;

int[] arr;

public Stack(int size)

arr = new int[size];

this.size=size;

public void push (int value)

arr[++top] = value;

public int pop()

return arr[top--];

public void reset()

top=-1;

}
}

public class STACKGUI extends javax.swing.JFrame {

Stack stackx= new Stack(5);

public STACKGUI() {

initComponents();

/**

* This method is called from within the constructor to initialize the form.

* WARNING: Do NOT modify this code. The content of this method is always

* regenerated by the Form Editor.

*/

@SuppressWarnings("unchecked")

// <editor-fold defaultstate="collapsed" desc="Generated Code">

private void initComponents() {

masuk = new javax.swing.JButton();

masuktxt = new javax.swing.JTextField();

hapus = new javax.swing.JButton();

perbahrui = new javax.swing.JButton();

jLabel1 = new javax.swing.JLabel();

arr1 = new javax.swing.JTextField();

arr2 = new javax.swing.JTextField();


arr3 = new javax.swing.JTextField();

arr4 = new javax.swing.JTextField();

arr5 = new javax.swing.JTextField();

jLabel2 = new javax.swing.JLabel();

jLabel3 = new javax.swing.JLabel();

jLabel4 = new javax.swing.JLabel();

jLabel5 = new javax.swing.JLabel();

jLabel6 = new javax.swing.JLabel();

jLabel7 = new javax.swing.JLabel();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

masuk.setText("MASUK");

masuk.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

masukActionPerformed(evt);

});

hapus.setText("HAPUS");

hapus.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

hapusActionPerformed(evt);

});
perbahrui.setText("PERBAHRUI");

perbahrui.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

perbahruiActionPerformed(evt);

});

jLabel1.setText("STACK (TUMPUKAN)");

arr3.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

arr3ActionPerformed(evt);

});

arr5.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

arr5ActionPerformed(evt);

});

jLabel2.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N

jLabel2.setText("GUI STACK (KELOMPOK 2)");


jLabel3.setText("PERTAMA");

jLabel4.setText("KEDUA");

jLabel5.setText("KETIGA");

jLabel6.setText("KEEMPAT");

jLabel7.setText("TERAKHIR");

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());

getContentPane().setLayout(layout);

layout.setHorizontalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addContainerGap()

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addComponent(masuk)

.addGap(18, 18, 18)

.addComponent(masuktxt, javax.swing.GroupLayout.PREFERRED_SIZE, 150,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addGap(18, 18, 18)

.addComponent(hapus)
.addGap(18, 18, 18)

.addComponent(perbahrui))

.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 477,


javax.swing.GroupLayout.PREFERRED_SIZE))

.addContainerGap(22, Short.MAX_VALUE))

.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()

.addGap(0, 0, Short.MAX_VALUE)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
layout.createSequentialGroup()

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addComponent(jLabel3, javax.swing.GroupLayout.Alignment.TRAILING)

.addComponent(jLabel4, javax.swing.GroupLayout.Alignment.TRAILING)

.addComponent(jLabel5, javax.swing.GroupLayout.Alignment.TRAILING)

.addComponent(jLabel6, javax.swing.GroupLayout.Alignment.TRAILING)

.addComponent(jLabel7, javax.swing.GroupLayout.Alignment.TRAILING))

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING,
false)

.addComponent(arr5, javax.swing.GroupLayout.DEFAULT_SIZE, 68,


Short.MAX_VALUE)

.addComponent(arr1, javax.swing.GroupLayout.Alignment.LEADING)

.addComponent(arr2, javax.swing.GroupLayout.Alignment.LEADING)

.addComponent(arr3, javax.swing.GroupLayout.Alignment.LEADING)

.addComponent(arr4, javax.swing.GroupLayout.Alignment.LEADING))

.addGap(138, 138, 138))


.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
layout.createSequentialGroup()

.addComponent(jLabel1)

.addGap(128, 128, 128))))))

);

layout.setVerticalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addContainerGap()

.addComponent(jLabel2)

.addGap(35, 35, 35)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(masuk)

.addComponent(masuktxt, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)

.addComponent(hapus)

.addComponent(perbahrui))

.addGap(18, 18, 18)

.addComponent(jLabel1)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(arr1, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)

.addComponent(jLabel7))

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(arr2, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)

.addComponent(jLabel6))

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(arr3, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)

.addComponent(jLabel5))

.addGap(11, 11, 11)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(arr4, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)

.addComponent(jLabel4))

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(arr5, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)

.addComponent(jLabel3))

.addContainerGap(33, Short.MAX_VALUE))

);

pack();

}// </editor-fold>

private void arr3ActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:

}
private void arr5ActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:

private void masukActionPerformed(java.awt.event.ActionEvent evt) {

stackx.push(Integer.parseInt(masuktxt.getText()));

switch(stackx.top){

case 0:

arr5.setText(masuktxt.getText());

break;

case 1:

arr4.setText(masuktxt.getText());

break;

case 2:

arr3.setText(masuktxt.getText());

break;

case 3:

arr2.setText(masuktxt.getText());

break;

case 4:

arr1.setText(masuktxt.getText());

JOptionPane.showMessageDialog(null,"full");
break;

private void hapusActionPerformed(java.awt.event.ActionEvent evt) {

switch(stackx.top){

case 0:

arr5.setText("");

break;

case 1:

arr4.setText("");

break;

case 2:

arr3.setText("");

break;

case 3:

arr2.setText("");

break;

case 4:

arr1.setText("");

break;

stackx.pop();

}
private void perbahruiActionPerformed(java.awt.event.ActionEvent evt) {

switch(0){

case 0:

arr5.setText("");

case 1:

arr4.setText("");

case 2:

arr3.setText("");

case 3:

arr2.setText("");

case 4:

arr1.setText("");

/**

* @param args the command line arguments

*/

public static void main(String args[]) {


/* Set the Nimbus look and feel */

//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">

/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.

* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html

*/

try {

for (javax.swing.UIManager.LookAndFeelInfo info :


javax.swing.UIManager.getInstalledLookAndFeels()) {

if ("Nimbus".equals(info.getName())) {

javax.swing.UIManager.setLookAndFeel(info.getClassName());

break;

} catch (ClassNotFoundException ex) {

java.util.logging.Logger.getLogger(STACKGUI.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);

} catch (InstantiationException ex) {

java.util.logging.Logger.getLogger(STACKGUI.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);

} catch (IllegalAccessException ex) {

java.util.logging.Logger.getLogger(STACKGUI.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);

} catch (javax.swing.UnsupportedLookAndFeelException ex) {

java.util.logging.Logger.getLogger(STACKGUI.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);

//</editor-fold>
/* Create and display the form */

java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {

new STACKGUI().setVisible(true);

});

// Variables declaration - do not modify

private javax.swing.JTextField arr1;

private javax.swing.JTextField arr2;

private javax.swing.JTextField arr3;

private javax.swing.JTextField arr4;

private javax.swing.JTextField arr5;

private javax.swing.JButton hapus;

private javax.swing.JLabel jLabel1;

private javax.swing.JLabel jLabel2;

private javax.swing.JLabel jLabel3;

private javax.swing.JLabel jLabel4;

private javax.swing.JLabel jLabel5;

private javax.swing.JLabel jLabel6;

private javax.swing.JLabel jLabel7;

private javax.swing.JButton masuk;

private javax.swing.JTextField masuktxt;


private javax.swing.JButton perbahrui;

// End of variables declaration

}
Import javax.swing.*;

Public Stack{

Int top =-1

Int size=0;

Int [] arr;

Public stack (int size){

Arr = new int(size)

You might also like