Xin CA
Xin CA

Reputation: 27

JFrame window won't show

I'm new on programming using JFrame and i can't figure out why the JFrame that I've created doesn't appear when I run the program.The program runs for about 1-3 second but JFrame wont show up. I used almost the same code to do another program and the JFrame showed up. I can't figure out where the error is, i would appreciate if someone could help.

package ficha10.pkg1;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
import javax.swing.*;

class Temperaturas extends JFrame{
    private JComboBox temp1;
    private JComboBox temp2;
    private javax.swing.JButton botConverte;
    private JTextField numero1;
    private JTextField resultado;
    private JLabel label1;

    void Temperaturas(){
        this.setPreferredSize(new Dimension(500,350));
        this.setTitle("Temperaturas");
        this.setLocation(100,100);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLayout(new FlowLayout());
        this.pack();

        label1 = new JLabel("Número 1");this.add(label1);
        numero1 = new JTextField(); this.add(numero1);
        JLabel label = new JLabel("Resultado");this.add(label);

        resultado = new JTextField(); this.add(resultado);
        resultado.setPreferredSize(new Dimension(60,20));
        resultado.setEditable(false);
        botConverte = new JButton("Converte");this.add(botConverte);
        botConverte.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String t1,t2;
                t1=temp1.getSelectedItem().toString();
                t2=temp2.getSelectedItem().toString();
                if(t1==t2){
                    resultado.setText(""+(Integer.valueOf(numero1.getText())));
                }
                else if(t1=="Fahrenheit" & t2 =="Celsius"){
                    double a = Integer.valueOf(numero1.getText());
                    a = (a-32)*1.8;
                    resultado.setText(""+a);
                }
                else if(t1=="Fahrenheit" & t2 =="Kelvin"){
                    double a = Integer.valueOf(numero1.getText());
                    a = (a-32)*1.8 + 273.15;                    
                    resultado.setText(""+a);
                }
                else if(t1=="Celsius" & t2 =="Fahrenheit"){
                    double a = Integer.valueOf(numero1.getText());
                    a = (a*1.8)+32;                    
                    resultado.setText(""+a);
                }
                else if(t1=="Celsius" & t2 =="Kelvin"){
                    double a = Integer.valueOf(numero1.getText());
                    a = a+273.15;                    
                    resultado.setText(""+a);
                }
                else if(t1=="Kelvin" & t2 =="Celsius"){
                    double a = Integer.valueOf(numero1.getText());
                    a = a-273.15;                    
                    resultado.setText(""+a);
                }
                else if(t1=="Kelvin" & t2 =="Fahrenheit"){
                    double a = Integer.valueOf(numero1.getText());
                    a = (a-273.15)*1.8 + 32;                    
                    resultado.setText(""+a);
                }                
            }
        });

        temp1 = new JComboBox(new String[]{"Fahrenheit","Celsius","Kelvin"});
        this.add(temp1);

        temp2 = new JComboBox(new String[]{"Fahrenheit","Celsius","Kelvin"});
        this.add(temp2);
        this.pack();
    }
}


public class Ficha101 {

    public static void main(String[] args) {
        Temperaturas p = new Temperaturas();
    }
}

Upvotes: 2

Views: 53

Answers (1)

Juan Carlos Mendoza
Juan Carlos Mendoza

Reputation: 5794

Your problem is that void Temperaturas() is a method. Change it to be a constructor and it will work:

public Temperaturas()

Also you have conditional expressions like: t1=="Fahrenheit". This is a wrong way of comparing strings in Java. Read this.

Upvotes: 1

Related Questions