Código:
import java.awt.Container;
import java.awt.Font;
import java.awt.event.*;
import javax.swing.*;
public class Despachadora extends JFrame{
Hilo1 h=new Hilo1();
Hilo2 h2=new Hilo2();
JLabel lblTexto=new JLabel("Despachadora de Agua/Hielo");
ImageIcon im[]={new ImageIcon("Vaso.jpg")};
ImageIcon im1=new ImageIcon("refrigerador.jpg");
ImageIcon im0=new ImageIcon("refrigerador2.jpg");
JLabel lblImagen2=new JLabel(im0);
JLabel lblImagen=new JLabel(im1);
JLabel lblVaso=new JLabel(im[0]);
JButton btn1=new JButton("$5 Chico");
JButton btn2=new JButton("$8 Mediano");
JButton btn5=new JButton("$12 Grande");
JLabel lblTex=new JLabel("Tu cambio es: $");
JLabel lblCambio=new JLabel();
JLabel lblDinero=new JLabel("Introduce tu dinero: $");
JTextField txtDinero=new JTextField();
int contador=0;
JPanel panel=new JPanel();
int posx, posy;
Despachadora(){
setTitle("Despachadora de Hielo y Agua");
setSize(415,625);
panel.setLayout(null);
lblDinero.setFont(new Font("Arial", Font.BOLD, 15));
lblDinero.setBounds(200,10,160,50);
panel.add(lblDinero);
txtDinero.setBounds(355,25,30,20);
panel.add(txtDinero);
lblTex.setFont(new Font("Arial", Font.BOLD,15));
lblTex.setBounds(10,10,110,50);
panel.add(lblTex);
lblTex.setFont(new Font("Arial", Font.BOLD,15));
lblCambio.setBounds(130,10,100,50);
panel.add(lblCambio);
btn1.setBounds(40,70,90,20);
panel.add(btn1);
btn2.setBounds(150,70,100,20);
panel.add(btn2);
btn5.setBounds(270,70,100,20);
panel.add(btn5);
lblVaso.setBounds(160,290,100,150);
panel.add(lblVaso);
lblImagen2.setBounds(160,152,100,100);
panel.add(lblImagen2);
lblImagen.setBounds(0,0,415,625);
panel.add(lblImagen);
h2.start();
Container c=getContentPane();
c.add(panel);
btn1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent uno){
int res,can,prec;
//for(int i=1; i<100; i++){
can = Integer.parseInt(txtDinero.getText());
int c=5;
res=can-c;
lblCambio.setText(String.valueOf(res));
//}
txtDinero.setText("");
h.start();
}
});
btn2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent dos){
int res,can,prec;
can = Integer.parseInt(txtDinero.getText());
int m=8;
res=can-m;
lblCambio.setText(String.valueOf(res));
txtDinero.setText("");
h.start();
}
});
btn5.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent cinco){
int res,can,prec;
can = Integer.parseInt(txtDinero.getText());
int g=12;
res=can-g;
lblCambio.setText(String.valueOf(res));
txtDinero.setText("");
h.start();
}
});
}
class Hilo1 extends Thread{
public void run(){
try{
Thread.sleep(5000);
dispose();
Despachadora2 d=new Despachadora2(txtDinero.getText());
d.setVisible(true);
//new Despachadora2().setVisible(true);
}catch(Exception er){}
}
}
class Hilo2 extends Thread{
public void run(){
try{
while(true){
Thread.sleep(1000);
if(contador<=3){
contador++;
lblImagen.setIcon(im[contador]);
}
else{
contador=0;
lblImagen.setIcon(im[0]);
}
}
}catch(Exception er){}
}
}
}
No hay comentarios:
Publicar un comentario