Temperature Converter (Fahrenheit and Celsius) In Java Swing

[postlink]https://iamdaowner.blogspot.com/2012/06/temperature-converter-fahrenheit-and.html[/postlink]
Fahrenheit
The freezing point of water is 32 degrees Fahrenheit (°F) and the boiling point 212 °F (at standard atmospheric pressure), placing the boiling and freezing points of water exactly 180 degrees apart.

Celsius
It is an international metric temperature scale on which water freezes at 0° and boils at 100° under normal atmospheric conditions.

Save this file as TempConverter.java.

01import java.awt.*;
02import java.awt.event.*;
03import javax.swing.*;
04
05public class TempConverter extends JFrame {
06  JMenuBar menu      = new JMenuBar();
07  JTextField input   = new JTextField(5);
08  JTextField output  = new JTextField(10);
09  JLabel label       = new JLabel("Enter some value");
10  ButtonGroup bgroup = new ButtonGroup();
11  JRadioButton jbuttonCelsius    = new JRadioButton("Celsius");
12  JRadioButton jbuttonFahrenheit = new JRadioButton("Fhrenheit");
13  JButton button     = new JButton("Convert");
14
15
16  JPanel p1 = new JPanel();
17  JPanel p2 = new JPanel();
18  JPanel p3 = new JPanel();
19  boolean setters = true;
20
21  public TempConverter() {
22    input.setText("0");
23    output.setEditable(false);
24    output.setForeground(Color.red);
25    output.setBackground(Color.white);
26    button.setBackground(Color.cyan);
27    jbuttonCelsius.setSelected(true);
28    setTitle("Converter 1.0");
29
30    p1.add(label,BorderLayout.WEST);
31    p1.add(input,BorderLayout.EAST);
32
33    p2.add(jbuttonCelsius,BorderLayout.WEST);
34    jbuttonCelsius.addActionListener(new ActionListener() {
35      public void actionPerformed(ActionEvent e) {Celsius();}
36    });
37
38    p2.add(jbuttonFahrenheit,BorderLayout.EAST);
39    jbuttonFahrenheit.addActionListener(new ActionListener() {
40      public void actionPerformed(ActionEvent e) {Fahrenheit();}
41    });
42
43    p3.add(button);
44    button.addActionListener(new ActionListener() {
45      public void actionPerformed(ActionEvent e) {Compare();}
46    });
47
48    p3.add(output);
49
50    add(p1,BorderLayout.NORTH);
51    add(p2,BorderLayout.CENTER);
52    add(p3,BorderLayout.SOUTH);
53
54    bgroup.add(jbuttonCelsius);
55    bgroup.add(jbuttonFahrenheit);
56
57    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
58
59    setSize(200,120);
60    setResizable(false);
61
62  }
63
64  void Celsius() {
65    setters = true;
66  }
67
68  void Fahrenheit() {
69    setters = false;
70  }
71
72  void Compare(){
73    if(setters == false) {
74      int f = (int)(9*(Double.parseDouble(input.getText()))/5+32);
75      output.setText(" " + f + "  F");
76    }else {
77      int s = (int)(((Double.parseDouble(input.getText()))-32) * 5/9);
78      output.setText(" " + s + "  C");
79    }
80  }
81  public static void main(String args []) {
82    try {
83      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
84    catch (Exception e) { }
85
86    final TempConverter s = new TempConverter();
87    s.show();
88  }
89}

0 comments:

Post a Comment

Grab the widget  IWeb Gator
Powered by Blogger.

Click the Like Button Below To Receive all updates via Facebook

Powered By Blogger Tricks |

Popular Posts