Java 插入带有输入的JFrame并显示输出
我是新来的,但我喜欢在StackOverflow上找到的一切。现在我正试图在我为java类的最后一个项目编写的旧程序的基础上进行构建,但遗憾的是,我遇到了一个障碍 我的程序应该为用户请求温度,然后询问温度是华氏度还是摄氏度。然后它计算华氏温度和摄氏温度,并告诉用户它是热的、冷的、温和的等等 我现在想做的是能够运行它,并让它显示在一个弹出窗口中,就像这是VisualBasic一样。可能吗Java 插入带有输入的JFrame并显示输出,java,jframe,output,user-input,display,Java,Jframe,Output,User Input,Display,我是新来的,但我喜欢在StackOverflow上找到的一切。现在我正试图在我为java类的最后一个项目编写的旧程序的基础上进行构建,但遗憾的是,我遇到了一个障碍 我的程序应该为用户请求温度,然后询问温度是华氏度还是摄氏度。然后它计算华氏温度和摄氏温度,并告诉用户它是热的、冷的、温和的等等 我现在想做的是能够运行它,并让它显示在一个弹出窗口中,就像这是VisualBasic一样。可能吗 package temperaturereader; import javax.swing.JFrame; i
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("");
}
}
所以您需要创建一个新窗口,以便将结果显示给用户 创建新的jFrame类并创建该类的新对象,并通过
objName.show(true)
使其可见。您可以从这里开始(我尝试优化您的代码):
导入java.awt.FlowLayout;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入javax.swing.JButton;
导入javax.swing.JComboBox;
导入javax.swing.JFrame;
导入javax.swing.JTextField;
公共类温度读取器{
私人双华氏度=0.0;
私人双摄氏度=0.0;
私有字符串ForC;
私有最终字符串Hot=“今天很热。”,Cold=“今天很冷。”,Cool=“今天很冷。”,Warm=“今天很热。”,
冰冻=“今天很冷。穿件夹克。”,TooHot=“天气热得很危险。保持水分充足。”,
SunHot=“你现在站在太阳上。”,Oymyakon=“你在俄罗斯的Oymyakon。”;
公共静态void main(字符串[]args){
TemperatureReader tr=新的TemperatureReader();
//这是一个非常简单的JFrame,可以改进,但这只是一个示例
JFrame jf=新JFrame(“温度读取器”);
jf.setSize(700400);//您可以更改它
jf.getContentPane().setLayout(新的FlowLayout());
JTextField t=新的JTextField(“,12);
字符串[]类型={“华氏”、“摄氏”};
t、 setHorizontalAlignment(JTextField.LEFT);
JComboBox类型=新的JComboBox(类型);
类型.setSelectedIndex(0);
jf.getContentPane().add(t);
JTextField结果=新的JTextField(“,60”);
结果:setHorizontalAlignment(JTextField.RIGHT);
JButton btn=新JButton(“计算”);
jf.getContentPane().add(t);
jf.getContentPane().add(类型);
jf.getContentPane().add(结果);
jf.getContentPane().add(btn);
jf.setVisible(真);
btn.addActionListener(新ActionListener(){
@凌驾
已执行的公共无效操作(操作事件arg0){
双输入=0.0;
//您需要首先验证它,并检查它是否为空,是否只包含数字..等等
input=Double.valueOf(t.getText());
tr.setForC(type.getSelectedItem().toString());
开关(tr.getForC()){
案例“华氏度”:
tr.setCelsius((输入-32)*.5556);
tr.setFahrenheit(输入);
打破
案例“摄氏度”:
tr.设定华氏温度((输入*1.8)+32);
tr.setCelsius(输入);
打破
}
双f=tr.getFahrenheit();
双c=tr.getCelsius();
字符串显示=(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.get冰冻()):
(f>=-58)?tr.result(f,c,tr.getOymyakon()):“错误”;
result.setText(显示);
}
});
}
公共字符串结果(双f、双c、字符串s){
return“当前温度为”+f+”华氏度和“+c+”摄氏度。\n“+s+”\n”;
}
公共双华氏度(){
返回华氏温度;
}
公共真空设定华氏度(双华氏度){
华氏度=华氏度;
}
公共双精度计数器(){
返回摄氏度;
}
公共空间设置摄氏度(双摄氏度){
摄氏度=摄氏度;
}
公共字符串getForC(){
返回ForC;
}
公共void setForC(字符串forC){
ForC=ForC;
}
公共字符串getHot(){
返回热;
}
公共字符串getCold(){
返寒;
}
公共字符串getCool(){
回凉;
}
公共字符串getWarm(){
回暖;
}
公共字符串getFringing(){
回冻;
}
公共字符串getTooHot(){
返回太热;
}
公共字符串getSunHot(){
回归阳光;
}
公共字符串getOymyakon(){
返回Oymyakon;
}
}
您遇到了什么问题?可能吗?对网上有各种各样的教程。StackOverflow不是为了“嘿,告诉我怎么做”,而是为了展示你所做的尝试,并解释你的困境。请参阅以获得一些指导。首先,SO不是一个教程网站,因此您必须自己做出一些努力。我建议从和开始。你也可以看看OK,但是OP是如何制作一个框架的,OP是如何在框架中添加控件的,OP是如何与控件交互的,以及如何管理事件的——这个问题太宽泛了,你的答案反映了它有多糟糕
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;
}
}