import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Exam2prog extends JApplet {
int cel, fah, grabInput;
double math;
JLabel fLabel=new JLabel("Fahrenheight: ");
JLabel cLabel=new JLabel("Celcius: ");
JTextField fField=new JTextField(4);
JTextField cField=new JTextField(4);
private Container container;
private FlowLayout layout;
public void init() {
try{
Panel p=new Panel();
Panel p2=new Panel();
Panel p3=new Panel();
Panel p4=new Panel();
p.add(fLabel);
p2.add(fField);
p3.add(cLabel);
p4.add(cField);
TextFieldHandler handler=new TextFieldHandler();
fField.addActionListener(handler);
cField.addActionListener(handler);
layout=new FlowLayout();
container=getContentPane();
container.setLayout(layout);
container.add(p);
container.add(p2);
container.add(p3);
container.add(p4);
}
catch(Exception exception) {
JOptionPane.showMessageDialog(this,""+exception.getMessage());
}
}
public class TextFieldHandler implements ActionListener {
public void actionPerformed(ActionEvent event) {
if(event.getSource()==fField) {
grabInput=Integer.parseInt(fField.getText());
math=((9.0/5.0)*grabInput+32);
cField.setText(""+math);
showStatus("Fahrenheight to Celcius");
}
else if(event.getSource()==cField) {
grabInput=Integer.parseInt(cField.getText());
math=((5.0/9.0)*(grabInput-32));
fField.setText(""+math);
showStatus("Celcius to Fahrenheight");
}
exceptHandling();
}
}
public void exceptHandling() throws Exception {
if(grabInput<=0&&grabInput>=0) {
int errors=0;
}
else {
throw new Exception("You must enter a number.");
}
}
public void paint(Graphics g) {
}
}
Posted by duffman12345
at 8:18 PM EST