我是使用JFrame编程的新手,我无法弄清楚为什么我运行程序时出现的JFrame没有出现。程序运行大约1 -3秒,但JFrame不会出现。 我使用几乎相同的代码来执行另一个程序,JFrame出现了。 我无法弄清楚错误的位置,如果有人可以提供帮助,我将不胜感激。
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();
}
}
答案 0 :(得分:1)
您的问题是void Temperaturas()
是一种方法。将其更改为构造函数,它将起作用:
public Temperaturas()
你也有条件表达式,如:t1=="Fahrenheit"
。这是比较Java中字符串的错误方法。 Read this