Confusao com combobox

4 respostas
A

Galera estamos ai denovo ..
eu queria fazer um programa assim :
que tivesse um text field.. pra inserir um valor, e ele convetia para graus celcius ou fahrenheit ..
ai exibe o valor..quando aperta o botao calcular, eu e um amigo meu erramos..
e eu nao consigo alterar.. é apenas 1 combobox que tem que ter as opções CELSIUS OU FAHRENHEIT...
se puderem me ajudar..
isso é o que fizemos.. e nao conseguimos mexer
abraços

import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class Conversor extends JFrame {	
private static final long serialVersionUID = 1L;	
private JComboBox entrada, saida;	
private JTextField visores[];		
public Conversor() {		
setTitle("[Conversor]");		
setSize(250, 250);		
setResizable(false);		
setLocationRelativeTo(null);		
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);				
iniciarComponentes();					
setVisible(true);	}		
private void iniciarComponentes() {		
setLayout(new FlowLayout(FlowLayout.CENTER));				
visores = new  JTextField[2];		
for(int i = 0; i < visores.length; i++){			
visores[i] = new JTextField();		
visores[i].setPreferredSize(new Dimension(240,32));				
}					
visores[1].setEditable(false);			
String temperaturas[] = {"Celsius", "Fahrenheit", "Kelvin"};		
entrada = new JComboBox(temperaturas);	
saida = new JComboBox(temperaturas);				
Container c = new Container();				
c.setLayout(new GridLayout(3, 2));				
JButton converte = new JButton("Converter!");	
converte.addActionListener(new ActionListener() {						@Override			
public void actionPerformed(ActionEvent arg0) {				
try {					
float a = Float.parseFloat(visores[0].getText());					
int b = entrada.getSelectedIndex();					
int c = saida.getSelectedIndex();									if(b == c)						
visores[1].setText(a + "");					
else if(b == 0 && c == 1)						
visores[1].setText((a * 1.8 + 32) + "");					
else if(b == 0 && c == 2)						
visores[1].setText((a + 273.15) + "");					
else if(b == 1 && c == 0)						
visores[1].setText(((a - 32) / 1.8) + "");					
else if(b == 1 && c == 2)						
visores[1].setText(((a - 32) / 1.8 + 273.15) + "");					
else if(b == 2 && c == 0)						
visores[1].setText((a - 273.15) + "");				
else if(b == 2 && c == 1)						
visores[1].setText((a - 273.15 - 32)/1.8 + "");					visores[0].setForeground(Color.BLACK);				
}				
catch (Exception e) {				
visores[0].setForeground(Color.RED);				
}			}		});				
c.add(new JLabel("De"));		
c.add(entrada);
		
c.add(new JLabel("Para"));		
c.add(saida);				
add(visores[0]);			
add(c);			
add(converte);
add(visores[1]);			}	
public static void main(String[] args) {		
new Conversor();	}}

4 Respostas

N

Cara tenta edentar o teu código que fica mais fácil de entender…

Anime

Seu código esta bem confuso mesmo, da uma olhadinha aqui http://download.oracle.com/javase/tutorial/uiswing/components/combobox.html

yhhik

cara da uma olhada…
uma maneira simples de se fazer isso…

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class guj extends JFrame{
	
	private JComboBox cb;
	private JButton butao;
	private JTextField txt;
	private JPanel painel;
	
	public guj(){
		
		painel=new JPanel();
		painel.setLayout(new FlowLayout());
		
		txt=new JTextField(15);
		cb=new JComboBox();
		
		cb.setModel(new DefaultComboBoxModel(new String[] { "celcius","fahrenheit"}));
		
		butao=new JButton("calcular");
		butao.addActionListener(new Calcular());
		
		painel.add(txt);
		painel.add(cb);
		painel.add(butao);
		
		this.add(painel);
		this.setSize(300,200);
		this.setResizable(false);
		this.setVisible(true);
		
		
	}
	
	private class Calcular implements ActionListener{
		
		float temperatura=0;

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			if(cb.getSelectedItem()=="celcius"){
				
			temperatura=Float.parseFloat(txt.getText());
			//coloque aki seu calculo celsios;
								
			}else{
				
				temperatura=Float.parseFloat(txt.getText());
				//coloque aki seu calculo fahr;
				
			}
			
		}	
		
	}

	public static void main(String[]args){
		
		new guj();
		
	}
}
A

vou dar uma estudada, qualquer coisa volto ai,
obrigado

Criado 28 de outubro de 2011
Ultima resposta 29 de out. de 2011
Respostas 4
Participantes 4