Você está na página 1de 12

Trabajo autnomo: colas

package colas;
public class aplicacioncolass {
public static void main(String[] args) {
}
}

package colas;
import javax.swing.JOptionPane;
public class JFrameCola extends javax.swing.JFrame {
String ColaN[]; int first, last;
public JFrameCola() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
jTextField1 = new javax.swing.JTextField();
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jButton4 = new javax.swing.JButton();
jButton5 = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
jLabel1.setText("Valor");
jButton1.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
jButton1.setText("Crea Cola");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jTextField1.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jScrollPane1.setViewportView(jTextArea1);
jButton2.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
jButton2.setText("incluir (Cola)");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jButton3.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
jButton3.setText("Elimina (Cola)");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
jButton4.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
jButton4.setText("Acceso (cola)");
jButton4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton4ActionPerformed(evt);
}
});
jButton5.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
jButton5.setText("Mostrar Arreglo");
jButton5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton5ActionPerformed(evt);
}
});

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


getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(56, 56, 56)
.addComponent(jLabel1)
.addGap(55, 55, 55)
.addComponent(jTextField1,
javax.swing.GroupLayout.PREFERRED_SIZE, 96,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1,
javax.swing.GroupLayout.PREFERRED_SIZE, 340,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(29, 29, 29)
.addComponent(jButton1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton3)
.addGroup(layout.createSequentialGroup()
.addComponent(jButton2)
.addGap(37, 37, 37)
.addComponent(jButton5))
.addComponent(jButton4))))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1))
.addGap(38, 38, 38)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1)
.addComponent(jButton2)
.addComponent(jButton5))

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton4)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE,
132, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
pack();
}// </editor-fold>
public void creaCola(int N){
ColaN= new String [N];colaVacia();
}
public void colaVacia(){
first =-1; last =-1;
}
public String incluirElemento(String nombre){
String msg=""; int indMax= ColaN.length-1;
if ( last< indMax){
last++; ColaN[last]= nombre;
if (first == -1)
first = 0;
}
else msg="Cola llena";
return msg;
}
public String eliminaElemento(){
String msg="";
if(first >-1){
msg=ColaN[first]; first++;
if(first > last)
colaVacia();
}
else msg ="Cola Vacia";
return msg;
}
public String acceso(){
String msg="";
if(first >-1)
msg=ColaN[first];
else msg ="Cola Vacia";
return msg;
}
public String recorreArreglo(){
String cadena="";
if( first >-1){
for(int i=first; i <= last;i++)
cadena+=ColaN[i]+" ";

}
return cadena;
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
int N= Integer.parseInt(jTextField1.getText());
creaCola(N);
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
String nombre=jTextField1.getText();
String msg= incluirElemento(nombre);
if(msg.length()!=0)
JOptionPane.showMessageDialog(rootPane, msg);
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
String msg=eliminaElemento();
JOptionPane.showMessageDialog(rootPane, msg);
}
private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {
jTextArea1.setText(recorreArreglo());
}
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
JOptionPane.showMessageDialog(rootPane, acceso());
}
public static void main(String args[]) {
//<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(JFrameCola.class.getName()).log(java.util.logging.Leve
l.SEVERE, null, ex);
} catch (InstantiationException ex) {

java.util.logging.Logger.getLogger(JFrameCola.class.getName()).log(java.util.logging.Leve
l.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(JFrameCola.class.getName()).log(java.util.logging.Leve
l.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(JFrameCola.class.getName()).log(java.util.logging.Leve
l.SEVERE, null, ex);
}
//</editor-fold>
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new JFrameCola().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JButton jButton5;
private javax.swing.JLabel jLabel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea jTextArea1;
private javax.swing.JTextField jTextField1;
// End of variables declaration
}

Você também pode gostar