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);
       
    }

}