Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package javaapplication130;
- import java.awt.event.*;
- import javax.swing.*;
- /**
- *
- * @author Daniel
- */
- public class NúmeroDeSuerte extends JFrame implements ActionListener, KeyListener{
- private JLabel label1, label2, label3, label4;
- private JTextField texto3;
- private JButton boton1;
- private JComboBox combo1, combo2;
- public NúmeroDeSuerte(){
- super("Número De La Suerte");
- this.setLayout(null);
- label1=new JLabel("Día:");
- label1.setBounds(10,10,200,20);
- add(label1);
- combo1=new JComboBox();
- combo1.setBounds(40,10,50,20);
- for(int i=1; i<=31; i++){
- combo1.addItem(i);
- }
- add(combo1);
- label2=new JLabel("Mes:");
- label2.setBounds(100,10,50,20);
- add(label2);
- combo2=new JComboBox();
- combo2.setBounds(140, 10, 90, 20);
- String[] meses={"Enero", "Febero", "Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre"};
- for(int i=0; i<meses.length; i++){
- combo2.addItem(meses[i]);
- }
- add(combo2);
- label3=new JLabel("Año:");
- label3.setBounds(240, 10, 50, 20);
- add(label3);
- texto3=new JTextField();
- texto3.setBounds(280,10,50,20);
- texto3.addKeyListener(this);
- add(texto3);
- boton1=new JButton("Adivinar");
- boton1.setBounds(30, 40, 100, 20);
- boton1.addActionListener(this);
- boton1.addKeyListener(this);
- add(boton1);
- label4=new JLabel(" ");
- label4.setBounds(140,40,200,20);
- add(label4);
- }
- public void numeroDeLaSuerte(){
- try{
- int dia=(Integer)combo1.getSelectedItem();
- int mes=(Integer)combo2.getSelectedIndex()+1;
- int año=Integer.parseInt(texto3.getText());
- if(dia<=31 && dia>=1){
- int suma=dia+mes+año;
- int temp=0;
- while(suma !=0){
- temp+=suma%10;
- suma/=10;
- }
- suma=temp;
- temp=0;
- while(suma !=0){
- temp+=suma%10;
- suma/=10;
- }
- if(temp==10)
- temp=1;
- label4.setText("Su Número De La Suerte Es: "+Integer.valueOf(temp));
- }
- }catch(Exception e){
- label4.setText("Escriba Un Número, Idiota");
- }
- }
- public void actionPerformed(ActionEvent e){
- if(e.getSource()==boton1){
- numeroDeLaSuerte();
- }
- }
- public void keyTyped(KeyEvent e) {
- }
- public void keyPressed(KeyEvent e) {
- switch(e.getKeyCode()){
- case KeyEvent.VK_ENTER:
- numeroDeLaSuerte();
- break;
- }
- }
- public void keyReleased(KeyEvent e) {
- }
- public static void main(String[] args) {
- NúmeroDeSuerte f1=new NúmeroDeSuerte();
- f1.setBounds(0,0,360,120);
- f1.setLocation(700, 440);
- f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- f1.setVisible(true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement