package gui;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;
import javax.swing.JTextField;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.InputMethodListener;
import java.awt.event.InputMethodEvent;
import javax.swing.JLabel;
import java.awt.Font;
public class Convert extends JFrame {
private JPanel contentPane;
private final ButtonGroup buttonGroup = new ButtonGroup();
private final ButtonGroup buttonGroup_1 = new ButtonGroup();
private JTextField txtIn;
private JTextField txtOut;
private int tempIn;
private int tempOut;
private float temp;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Convert frame = new Convert();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Convert() {
setTitle("Temperature conversion");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 341);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JRadioButton rdbtnNewRadioButton = new JRadioButton("F");
rdbtnNewRadioButton.setSelected(true);
rdbtnNewRadioButton.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent arg0) {
tempIn = 1;
temp = convertTemp(tempIn, tempOut);
txtOut.setText(temp+"");
}
});
buttonGroup.add(rdbtnNewRadioButton);
rdbtnNewRadioButton.setBounds(21, 54, 109, 23);
contentPane.add(rdbtnNewRadioButton);
JRadioButton rdbtnC = new JRadioButton("C");
rdbtnC.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
tempIn = 2;
temp = convertTemp(tempIn, tempOut);
txtOut.setText(temp+"");
}
});
buttonGroup.add(rdbtnC);
rdbtnC.setBounds(152, 54, 109, 23);
contentPane.add(rdbtnC);
JRadioButton rdbtnK = new JRadioButton("K");
rdbtnK.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
tempIn = 3;
temp = convertTemp(tempIn, tempOut);
txtOut.setText(temp+"");
}
});
buttonGroup.add(rdbtnK);
rdbtnK.setBounds(281, 54, 109, 23);
contentPane.add(rdbtnK);
JRadioButton rdbtnF = new JRadioButton("F");
rdbtnF.setSelected(true);
rdbtnF.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
tempOut = 1;
temp = convertTemp(tempIn, tempOut);
txtOut.setText(temp+"");
}
});
buttonGroup_1.add(rdbtnF);
rdbtnF.setBounds(21, 208, 109, 23);
contentPane.add(rdbtnF);
JRadioButton rdbtnC_1 = new JRadioButton("C");
rdbtnC_1.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
tempOut = 2;
temp = convertTemp(tempIn, tempOut);
txtOut.setText(temp+"");
}
});
buttonGroup_1.add(rdbtnC_1);
rdbtnC_1.setBounds(152, 208, 109, 23);
contentPane.add(rdbtnC_1);
JRadioButton rdbtnK_1 = new JRadioButton("K");
rdbtnK_1.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
tempOut = 3;
temp = convertTemp(tempIn, tempOut);
txtOut.setText(temp+"");
}
});
buttonGroup_1.add(rdbtnK_1);
rdbtnK_1.setBounds(281, 208, 109, 23);
contentPane.add(rdbtnK_1);
txtIn = new JTextField();
txtIn.addInputMethodListener(new InputMethodListener() {
public void caretPositionChanged(InputMethodEvent arg0) {
}
public void inputMethodTextChanged(InputMethodEvent arg0) {
temp = convertTemp(tempIn, tempOut);
txtOut.setText(temp+"");
}
});
txtIn.setBounds(21, 114, 232, 20);
contentPane.add(txtIn);
txtIn.setColumns(10);
txtOut = new JTextField();
txtOut.setColumns(10);
txtOut.setBounds(21, 271, 232, 20);
contentPane.add(txtOut);
JLabel lblNewLabel = new JLabel("Convert from:");
lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 14));
lblNewLabel.setBounds(21, 22, 116, 20);
contentPane.add(lblNewLabel);
JLabel lblEnterNumeric = new JLabel("Enter numeric Temperature:");
lblEnterNumeric.setFont(new Font("Tahoma", Font.PLAIN, 14));
lblEnterNumeric.setBounds(21, 84, 178, 20);
contentPane.add(lblEnterNumeric);
JLabel lblConvertTo = new JLabel("Convert to:");
lblConvertTo.setFont(new Font("Tahoma", Font.PLAIN, 14));
lblConvertTo.setBounds(21, 167, 116, 20);
contentPane.add(lblConvertTo);
JLabel lblCom = new JLabel("Comparable temperature is:");
lblCom.setFont(new Font("Tahoma", Font.PLAIN, 14));
lblCom.setBounds(21, 238, 197, 20);
contentPane.add(lblCom);
}
public float convertTemp(int tempIn, int tempOut){
float temp;
if(!txtIn.getText().equals("")){
temp = Float.parseFloat(txtIn.getText());
switch(tempIn){
case 1:{
if(tempOut==2){
temp = (float) ((temp-32)/1.8);
}else if(tempOut==3)
temp = (float) ((temp-32)/1.8 + 273);
break;
}
case 2:{
if(tempOut==1){
temp = (float) (temp*1.8+32);
}else if(tempOut==3)
temp += 273;
break;
}
case 3:{
if(tempOut==1)
temp = (float) ((temp-273)*1.8 +32);
else if(tempOut==2)
temp -= 273;
break;
}
}
}else{
temp = 0;
}
return temp;
}
}
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.