les pongo los dos codigos de la pila ojala me puedan ayudar con el llenado de la pila todo esto es en netbeans ojala pudieran hacerlo no se en que clase se tenga que agregar
package pilagrafica;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
public class PruebaPila extends javax.swing.JFrame {
private pila pila;
public PruebaPila() {
initComponents();
setLocationRelativeTo( null );
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
jPanelPila = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jPanel2 = new javax.swing.JPanel();
jCrear = new javax.swing.JButton();
jApilar = new javax.swing.JButton();
jVaciar = new javax.swing.JButton();
jDesapilar = new javax.swing.JButton();
jButton1 = new javax.swing.JButton();
jMensaje = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Pila Grafica");
setResizable(false);
jPanelPila.setBackground(new java.awt.Color(255, 255, 255));
jPanelPila.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 2, true));
jPanelPila.setLayout(new java.awt.GridLayout(1, 0));
jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14));
jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel1.setText("Pila Grafica");
jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Opciones"));
jCrear.setText("Crear");
jCrear.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jCrearActionPerformed(evt);
}
});
jApilar.setText("Apilar");
jApilar.setEnabled(false);
jApilar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jApilarActionPerformed(evt);
}
});
jVaciar.setText("Vaciar");
jVaciar.setEnabled(false);
jVaciar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jVaciarActionPerformed(evt);
}
});
jDesapilar.setText("Desapilar");
jDesapilar.setEnabled(false);
jDesapilar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jDesapilarActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jApilar, javax.swing.GroupLayout.DEFAULT_SIZE, 181, Short.MAX_VALUE)
.addComponent(jCrear, javax.swing.GroupLayout.DEFAULT_SIZE, 181, Short.MAX_VALUE)
.addComponent(jDesapilar, javax.swing.GroupLayout.DEFAULT_SIZE, 181, Short.MAX_VALUE)
.addComponent(jVaciar, javax.swing.GroupLayout.DEFAULT_SIZE, 181, Short.MAX_VALUE))
.addContainerGap())
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jCrear)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jApilar)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jDesapilar)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jVaciar)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
jButton1.setText("Salir ahora");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jMensaje.setFont(new java.awt.Font("Tahoma", 1, 36)); // NOI18N
jMensaje.setForeground(java.awt.Color.red);
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.TRAILING)
.addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 180, Short.MAX_VALUE)
.addComponent(jPanelPila, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 180, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jMensaje, javax.swing.GroupLayout.DEFAULT_SIZE, 213, Short.MAX_VALUE)
.addComponent(jButton1, javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jPanelPila, javax.swing.GroupLayout.DEFAULT_SIZE, 356, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(26, 26, 26)
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jMensaje, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 97, Short.MAX_VALUE)
.addComponent(jButton1)))
.addContainerGap())
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void jCrearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCrearActionPerformed
try {
String strTam = JOptionPane.showInputDialog(null, "Introduzca El Tamaño De La Pila:");
int tam = Integer.parseInt( strTam );
if( tam > 10 ) {
JOptionPane.showMessageDialog(null, "Solo Se Puede Crear Una Pila Con Un Maximo De 10 elementos",
"ADVERTENCIA", JOptionPane.WARNING_MESSAGE );
} else if( tam == 0 ) {
JOptionPane.showMessageDialog(null, "No Se Puede Aceptar Ese Valor", "ERROR",
JOptionPane.ERROR_MESSAGE);
} else {
pila = new pila(tam, jPanelPila);
pila.start();
jCrear.setEnabled(false);
jApilar.setEnabled(true);
jDesapilar.setEnabled(true);
jVaciar.setEnabled(true);
}
} catch( NumberFormatException e ) {
JOptionPane.showMessageDialog(null, "Solo esta permitido introducir numeros enteros.",
"ERROR", JOptionPane.ERROR_MESSAGE);
}
}//GEN-LAST:event_jCrearActionPerformed
private void jApilarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jApilarActionPerformed
if( pila.estaLlena() ) {
JOptionPane.showMessageDialog(null, "Ya No Se Puede Insertar Mas.", "PILA LLENA",
JOptionPane.WARNING_MESSAGE );
} else {
String nombre = JOptionPane.showInputDialog(null, "Introduzca Datos A La Pila:");
pila.apilar( nombre );
}
}//GEN-LAST:event_jApilarActionPerformed
private void jDesapilarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jDesapilarActionPerformed
pila.desapilar();
}//GEN-LAST:event_jDesapilarActionPerformed
private void jVaciarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jVaciarActionPerformed
pila.vaciar();
}//GEN-LAST:event_jVaciarActionPerformed
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
System.exit(0);
}//GEN-LAST:event_jButton1ActionPerformed
public static void main(String args[]) {
try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());}
catch( Exception e ) { e.printStackTrace(); }
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new PruebaPila().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton jApilar;
private javax.swing.JButton jButton1;
private javax.swing.JButton jCrear;
private javax.swing.JButton jDesapilar;
private javax.swing.JLabel jLabel1;
public static javax.swing.JLabel jMensaje;
private javax.swing.JPanel jPanel2;
private javax.swing.JPanel jPanelPila;
private javax.swing.JButton jVaciar;
// End of variables declaration//GEN-END:variables
}
y aqui el otro
package pilagrafica;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.LineBorder;
/**
* Esta clase representa una simulacion de una pila
* grafica.
* @author ChEmO
*/
public class pila extends Thread {
//Representa la longitud que tendra la pila
private int longitud;
//Representa la cima de la pila
private int cima;
//Representa el tope maximo de la pila
private int tope;
//Arreglo de JTextField que simulan una pila
private JTextField pila[];
//Variable necesaria para saber cuando iniciar y detener el hilo
private boolean seContinua = true;
/**
* Constructor por defecto encargado de construir un arreglo
* de JTextFields simulando una pila grafica y, despues se
* agrega dentro de un panel.
* --------------------------
* @param tam - El tamaño de la pila
* @param panel - El panel en donde se agregara la pila grafica
*/
public pila( int tam , JPanel panel ) {
this.longitud = tam;
pila = new JTextField[ longitud ];
cima = -1;
tope = pila.length-1;
panel.setLayout( new GridLayout( longitud, 0 ) );
for( int i = pila.length-1; i >= 0; i-- )
{
pila = new JTextField("");
pila.setEditable( false );
pila.setOpaque( true );
pila.setBackground( Color.WHITE );
pila.setFont( new Font("Arial",1,20 ) );
pila.setHorizontalAlignment( JTextField.CENTER );
pila.setForeground( Color.BLACK );
pila.setBorder( new LineBorder(Color.BLACK) );
panel.add( pila );
panel.updateUI();
}
}
@Override
public void run() {
while ( seContinua )
{
if ( estaLlena() )
{ //Si la pila esta llena
PruebaPila.jMensaje.setText("Pila Llena");
for( int i = pila.length-1; i >= 0; i-- ) {
try
{
sleep(150);
pila.setBackground(Color.ORANGE);
}
catch (InterruptedException ex)
{
System.out.println("Error: " + ex.getMessage() );
}
}
} else
{ //Si no esta llena
for( int i = pila.length-1; i >= 0; i-- ) {
try
{
sleep(150);
pila.setBackground(Color.WHITE);
pila.setForeground(Color.BLACK);
}
catch (InterruptedException ex)
{
System.out.println("Error: " + ex.getMessage() );
}
}
}
PruebaPila.jMensaje.setText("");
}
}
/**
* Metodo encargado de apilar un elemento dentro de la pila
* @param nombre - El nombre a insertar
*/
public void apilar( String nombre ) {
if( estaLlena() )
{
JOptionPane.showMessageDialog(null, "La Pila Esta Totalmente Llena.", "Pila Llena",
JOptionPane.WARNING_MESSAGE);
} else if( nombre.isEmpty() )
{
JOptionPane.showMessageDialog(null, "Tiene Que Introducir Un Nombre Para Continuar.",
"ADVERTENCIA", JOptionPane.WARNING_MESSAGE );
} else
{
cima++;
pila[ cima ].setText( nombre );
pila[ cima ].setBackground( Color.WHITE );
}
}
/**
* Metodo encargado de desapilar un elemento dentro de la pila
*/
public void desapilar() {
if( estaVacia() )
{
JOptionPane.showMessageDialog(null, "La Pila Esta Totelmente Vacia.", "Pila Vacia",
JOptionPane.WARNING_MESSAGE);
} else
{
pila[ cima ].setText("");
pila[ cima ].setBackground( Color.WHITE);
cima--;
}
}
/**
* Metodo encargado de vaciar completamente la pila
*/
public void vaciar() {
if( estaVacia() )
{
JOptionPane.showMessageDialog(null, "La Pila Ya Se Encuentra Vacia.", "Pila Vacia",
JOptionPane.WARNING_MESSAGE);
} else
{
for( int i = pila.length-1; i >= 0; i-- )
{
cima = -1;
tope = pila.length-1;
pila.setText("");
}
}
}
/**
* Metodo encargado de detener la ejecucion del hilo
*/
public void detente() { seContinua = false; }
/**
* Metodo encargado de iniciar la ejecucion del hilo
*/
public void reiniciar() { seContinua = true; }
/**
* Metodo encargado de retornar el numero total de espacios
* disponibles dentro de la pila
* -----------------------------
* @return - El total de espacios sisponibles
*/
public int obtenerEspacios() { return tope - cima; }
public boolean estaLlena() {
if( cima == tope ) { return true; }
return false;
}
public boolean estaVacia() {
if( cima == -1 ) { return true; }
return false;
}
}