Java GUI温度计算器,带有两个Jlabel5中Calc的JButton(Cel/Fah)输出?
我正在尝试构建一个GUI温度计算器。我几乎可以肯定,在我阅读旧代码和java书籍之前,我已经接近完成并试图找到修复方法。另外,也许这会对将来的人有所帮助。我相信我的错误发生在if语句部分,我只能认为我可能把东西放错地方了,比如文本字段和标签Java GUI温度计算器,带有两个Jlabel5中Calc的JButton(Cel/Fah)输出?,java,user-interface,jbutton,Java,User Interface,Jbutton,我正在尝试构建一个GUI温度计算器。我几乎可以肯定,在我阅读旧代码和java书籍之前,我已经接近完成并试图找到修复方法。另外,也许这会对将来的人有所帮助。我相信我的错误发生在if语句部分,我只能认为我可能把东西放错地方了,比如文本字段和标签 import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.lang.Math; public class GUI imp
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.Math;
public class GUI implements ActionListener{
JFrame frame;
JPanel contentPane;
JLabel label1, label2, label3, label4, label5;
JTextField word1;
JButton Celcius, Fahrenheit;
public GUI(){
frame = new JFrame("Temperature Convertor");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Box layout
//contentPane.setLayout(new BoxLayout(contentPane,BoxLayout.PAGE_AXIS));
//contentPane.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
//contentPane.setBackground(Color.black);
//Grid layout
contentPane = new JPanel();
contentPane.setLayout(new GridLayout(0, 2, 10, 5));
contentPane.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
contentPane.setBackground(Color.black);
label1 = new JLabel("Temperature Convertor");
label1.setAlignmentX(JLabel.CENTER_ALIGNMENT);
label1.setBorder(BorderFactory.createEmptyBorder(30,30,30,30));
contentPane.add(label1);
label2 = new JLabel(new ImageIcon("image1.jpg"));
label2.setAlignmentX(JLabel.CENTER_ALIGNMENT);
label2.setBorder(BorderFactory.createEmptyBorder(30,30,30,30));
contentPane.add(label2);
label3 = new JLabel("Enter Temperature: ");
label3.setAlignmentX(JLabel.CENTER_ALIGNMENT);
label3.setBorder(BorderFactory.createEmptyBorder(30,30,30,30));
contentPane.add(label3);
word1 = new JTextField(10);
contentPane.add(word1);
Celcius = new JButton("Celcius");
Celcius.setAlignmentX(JButton.CENTER_ALIGNMENT);
Celcius.setActionCommand("Celcius");
Celcius.addActionListener(this);
contentPane.add(Celcius);
Fahrenheit = new JButton("Fahrenheit");
Fahrenheit.setAlignmentX(JButton.CENTER_ALIGNMENT);
Fahrenheit.setActionCommand("Fahrenheit");
Fahrenheit.addActionListener(this);
contentPane.add(Fahrenheit);
label4 = new JLabel("The Coverted Temperature is: ");
label4.setAlignmentX(JLabel.CENTER_ALIGNMENT);
label4.setBorder(BorderFactory.createEmptyBorder(30,30,30,30));
contentPane.add(label4);
label5 = new JLabel("");
label5.setAlignmentX(JLabel.CENTER_ALIGNMENT);
label5.setBorder(BorderFactory.createEmptyBorder(30,30,30,30));
contentPane.add(label5);
frame.setContentPane(contentPane);
frame.pack();
frame.setVisible(true);
}
public void actionPerformed(ActionEvent event){
String eventName = event.getActionCommand();
if (eventName.equals("Celcius")){
String label5;
double word1;
label5 = word1.getText();
label5 = (((Double.parseDouble(word1)*9)%5)+32) + "Degree's Celcuis"
;
label5.setText(Double.toString(word1));
}
else if (eventName.equals("Fahrenheit")){
String label5;
double word1;
label5 = word1.getText();
label5 = ((((Double.parseDouble(word1)-32)*5)%9)) + "Degree's
Fahrenheit" ;
label5.setText(Double.toString(word1));
}
}
public static void main(String[] args){
GUI g = new GUI();
}
}
你的问题就在这里
double word1;
label5 = word1.getText();
double是一个原语,它没有属性或方法,更不用说getText方法了
我想你想要的是更像
String label5;
label5 = word1.getText();
label5 = (((Double.parseDouble(label5) * 9) % 5) + 32) + "Degree's Celcuis";
我不知道你当时是怎么想的
label5.setText(Double.toString(label5));
label5已经是一个字符串,而且字符串也没有setText方法,所以它没有任何意义。也许你想用JLabel label5;相反,这意味着您需要更好地命名变量
public void actionPerformed(ActionEvent event){
String eventName = event.getActionCommand();
if (eventName.equals("Celcius")){
String label5;
double word1;
label5 = word1.getText();
label5 = (((Double.parseDouble(word1)*9)%5)+32) + "Degree's Celcuis" ;
label5.setText(Double.toString(word1));
}
else if (eventName.equals("Fahrenheit")){
String label5;
double word1;
label5 = word1.getText();
label5 = ((((Double.parseDouble(word1)-32)*5)%9)) + "Degree'sFahrenheit" ;
label5.setText(Double.toString(word1));
}
}
你的错误在这里。。。
您声明了2个新的局部变量,lable5为String,word1为double,因此以前的变量word1为jtextfield,lable5为JLable在此不可用。。。双变量没有getText方法,字符串没有setText。
这样做:
double d = Double.valueOf(word1.getText());
String s = (((d*9)%5)+32) + "Degree's Celcuis" ;
label5.setText(s);
我相信我的错误已经发生了——我认为如果你:1——描述一下你实际遇到的问题,会更有用;2-提出一个实际问题,3-让我们知道它目前正在做什么和应该做什么-作为一些建议