F-to-C F: Fahrenheit
C-to-F C: Celsius
K-to-C K: Kelvin
C-to-K
K-to-F
F-to-K
Event handling should be set up so that clicking on any one of the six buttons generates an event which your program handles. You must use an inner class to set up your event handling. You can display the result in an output text field or in a JLabel. Your program should accurately convert from Fahrenheit, Celsius, Kelvin to Fahrenheit, Celsius, Kelvin. NOTE: Only the selected conversion is displayed in the output area!!! When the conversion selection changes, the output area should change to show only the new result. The output should display three digits after the decimal point.
The code I have in place works as designed, but I'm curious to know how I could use the string.format to set the output field to 3 digits after the decimal point instead of using "import java.text.DecimalFormat;"??? I'm specifically referring to lines 6, 25, & 125. Below is the program:
package temp1;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.DecimalFormat;
@SuppressWarnings("serial")
public class Temp1 extends JFrame
implements ActionListener {
private JButton button1, button2, button3, button4, button5, button6;
@SuppressWarnings("unused")
private JPanel panel;
@SuppressWarnings("unused")
private JFrame frame;
@SuppressWarnings("unused")
private JTextField Text;
@SuppressWarnings("unused")
private JLabel label;
private JTextField infield = new JTextField(10);
private JTextField outfield = new JTextField(10);
private JLabel inlabel = new JLabel("Enter Input Value Here:");
private JLabel outlabel = new JLabel("Converted Temperature Output:");
DecimalFormat three = new DecimalFormat("0.000");//sets up the 3 Decimal places for use on the output
public static void main(String[] args) {
Temp1 frame = new Temp1();
frame.createFrame();
frame.createButton1();
frame.createButton2();
frame.createButton3();
frame.createButton4();
frame.createButton5();
frame.createButton6();
frame.addLabels();
}
public void createFrame() {
Container window = getContentPane();
window.setLayout(new FlowLayout() ); //FlowLayout manager
setSize(600, 400); //setting the size
setVisible(true); //allows for manual size
setTitle("Temperature Converter Application");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new JPanel(new GridLayout());
add(new JLabel("Temperature Conversions:"+ " C: Celsius"+ " F: Farhrenheit"+ " K: Kelvin"));
}
public void createButton1() {
//Fahrenheit to Celsius button
button1 = new JButton("F-to-C");
add(button1);
button1.addActionListener((ActionListener) this);//this performs action when button1 is clicked
}
public void createButton2() {
//Fahrenheit to Kelvin button
button2 = new JButton("C-to-F");
add(button2);
button2.addActionListener((ActionListener) this);//this performs action when button2 is clicked
}
public void createButton3() {
//Celsius to Kelvin button
button3 = new JButton("K-to-C");
add(button3);
button3.addActionListener((ActionListener) this);//this performs action when button3 is clicked
}
public void createButton4() {
//Celsius to Fahrenheit button
button4 = new JButton("C-to-K");
add(button4);
button4.addActionListener((ActionListener) this);//this performs action when button4 is clicked
}
public void createButton5() {
//Kelvin to Celsius button
button5 = new JButton("K-to-F");
add(button5);
button5.addActionListener((ActionListener) this);//this performs action when button5 is clicked
}
public void createButton6() {
//Kelvin to Fahrenheit button
button6 = new JButton("F-to-K");
add(button6);
button6.addActionListener((ActionListener) this);//this performs action when button6 is clicked
}
public void addLabels() {
add(inlabel);
add(infield);
add(outlabel);
add(outfield);
outfield.setEditable(false);//exception handling
}
public void actionPerformed(ActionEvent i) {
int temp;
double newtemp = 0;
String inputString;
inputString = infield.getText();//computes input
temp = Integer.parseInt(inputString);
if(i.getSource() == button1) {
newtemp = (5/9) * (temp - 32);//F to C formula
}
else if(i.getSource() == button2) {
newtemp = ((9/5) * temp) + 32;//C to F formula
}
else if(i.getSource() == button3) {
newtemp = temp - 273;//K to C formula
}
else if(i.getSource() == button4) {
newtemp = temp + 273;//C to K formula
}
else if(i.getSource() == button5) {
newtemp = ((temp - 273) * (9/5)) + 32;//K to F formula
}
else {
newtemp = (5/9) * (temp - 32) + 273;//F to K formula
}
outfield.setText(" "+ three.format(newtemp));
}
}
Any help would be greatly appreciated.
volvera215, on 07 August 2011 - 01:05 PM, said:
Assignment:You will write a temperature conversion program. The GUI and event handling setup should be done in the constructor of the class that implements the GUI. Your GUI class should contain a JFrame member variable. Do not use any of the GUI editing capabilities of Eclipse for this assignment. The temperature conversion application should have a JLabel and JTextField in which the user inputs a value. There should be a set of six JButtons on the display representing the following temperature conversions:
F-to-C F: Fahrenheit
C-to-F C: Celsius
K-to-C K: Kelvin
C-to-K
K-to-F
F-to-K
Event handling should be set up so that clicking on any one of the six buttons generates an event which your program handles. You must use an inner class to set up your event handling. You can display the result in an output text field or in a JLabel. Your program should accurately convert from Fahrenheit, Celsius, Kelvin to Fahrenheit, Celsius, Kelvin. NOTE: Only the selected conversion is displayed in the output area!!! When the conversion selection changes, the output area should change to show only the new result. The output should display three digits after the decimal point.
The code I have in place works as designed, but I'm curious to know how I could use the string.format to set the output field to 3 digits after the decimal point instead of using "import java.text.DecimalFormat;"??? I'm specifically referring to lines 6, 25, & 125. Below is the program:
Any help would be greatly appreciated.
F-to-C F: Fahrenheit
C-to-F C: Celsius
K-to-C K: Kelvin
C-to-K
K-to-F
F-to-K
Event handling should be set up so that clicking on any one of the six buttons generates an event which your program handles. You must use an inner class to set up your event handling. You can display the result in an output text field or in a JLabel. Your program should accurately convert from Fahrenheit, Celsius, Kelvin to Fahrenheit, Celsius, Kelvin. NOTE: Only the selected conversion is displayed in the output area!!! When the conversion selection changes, the output area should change to show only the new result. The output should display three digits after the decimal point.
The code I have in place works as designed, but I'm curious to know how I could use the string.format to set the output field to 3 digits after the decimal point instead of using "import java.text.DecimalFormat;"??? I'm specifically referring to lines 6, 25, & 125. Below is the program:
package temp1;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.DecimalFormat;
@SuppressWarnings("serial")
public class Temp1 extends JFrame
implements ActionListener {
private JButton button1, button2, button3, button4, button5, button6;
@SuppressWarnings("unused")
private JPanel panel;
@SuppressWarnings("unused")
private JFrame frame;
@SuppressWarnings("unused")
private JTextField Text;
@SuppressWarnings("unused")
private JLabel label;
private JTextField infield = new JTextField(10);
private JTextField outfield = new JTextField(10);
private JLabel inlabel = new JLabel("Enter Input Value Here:");
private JLabel outlabel = new JLabel("Converted Temperature Output:");
DecimalFormat three = new DecimalFormat("0.000");//sets up the 3 Decimal places for use on the output
public static void main(String[] args) {
Temp1 frame = new Temp1();
frame.createFrame();
frame.createButton1();
frame.createButton2();
frame.createButton3();
frame.createButton4();
frame.createButton5();
frame.createButton6();
frame.addLabels();
}
public void createFrame() {
Container window = getContentPane();
window.setLayout(new FlowLayout() ); //FlowLayout manager
setSize(600, 400); //setting the size
setVisible(true); //allows for manual size
setTitle("Temperature Converter Application");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new JPanel(new GridLayout());
add(new JLabel("Temperature Conversions:"+ " C: Celsius"+ " F: Farhrenheit"+ " K: Kelvin"));
}
public void createButton1() {
//Fahrenheit to Celsius button
button1 = new JButton("F-to-C");
add(button1);
button1.addActionListener((ActionListener) this);//this performs action when button1 is clicked
}
public void createButton2() {
//Fahrenheit to Kelvin button
button2 = new JButton("C-to-F");
add(button2);
button2.addActionListener((ActionListener) this);//this performs action when button2 is clicked
}
public void createButton3() {
//Celsius to Kelvin button
button3 = new JButton("K-to-C");
add(button3);
button3.addActionListener((ActionListener) this);//this performs action when button3 is clicked
}
public void createButton4() {
//Celsius to Fahrenheit button
button4 = new JButton("C-to-K");
add(button4);
button4.addActionListener((ActionListener) this);//this performs action when button4 is clicked
}
public void createButton5() {
//Kelvin to Celsius button
button5 = new JButton("K-to-F");
add(button5);
button5.addActionListener((ActionListener) this);//this performs action when button5 is clicked
}
public void createButton6() {
//Kelvin to Fahrenheit button
button6 = new JButton("F-to-K");
add(button6);
button6.addActionListener((ActionListener) this);//this performs action when button6 is clicked
}
public void addLabels() {
add(inlabel);
add(infield);
add(outlabel);
add(outfield);
outfield.setEditable(false);//exception handling
}
public void actionPerformed(ActionEvent i) {
int temp;
double newtemp = 0;
String inputString;
inputString = infield.getText();//computes input
temp = Integer.parseInt(inputString);
if(i.getSource() == button1) {
newtemp = (5/9) * (temp - 32);//F to C formula
}
else if(i.getSource() == button2) {
newtemp = ((9/5) * temp) + 32;//C to F formula
}
else if(i.getSource() == button3) {
newtemp = temp - 273;//K to C formula
}
else if(i.getSource() == button4) {
newtemp = temp + 273;//C to K formula
}
else if(i.getSource() == button5) {
newtemp = ((temp - 273) * (9/5)) + 32;//K to F formula
}
else {
newtemp = (5/9) * (temp - 32) + 273;//F to K formula
}
outfield.setText(" "+ three.format(newtemp));
}
}
Any help would be greatly appreciated.
REVISED program: I sent the wrong one the first time. Same line of question still apply.
package temp1;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.DecimalFormat;
@SuppressWarnings("serial")
public class Temp1 extends JFrame
implements ActionListener {
private JButton button1, button2, button3, button4, button5, button6;
@SuppressWarnings("unused")
private JPanel panel;
@SuppressWarnings("unused")
private JFrame frame;
@SuppressWarnings("unused")
private JTextField Text;
@SuppressWarnings("unused")
private JLabel label;
private JTextField infield = new JTextField(10);
private JTextField outfield = new JTextField(10);
private JLabel inlabel = new JLabel("Enter Input Value Here:");
private JLabel outlabel = new JLabel("Converted Temperature Output:");
DecimalFormat three = new DecimalFormat("0.000");//sets up the 3 Decimal places for use on the output
public static void main(String[] args) {
Temp1 frame = new Temp1();
frame.createFrame();
frame.createButton1();
frame.createButton2();
frame.createButton3();
frame.createButton4();
frame.createButton5();
frame.createButton6();
frame.addLabels();
}
public void createFrame() {
Container window = getContentPane();
window.setLayout(new FlowLayout() ); //FlowLayout manager
setSize(600, 400); //setting the size
setVisible(true); //allows for manual size
setTitle("Temperature Converter Application");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new JPanel(new GridLayout());
add(new JLabel("Temperature Conversions:"+ " C: Celsius"+ " F: Farhrenheit"+ " K: Kelvin"));
}
public void createButton1() {
//Fahrenheit to Celsius button
button1 = new JButton("F-to-C");
add(button1);
button1.addActionListener((ActionListener) this);//this performs action when button1 is clicked
}
public void createButton2() {
//Fahrenheit to Kelvin button
button2 = new JButton("C-to-F");
add(button2);
button2.addActionListener((ActionListener) this);//this performs action when button2 is clicked
}
public void createButton3() {
//Celsius to Kelvin button
button3 = new JButton("K-to-C");
add(button3);
button3.addActionListener((ActionListener) this);//this performs action when button3 is clicked
}
public void createButton4() {
//Celsius to Fahrenheit button
button4 = new JButton("C-to-K");
add(button4);
button4.addActionListener((ActionListener) this);//this performs action when button4 is clicked
}
public void createButton5() {
//Kelvin to Celsius button
button5 = new JButton("K-to-F");
add(button5);
button5.addActionListener((ActionListener) this);//this performs action when button5 is clicked
}
public void createButton6() {
//Kelvin to Fahrenheit button
button6 = new JButton("F-to-K");
add(button6);
button6.addActionListener((ActionListener) this);//this performs action when button6 is clicked
}
public void addLabels() {
add(inlabel);
add(infield);
add(outlabel);
add(outfield);
outfield.setEditable(false);//exception handling
}
public void actionPerformed(ActionEvent i) {
int temp;
double newtemp = 0;
String inputString;
inputString = infield.getText();//computes input
temp = Integer.parseInt(inputString);
if(i.getSource() == button1) {
newtemp = (5.0/9.0) * (temp - 32);//F to C formula
}
else if(i.getSource() == button2) {
newtemp = ((9.0/5.0) * temp) + 32;//C to F formula
}
else if(i.getSource() == button3) {
newtemp = temp - 273;//K to C formula
}
else if(i.getSource() == button4) {
newtemp = temp + 273;//C to K formula
}
else if(i.getSource() == button5) {
newtemp = ((temp - 273) * (9.0/5.0)) + 32;//K to F formula
}
else {
newtemp = (5.0/9.0) * (temp - 32) + 273;//F to K formula
}
outfield.setText(" "+ three.format(newtemp));
}
}

New Topic/Question
Reply




MultiQuote






|