domingo, 15 de enero de 2012

probando jlist

package paquetito;

import java.awt.Color;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

public class clasesita
{
 
    private JFrame v;
    private JButton b;
    private JButton algo;
   
    private JTextField t;
   
 
    public static void main(String [] args)
    {
        new clasesita();
    }
   
 
    public clasesita()
    {
       
        v = new JFrame("Ventana Hola Mundo");
        v.getContentPane().setLayout(null);
       
       
        b = new JButton("Púlsame");
        algo = new JButton("aceptar");
        v.getContentPane().add (b);
       
       
        String subject[] = {"cadena1", "cadena2", "cadena3", "cadena4"};
        JPanel panel = new JPanel();
        JList list = new JList(subject);
       
        v.getContentPane().add(list);
        list.setBounds(new Rectangle(350,10,100,40));
       
       
        t = new JTextField(10);
        v.getContentPane().add(t);
        v.getContentPane().add (algo);
        algo.setBounds(new Rectangle(120,10,100,23));
        b.setBounds(new Rectangle(10,10,100,23));
        t.setBounds(new Rectangle(240,10,100,23));
       
        b.addActionListener(new ActionListener()
        {
        public void actionPerformed(ActionEvent e)
        {
                t.setText ("webada");
               
        }
        });
       
        algo.addActionListener(new ActionListener()
        {
        public void actionPerformed(ActionEvent e)
        {
        String alg = null ;
        alg = b.getText();
                t.setText (alg);
                b.setBackground(Color.blue);
                JOptionPane.showMessageDialog(v, "mensajito xD",
                   "titulo de ventanita",
                   JOptionPane.INFORMATION_MESSAGE);
        }
        });
     
        v.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
       
        v.setSize(500, 500);
             
        v.setVisible(true);
       
    }

}

viernes, 13 de enero de 2012

hola mundo con boton textbox y evento clic

/**
 * Javier Abellán. 10 Junio 2006
 *
 * Hola mundo con SWING en Java
 */
package chuidiang.ejemplos;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

/**
 * Construye una ventana con un botón y una campo de texto. Cuando se pulsa
 * el botón, escribe "Hola mundo" en el campo de texto.
 *
 * @author Chuidiang
 */
public class HolaMundoSwing
{
    /** La ventana */
    private JFrame v;
   
    /** El botón */
    private JButton b;
   
    /** El campo de texto */
    private JTextField t;
   
    /**
     * Crea una instancia de HolaMundoSwing
     * @param args
     */
    public static void main(String [] args)
    {
        new HolaMundoSwing();
    }
   
    /**
     * Crea la ventana, inicializa todo y la visualiza
     */
    public HolaMundoSwing()
    {
        // Nueva ventana. Se el pone un FlowLayout para que el botón y campo
        // de texto quede alineados.
        v = new JFrame("Ventana Hola Mundo");
        v.getContentPane().setLayout(new FlowLayout());
       
        // Se crea el botón y se mete en la ventana
        b = new JButton("Púlsame");
        v.getContentPane().add (b);
       
        // Se crea el campo de texto y se mete en la ventana
        t = new JTextField(20);
        v.getContentPane().add(t);
       
        // Se le dice al botón qué tiene que hacer cuando lo pulsemos.
        b.addActionListener(new ActionListener()
        {
             public void actionPerformed(ActionEvent e)
             {
                t.setText ("Hola mundo");
             }
        });
       
        // Se le dice a la ventana que termine el programa cuando se la cierre
        v.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
       
        // Se le da un tamaño automático a la ventana para que quepa todo su
        // contenido.
        v.pack();
       
        // Se hace visible la ventana
        v.setVisible(true);
    }

}

jframe editado

package paquetito;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

/**
 * Construye una ventana con un botón y una campo de texto. Cuando se pulsa
 * el botón, escribe "Hola mundo" en el campo de texto.
 *
 * @author Chuidiang
 */
public class clasesita
{
    /** La ventana */
    private JFrame v;
   
    /** El botón */
    private JButton b;
    private JButton algo;
    /** El campo de texto */
    private JTextField t;
   
    /**
     * Crea una instancia de HolaMundoSwing
     * @param args
     */
    public static void main(String [] args)
    {
        new clasesita();
    }
   
    /**
     * Crea la ventana, inicializa todo y la visualiza
     */
    public clasesita()
    {
        // Nueva ventana. Se el pone un FlowLayout para que el botón y campo
        // de texto quede alineados.
        v = new JFrame("Ventana Hola Mundo");
        v.getContentPane().setLayout(null);
       
        // Se crea el botón y se mete en la ventana
        b = new JButton("Púlsame");
        algo = new JButton("aceptar");
        v.getContentPane().add (b);
       
        // Se crea el campo de texto y se mete en la ventana
        t = new JTextField(10);
        v.getContentPane().add(t);
        v.getContentPane().add (algo);
        algo.setBounds(new Rectangle(120,10,100,23));
        b.setBounds(new Rectangle(10,10,100,23));
        t.setBounds(new Rectangle(240,10,100,23));
        // Se le dice al botón qué tiene que hacer cuando lo pulsemos.
        b.addActionListener(new ActionListener()
        {
             public void actionPerformed(ActionEvent e)
             {
                t.setText ("Hola mundo");
             }
        });
       
        algo.addActionListener(new ActionListener()
        {
             public void actionPerformed(ActionEvent e)
             {
                t.setText ("jhonny");
                b.setBackground(Color.blue);
                JOptionPane.showMessageDialog(v, "mensajito xD",
                        "titulo de ventanita",
                        JOptionPane.QUESTION_MESSAGE);
             }
        });
        // Se le dice a la ventana que termine el programa cuando se la cierre
        v.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
       
        // Se le da un tamaño automático a la ventana para que quepa todo su
        // contenido.
       
        v.setSize(500, 500);
        // Se hace visible la ventana
       
        v.setVisible(true);
       
    }

}

miércoles, 14 de diciembre de 2011

ejemplo de cola java con menu

package paquetito;
import java.util.*;
public class Cola {
    public static void main( String args[] ){
       Scanner leer = new Scanner(System.in);
      
       colagenerica obj = new colagenerica();
      
       int op;
       int num;
      
       do{
          menu();
          op = leer.nextInt();
         
          switch(op){
              case 1:
                     System.out.println( "Numero a insertar" );
                     num = leer.nextInt();
                     if(obj.inscola(num)){
                        System.out.println( "fre"+obj.fre+"fin"+obj.fin+"aux"+obj.max );
                        System.out.println( "El numero "+num+" se inserto en la cola ["+obj.dret+"]" );
                        System.out.println();
                     }
                     else{
                          System.out.println( "Cola llena" );
                     }
                     break;
              case 2:
                    if(obj.retcola()){
                       System.out.println( "El dato retirado fue: "+obj.dret );
                    }
                    else{
                        System.out.println( "Cola vacia" );
                    }
                    break;
              case 3:
                    if(obj.fre==-1 && obj.fin==-1){
                       System.out.println( "Cola vacia" );
                    }
                    else{
                         System.out.println( "Estado de la cola:" );
                         for(int i=obj.fre; i<=obj.fin; i++){
                            System.out.print(obj.c[i]+" \t");
                         }
                         break;
                    }
          }
       }
       while(op != 4);
    }
   
    public static void menu(){    
       System.out.println( "\t Menu para colas \n" );
       System.out.println( "1.- Insertar" );
       System.out.println( "2.- Retirar" );
       System.out.println( "3.- Estado" );
       System.out.println( "4.- Fin" );
       System.out.println( "\n Selecciona" );
    }
}


class colagenerica
 {
      public int max;
      protected Object dret;
      public Object c[];
      public int fre = -1;
      public int fin = -1;
     
      public colagenerica()
       {
            max=20;
            c=new Object [max];
       }
     
      public colagenerica(int n)
         { max=n;
          c=new Object [max];
         }
     
      public boolean colallena(int fin,int max)
      {
      boolean llena;
      if (fin==max-1)
        llena=true;
        else
          llena=false;
      return llena;
     }
    
    public boolean colavacia(int fre)
      {
      boolean vacia;
      if (fre==-1)
        vacia=true;
        else
          vacia=false;
      return vacia;
     }  
       
      public boolean inscola(Object dato)
       {
            if (fin==max-1)
               return false;
            fin++;
            c[fin] = dato;
            if (fin==0)
               fre=0;
            return true;
     }
       
      public boolean retcola()
        {
            if (fre ==-1)
               return false;
            dret=c[fre];
            if (fre==fin)
              {
                  fre=-1;
                  fin=-1;
              }
             else
               fre++;
            return true;
        }
 }