miércoles, 23 de mayo de 2007

Ventana de validacion

import java.awt.Insets;
import javax.swing.*;
public class ejemplo1 extends JFrame{
JButton aceptar,cancelar;
JPasswordField pas;
JTextField usu;
JLabel lbUsuario,lbPasword;
public ejemplo1() {
setSize(320, 240);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setTitle("Validacion");
setIconImage(new ImageIcon("img/Key.gif").getImage());
getContentPane().setLayout(null);
lbUsuario=new JLabel("Usuario");
lbUsuario.setBounds(50, 60, 80, 20);
getContentPane().add(lbUsuario);
lbPasword=new JLabel("Password");
lbPasword.setBounds(50,100,80,20);
getContentPane().add(lbPasword);
pas=new JPasswordField();
pas.setBounds(140, 100, 100, 20);
getContentPane().add(pas);
usu=new JTextField();
usu.setBounds(140,60,100,20);
getContentPane().add(usu);
aceptar=new JButton("Aceptar",new ImageIcon("img/Redo.gif"));
aceptar.setMargin(new Insets(-10,-10,-10,-10));
aceptar.setBounds(40,150,90,24);
getContentPane().add(aceptar);
cancelar=new JButton("Cancelar",new ImageIcon("img/Delete.gif"));
cancelar.setMargin(new Insets(-10,-10,-10,-10));
cancelar.setBounds(180, 150, 90, 24);
getContentPane().add(cancelar);
}
public static void main(String[] args) {
new ejemplo1().setVisible(true);
}
}

miércoles, 9 de mayo de 2007

SWING con Timer


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class ejemplo extends JFrame implements ActionListener{
Timer timer;
JLabel avion;
JButton abajo;
int x=1,y=10;
public ejemplo(){
setSize(800, 600);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
getContentPane().setLayout(null);
avion=new JLabel(new ImageIcon("avion.gif"));
avion.setBounds(0, 10, 200, 80);
getContentPane().add(avion);
timer=new Timer(1,this);
timer.start();
abajo=new JButton("abajo");
abajo.setBounds(200,500,100,40);
getContentPane().add(abajo);
abajo.addActionListener(this);
}
public static void main(String[] args) {
new ejemplo().setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==timer){
x=x+1;
avion.setLocation(x, y);
}
if(e.getSource()==abajo){
y=y+1;
avion.setLocation(x, y);
}
}
}

Ventana Básica SWING

import javax.swing.*;
public class ventana extends JFrame{
public ventana(){
setSize(400, 300);
setTitle("mi Ventana");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
}
public static void main(String[] args) {
new ventana().setVisible(true);
}
}