我无法理解为什么我的应用程序无法运行。刚刚开始使用编程。
我得到的主要错误是FahrenHandler,但我不知道在哪个地方放置我的事件处理程序和该类的动作侦听器。
以下是我的两个java文件,它们是ConverterT包的一部分:
ConverterFrame.java
package ConverterT;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Ahmed
*/
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JOptionPane;
public class ConverterFrame extends JFrame {
private final JTextField celciusField; // text field for celcius
private final JLabel celciusLabel; //label for celcius
private final JTextField fahrField; //text field for fahrenheit
private final JLabel fahrLabel; //label for fahr
private double celsius, fahrenheit;
//Action Listener definition for textFields
//ConverterFrame constructor adds JTextFields to JFrame
public ConverterFrame() {
super("Temperature");
setLayout(new FlowLayout());
//construct textfield with columns
celciusField = new JTextField(10);
add(celciusField); //add textField1 to JFrame
celciusLabel = new JLabel("Enter Degrees in Celcius");
add(celciusLabel); //add textField2 to JFrame
fahrField = new JTextField(10);
add(fahrField); // add fahr field to JFrame
fahrLabel = new JLabel("Enter Degrees in Fahrenheit");
add(fahrLabel); //add fahr Label to JFrame
//register event handlers
CelsHandler handlerCelcius = new CelsHandler();
celciusField.addActionListener(handlerCelcius);
FahrenHandler handlerFahren = new FahrenHandler();
fahrField.addActionListener(handlerFahren);
}// end ConverterFrame constructor
//private inner class for event handling
private class CelsHandler implements ActionListener {
@Override
//process textfield events
public void actionPerformed(ActionEvent event) {
//user pressed enter in JTextField celciusField
celsius = Double.parseDouble(celciusField.getText());
//calculate the equivalent temp in fahrenheit
fahrenheit = 9.0 / 5.0 * celsius + 32;
//Display the result in fahrenheit field
fahrField.setText(String.format("%.2f", fahrenheit));
}
FahrenHandler handlerFahren = new FahrenHandler();
private class FahrenHandler implements ActionListener {
@Override
public void actionPerformed(ActionEvent event) {
//Get the data from the text field once the user presses enter
celsius = Double.parseDouble(celciusField.getText());
//Calculate the equivalent temp in fahrenheit
fahrenheit = 5.0 / 9.0 * (celsius - 32);
//Display the result in celsiusField
celciusField.setText(String.format("%.2f", celsius));
}
}
}
}
/* if(event.getSource() == celciusField)
int tempFahr = (int) ((Double.parseDouble(celciusField.getText())) * 1.8 + 32);
event.getActionCommand());
//user pressed Enter in JTextField fahrField
else if (event.getSource() == fahrField)
int tempCel = (int) ((5.0/9.0 * (((Double.parseDouble(fahrField.getText())) -32))));
event.getActionCommand());
//display JTextField content
JOptionPane.showMessageDialog(ConverterFrame.this, string);
*/
ConverterTest.Java
package ConverterT;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Ahmed
*/
import javax.swing.JFrame;
public class ConverterTest {
public static void main(String[] args) {
ConverterFrame converterFrame = new ConverterFrame();
converterFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
converterFrame.setSize(350, 100);
converterFrame.setVisible(true);
}
}//end class ConverterTest
答案 0 :(得分:0)
我不确定你收到了什么错误,但是你做的比你需要的更困难。做这样的事情:
import java.util.Scanner;
import java.io.*;
public class HelloWorld{
public static void main(String []args){
Scanner scan = new Scanner(System.in);
System.out.println("Enter the temperature in Fahrenheit to convert to Celsius: ");
double fahTemp = scan.nextDouble();
System.out.println("Enter the temperature in Celsius to convert to Fahrenheit: ");
double celTemp = scan.nextDouble();
double fahrenheit = (celTemp * (1.8)) + 32;
double celsius = (fahTemp - 32) * (0.555);
System.out.println(fahrenheit);
System.out.println(celsius);
}
}
答案 1 :(得分:0)
我在我的本地电脑上尝试了你的代码,我发现你正试图从它的外部类的构造函数创建一个内部类对象。您的代码中的问题是;
<Context docBase="D:\uploads" path="/uploads"/>
您应该从构造函数中删除此部分。
public ConverterFrame() {
super("Temperature");
setLayout(new FlowLayout());
//construct textfield with columns
celciusField = new JTextField(10);
add(celciusField); //add textField1 to JFrame
celciusLabel = new JLabel("Enter Degrees in Celcius");
add(celciusLabel); //add textField2 to JFrame
fahrField = new JTextField(10);
add(fahrField); // add fahr field to JFrame
fahrLabel = new JLabel("Enter Degrees in Fahrenheit");
add(fahrLabel); //add fahr Label to JFrame
//register event handlers
CelsHandler handlerCelcius = new CelsHandler();
celciusField.addActionListener(handlerCelcius);
ConverterFrame cf = new ConverterFrame();
FahrenHandler handlerFahren = new FahrenHandler();
fahrField.addActionListener(handlerFahren);
}