可见
我是这个地方的新手,但是我喜欢我在Stackoverflow上发现的一切。现在,我正在尝试建立在我在Java课程中为最后一个项目制作的旧程序,但可惜我已经打动了道路。
我的程序应该为用户请求温度,然后询问它是华氏或摄氏。然后,它可以为华氏和摄氏摄氏度计算,并将告诉用户它是否热,冷,温和等。
我现在想做的就是能够运行它,并将其显示在弹出窗口中,就像这是Visual Basic一样。是否可以?
package temperaturereader;
import javax.swing.JFrame;
import javax.swing.WindowConstants;
import java.util.Scanner;
/**
*
* @author pcstudent
*/
public class TemperatureReader {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
//Declare your Variables
double input = 0.0;
double Fahrenheit= 0.0;
double Celsius= 0.0;
String ForC;
String Hot = "It is hot today.";
String Cold = "It is cold today.";
String Cool = "It is cool today.";
String Warm = "It is warm today.";
String Freezing = "It is freezing today. Take a jacket.";
String TooHot = "It is dangerously hot. Stay hydrated.";
String SunHot = "You are currently standing on the sun.";
String Oymyakon = "You are in Oymyakon, Russia.";
//Asking for Temperature
Scanner one = new Scanner(System.in);
System.out.println("Enter the temperature:");
input = one.nextDouble();
//Asking for Fahrenheit or Celsius
Scanner two = new Scanner(System.in);
System.out.println("Is the temperature in Fahrenheit or Celsius?");
ForC = two.next();
//Check for Fahrenheit and convert accordingly
if (ForC.equalsIgnoreCase("Fahrenheit")) {
Fahrenheit = input;
Celsius = (Fahrenheit -32)*.5556;
}
else if (ForC.equalsIgnoreCase("F")) {
Fahrenheit = input;
Celsius = (Fahrenheit -32)*.5556;
}
//Check for Celsius and convert accordingly
else if (ForC.equalsIgnoreCase("Celsius")) {
Celsius = input;
Fahrenheit = (Celsius*1.8) + 32;
}
else if (ForC.equalsIgnoreCase("C")){
Celsius = input;
Fahrenheit = (Celsius*1.8) + 32;
}
//If there is an error, stop the program.
else {
System.out.println("Error.");
System.exit(0);
}
System.out.println("");
//Compare temperatures and display.
if (Fahrenheit >= 9941) {
System.out.println("The temperature is currently " + Fahrenheit + " degrees Fahrenheit and " + Celsius + " degrees Celsius.");
System.out.println("");
System.out.println(SunHot);
}
else if (Fahrenheit >= 105) {
System.out.println("The temperature is currently " + Fahrenheit + " degrees Fahrenheit and " + Celsius + " degrees Celsius.");
System.out.println("");
System.out.println(TooHot);
}
else if (Fahrenheit >= 80) {
System.out.println("The temperature is currently " + Fahrenheit + " degrees Fahrenheit and " + Celsius + " degrees Celcius.");
System.out.println("");
System.out.println(Hot);
}
else if (Fahrenheit >= 60) {
System.out.println("The temperature is currently " + Fahrenheit + " degrees Fahrenheit and " + Celsius + " degrees Celsius.");
System.out.println("");
System.out.println(Warm);
}
else if (Fahrenheit >= 59) {
System.out.println("The temperature is currently " + Fahrenheit + " degrees Fahrenheit and " + Celsius + " degrees Celsius.");
System.out.println("");
System.out.println(Cool);
}
else if (Fahrenheit >= 40) {
System.out.println("The temperature is currently " + Fahrenheit + " degrees Fahrenheit and " + Celsius + " degrees Celsius.");
System.out.println("");
System.out.println(Cold);
}
else if (Fahrenheit >= 32) {
System.out.println("The temperature is currently " + Fahrenheit + " degrees Fahrenheit and " + Celsius + " degrees Celsius.");
System.out.println("");
System.out.println(Freezing);
}
else if (Fahrenheit >= -58) {
System.out.println("The temperature is currently " + Fahrenheit + " degrees Fahrenheit and " + Celsius + " degrees Celsius.");
System.out.println("");
System.out.println(Oymyakon);
}
else {
System.out.println("");
}
}
您可以从这里开始(我尝试优化您的代码(:
import java.awt.FlowLayout;
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.JTextField;
public class TemperatureReader {
private double Fahrenheit= 0.0;
private double Celsius= 0.0;
private String ForC;
private final String Hot = "It is hot today.", Cold = "It is cold today.", Cool = "It is cool today.",Warm = "It is warm today.",
Freezing = "It is freezing today. Take a jacket.",TooHot = "It is dangerously hot. Stay hydrated.",
SunHot = "You are currently standing on the sun.",Oymyakon = "You are in Oymyakon, Russia.";
public static void main(String[] args) {
TemperatureReader tr = new TemperatureReader();
// a very simple JFrame , it can be improved but this is just an example
JFrame jf = new JFrame("Temperature Reader");
jf.setSize(700, 400); // you can change this
jf.getContentPane().setLayout(new FlowLayout());
JTextField t = new JTextField("", 12);
String []types = {"Fahrenheit", "Celsius"};
t.setHorizontalAlignment(JTextField.LEFT);
JComboBox<String> type = new JComboBox<String>(types);
type.setSelectedIndex(0);
jf.getContentPane().add(t);
JTextField result = new JTextField("", 60);
result.setHorizontalAlignment(JTextField.RIGHT);
JButton btn = new JButton("Calculate");
jf.getContentPane().add(t);
jf.getContentPane().add(type);
jf.getContentPane().add(result);
jf.getContentPane().add(btn);
jf.setVisible(true);
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
double input = 0.0;
// you need to validate it first and check if it's not empty and contains only digits ..etc
input = Double.valueOf(t.getText());
tr.setForC(type.getSelectedItem().toString());
switch(tr.getForC()){
case "Fahrenheit":
tr.setCelsius((input -32)*.5556);
tr.setFahrenheit(input);
break;
case "Celsius":
tr.setFahrenheit((input*1.8) + 32);
tr.setCelsius(input);
break;
}
double f = tr.getFahrenheit();
double c = tr.getCelsius();
String display = (f>=9941)? tr.result(f, c, tr.getSunHot()):
(f>= 105)? tr.result(f, c, tr.getTooHot()):
(f>= 80)? tr.result(f, c, tr.getHot()):
(f>= 60)? tr.result(f, c, tr.getWarm()):
(f>= 59)? tr.result(f, c, tr.getCool()):
(f>= 40)? tr.result(f, c, tr.getCold()):
(f>= 32)? tr.result(f, c, tr.getFreezing()):
(f>= -58)? tr.result(f, c, tr.getOymyakon()): "Error";
result.setText(display);
}
});
}
public String result(double f, double c, String s){
return "The temperature is currently " + f + " degrees Fahrenheit and " + c + " degrees Celsius.n" + s + "n";
}
public double getFahrenheit() {
return Fahrenheit;
}
public void setFahrenheit(double fahrenheit) {
Fahrenheit = fahrenheit;
}
public double getCelsius() {
return Celsius;
}
public void setCelsius(double celsius) {
Celsius = celsius;
}
public String getForC() {
return ForC;
}
public void setForC(String forC) {
ForC = forC;
}
public String getHot() {
return Hot;
}
public String getCold() {
return Cold;
}
public String getCool() {
return Cool;
}
public String getWarm() {
return Warm;
}
public String getFreezing() {
return Freezing;
}
public String getTooHot() {
return TooHot;
}
public String getSunHot() {
return SunHot;
}
public String getOymyakon() {
return Oymyakon;
}
}
,因此您需要创建一个新窗口以显示结果?
创建新的Jframe类并创建该类的新对象,并通过objName.show(true)