To use JOptionPane, only the following code needs to be changed.You can copy and paste this code in the previous project, or download the whole project from the Link below. CelsiusToFahrenheitApp.java package celsiustofahrenheitapp; import java.util.Scanner; import javax.swing.JOptionPane; /** * * @author Dipro */ public class CelsiusToFahrenheitApp { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here //Create a varible to store the userinput temporarily double tempCelsius; //Creating Object from our class Converter converterObj = new Converter(); //We also need an Scanner object to get the user input from the window String input; //Since JOptionPane returns a string value we need a string variable //Greetings goes here JOptionPane.showMessageDialog(null,"***************Welcome to my Converter Application!***********"); //Now ask the user to enter the value in celsius temperature JOptionPane.showMessageDialog(null,"Please Enter Your Celsius Temperature"); //Here input.nextDouble will read from userInput as a double value and store it into the temCelsius variable input = JOptionPane.showInputDialog("Please Enter a Celsius Value"); tempCelsius = Double.parseDouble(input); //Converting a String into Double Value //Now call the setter method to pass this value to the data member (celsius variable)of our Converter class converterObj.setCelsius(tempCelsius); //Calling the converter method in our Converter class to convert this value to celsius converterObj.converter(); //Calling this method to get the value stored in Farenheit variable JOptionPane.showMessageDialog(null,"Temperature in Farenheit is \n" + converterObj.getFahrenheit()); } } You will notice that, if you press "ok" or "cancel" button without any input then you will get some errors in the console, and to handle errors, we need to know exception handling. I will discuss it in the next project which is a program that will take exam scores as an input and show the letter grade according to the score range. Download Link :
1 Comment
|