Problema com Jcombobox

1 resposta
B

tenho o codigo abaixo para retornar a idade de uma pessoa onde
o user escolhe em um combobox para dia, mes e ano
o mes está ok
agora como pego o valor do combobox dia e ano
tipo seleciona o ano de 1980 e eu consiga retornar o valor 1980
ao inves do .getSelectedIndex

/*
 * As.java
 *
 * Created on 14 de Abril de 2005, 19:01
 */

package pos;

import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.WindowConstants;
import java.awt.event.*;
import java.util.GregorianCalendar;
import javax.swing.JOptionPane;

public class Idade extends JFrame implements ActionListener {
    JLabel ldia = new JLabel( "Dia" );    
    JLabel lmes = new JLabel( "Mês" );    
    JLabel lano = new JLabel( "Ano" );    
    
    JButton button1 = new JButton("Calcular Agora");
    
    JComboBox cBDia = new JComboBox();
    JComboBox cBMes = new JComboBox();
    JComboBox cBAno = new JComboBox();  
     int MES=0;
    
    /** Creates a new instance of As */
    public Idade() {
        super("Exer05");
    setSize(400,400);  
    inicializa();   
    }
    public void inicializa(){

        
    for (Integer i = 1; i <= 31; i++){
       cBDia.addItem(makeObj(i.toString()));
    } 
    cBMes.addItem(makeObj("JANEIRO"));
    cBMes.addItem(makeObj("FEVEREIRO"));
    cBMes.addItem(makeObj("MARÇO"));
    cBMes.addItem(makeObj("ABRIL"));
    cBMes.addItem(makeObj("MAIO"));
    cBMes.addItem(makeObj("JUNHO"));
    cBMes.addItem(makeObj("JULHO"));
    cBMes.addItem(makeObj("AGOSTO"));
    cBMes.addItem(makeObj("SETEMBRO"));
    cBMes.addItem(makeObj("OUTUBRO"));
    cBMes.addItem(makeObj("NOVEMBRO"));
    cBMes.addItem(makeObj("DEZEMBRO"));
    for (Integer i = 2005; i >= 1900; i--){
       cBAno.addItem(makeObj(i.toString()));
    } 
    
    setLayout( new FlowLayout() );            
    setDefaultCloseOperation( WindowConstants.EXIT_ON_CLOSE );
    button1.addActionListener(this);
    add( ldia );
    add( cBDia );
    add( lmes );
    add( cBMes );
    add( lano );
    add( cBAno );
    add(button1);
   
    
    pack();    
  }
  private Object makeObj(final String item)  {
     return new Object() { 
         public String toString() {      
             return item; 
         } 
     };
  }

  
  public static void main(String args[]) {
        
       new Idade().setVisible(true);
        
  }
  public void retornaMes(){
       switch (cBMes.getSelectedIndex()){
              case 0:MES = 1;
                     break;
              case 1:MES = 2;
                     break;
              case 2:MES = 3;
                     break;
              case 3:MES = 4;
                     break;
              case 4:MES = 5;
                     break;
              case 5:MES = 6;
                     break;
              case 6:MES = 7;
                     break;
              case 7:MES = 8;
                     break;
              case 8:MES = 9;
                     break;  
              case 9:MES = 10;
                     break; 
              case 10:MES = 11;
                     break; 
              case 11:MES = 12;
                     break;  
          }      
    } 

  public void actionPerformed(ActionEvent e) {
      if (e.getSource() == button1){
          retornaMes();
         
          
          int ANO = AQUI RETORNAR ANO;    
          
          int DIA = AQUI RETORNAR DIA;
          GregorianCalendar c = new GregorianCalendar(ANO, MES, DIA); 
          long dif = System.currentTimeMillis() - c.getTimeInMillis(); 
                  //dif é a idade em milisegundos 
             dif = dif / 1000; //idade em segundos 
             dif = dif / 60; //idade em minutos 
             dif = dif / 60; //idade em horas 
             dif = dif / 24; //idade em dias 
             int idade = (int) dif / 365; //idade em anos 

            JOptionPane.showMessageDialog(null,"Sua idade é: "+ idade); 
  
        }
    }
  
}

1 Resposta

F

cria uma nova Integer passando comboBox.getSelectedItem().toString() como argumento…

isso deve resolver…

Criado 14 de abril de 2005
Ultima resposta 14 de abr. de 2005
Respostas 1
Participantes 2