Converting application to applet
I ended up making a temperature conversion tool for my school assignment, but after making it an application, it ended up needing to be an applet. I was able to find this post that someone else started: http://forum.java.sun.com/thread.jspa?threadID=390846&messageID=1689271 but the format of my application looks to be a little more complex. Do I just need to replace
publicstaticvoid main(String[] args){
javax.swing.SwingUtilities.invokeLater(new Runnable(){
publicvoid run(){
createAndShowGUI();
}
});
}
with
/** Initializes AppletOne */
publicvoid init(){
createAndShowGUI();
}
but I'm not sure what to do with javax.swing.SwingUtilities.invokeLater(new Runnable()
Just for reference, here is my whole code as an application:/**
*
* @author tristan
*/
import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;
import java.util.*;
import javax.swing.*;
publicclass Converterextends JPanelimplements ActionListener{
static String CelInString ="癈";
static String FahInString ="癋";
static String KelInString =" K";
static String CelOutString ="癈";
static String FahOutString ="癋";
static String KelOutString =" K";
private JRadioButton CelInButton =new JRadioButton(CelInString);
private JRadioButton FahInButton =new JRadioButton(FahInString);
private JRadioButton KelInButton =new JRadioButton(KelInString);
private JRadioButton CelOutButton =new JRadioButton(CelOutString);
private JRadioButton FahOutButton =new JRadioButton(FahOutString);
private JRadioButton KelOutButton =new JRadioButton(KelOutString);
private JRadioButton disabledButton =new JRadioButton();
private JLabel Title =new JLabel("Enter in a value:");
private JFormattedTextField value =new JFormattedTextField();
privatefloat temp;
private JLabel output =new JLabel("--Output here");
public Converter(){
CelInButton.setActionCommand(CelInString);
FahInButton.setActionCommand(FahInString);
KelInButton.setActionCommand(KelInString);
CelOutButton.setActionCommand(CelOutString);
FahOutButton.setActionCommand(FahOutString);
KelOutButton.setActionCommand(KelOutString);
value =new JFormattedTextField();
value.setValue(new Float(temp));
value.setColumns(5);
value.setHorizontalAlignment(JTextField.CENTER);
ButtonGroup radioIn =new ButtonGroup();
radioIn.add(CelInButton);
radioIn.add(FahInButton);
radioIn.add(KelInButton);
ButtonGroup radioOut =new ButtonGroup();
radioOut.add(CelOutButton);
radioOut.add(FahOutButton);
radioOut.add(KelOutButton);
//Register the actions
CelInButton.addActionListener(this);
FahInButton.addActionListener(this);
KelInButton.addActionListener(this);
CelOutButton.addActionListener(this);
FahOutButton.addActionListener(this);
KelOutButton.addActionListener(this);
// Put everything in boxes.
Box inputBox = Box.createVerticalBox();
Box valueBox = Box.createVerticalBox();
valueBox.add(Title);
valueBox.add(value);
Box radioBox = Box.createHorizontalBox();// for the 2 radio button boxes
//Put the radioIn buttons in a column in a box.
Box radioInBox = Box.createVerticalBox();
JLabel from =new JLabel("From");
radioInBox.add(from);
radioInBox.add(CelInButton);
radioInBox.add(FahInButton);
radioInBox.add(KelInButton);
//Put the radioOut buttons in a column in a box.
Box radioOutBox = Box.createVerticalBox();
JLabel to =new JLabel("To");
radioOutBox.add(to);
radioOutBox.add(CelOutButton);
radioOutBox.add(FahOutButton);
radioOutBox.add(KelOutButton);
radioBox.add(radioInBox);
radioBox.add(Box.createHorizontalGlue());// absorbs extra space
radioBox.add(radioOutBox);
Box outputBox = Box.createHorizontalBox();
outputBox.add(Box.createHorizontalGlue());
outputBox.add(output);
outputBox.add(Box.createHorizontalGlue());
inputBox.add(valueBox);
inputBox.add(radioBox);
inputBox.add(outputBox);
add(inputBox);
}
privatestaticvoid createAndShowGUI(){
//Create and set up the window.
JFrame frame =new JFrame("Temperature Conversion");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
JComponent newContentPane =new Converter();
newContentPane.setOpaque(true);//content panes must be opaque
frame.setContentPane(newContentPane);
//Display the window.
frame.pack();
frame.setVisible(true);
}
/**
* @param args the command line arguments
*/
publicstaticvoid main(String[] args){
javax.swing.SwingUtilities.invokeLater(new Runnable(){
publicvoid run(){
createAndShowGUI();
}
});
}
publicstaticfloat CtoF(float degCelsius){
float degFahrenheit;
degFahrenheit = degCelsius * 9/5 + 32;
return degFahrenheit;
}
publicstaticdouble CtoK(float degCelsius){
double degKelvin;
degKelvin = degCelsius + 273.15;
return degKelvin;
}
// Method to convert from degrees Fahrenheit to degrees Celsius
publicstaticfloat FtoC(float degFahrenheit){
float degCelsius;
degCelsius = (degFahrenheit - 32) * 5/9;
return degCelsius;
}
publicstaticdouble FtoK(float degFahrenheit){
double degKelvin;
degKelvin = (degFahrenheit + 459.67) / 1.8;
return degKelvin;
}
publicstaticdouble KtoC(float degKelvin){
double degCelsius;
degCelsius = degKelvin - 273.15;
return degCelsius;
}
publicstaticdouble KtoF(float degKelvin){
double degFahrenheit;
degFahrenheit = (degKelvin * 1.8) / 459.67;
return degFahrenheit;
}
publicvoid actionPerformed(ActionEvent e){
JRadioButton newSelection = (JRadioButton)e.getSource();
if (newSelection == CelInButton){
if (CelOutButton.isSelected()){
temp = ((Number)value.getValue()).floatValue();
output.setText(temp +"癈 converts to " + temp +"癈");
}
if (FahOutButton.isSelected()){
temp = ((Number)value.getValue()).floatValue();
DecimalFormat df =new DecimalFormat("0.00");
String convert = df.format(CtoF(temp));
output.setText(temp +"癈 converts to " + convert +"癋");
}
if (KelOutButton.isSelected()){
temp = ((Number)value.getValue()).floatValue();
DecimalFormat df =new DecimalFormat("0.00");
String convert = df.format(CtoK(temp));
output.setText(temp +"癈 converts to " + convert +"K");
}
}elseif (FahInButton.isSelected()){
if (FahOutButton.isSelected()){
temp = ((Number)value.getValue()).floatValue();
output.setText(temp +"癋 converts to " + temp +"癋");
}
if (CelOutButton.isSelected()){
temp = ((Number)value.getValue()).floatValue();
DecimalFormat df =new DecimalFormat("0.00");
String convert = df.format(FtoC(temp));
output.setText(temp +"癋 converts to " + convert +"癈");
}
if (KelOutButton.isSelected()){
temp = ((Number)value.getValue()).floatValue();
DecimalFormat df =new DecimalFormat("0.00");
String convert = df.format(FtoK(temp));
output.setText(temp +"癋 converts to " + convert +"K");
}
}elseif (KelInButton.isSelected()){
if (KelOutButton.isSelected()){
temp = ((Number)value.getValue()).floatValue();
output.setText(temp +"K converts to " + temp +"K");
}
if (CelOutButton.isSelected()){
temp = ((Number)value.getValue()).floatValue();
DecimalFormat df =new DecimalFormat("0.00");
String convert = df.format(KtoC(temp));
output.setText(temp +"K converts to " + convert +"癈");
}
if (FahOutButton.isSelected()){
temp = ((Number)value.getValue()).floatValue();
DecimalFormat df =new DecimalFormat("0.00");
String convert = df.format(CtoF(temp));
output.setText(temp +"K converts to " + convert +"癋");
}
}elseif (CelOutButton.isSelected()){
if (CelInButton.isSelected()){
temp = ((Number)value.getValue()).floatValue();
output.setText(temp +"癈 converts to " + temp +"癈");
}
if (FahInButton.isSelected()){
temp = ((Number)value.getValue()).floatValue();
DecimalFormat df =new DecimalFormat("0.00");
String convert = df.format(FtoC(temp));
output.setText(temp +"癋 converts to " + convert +"癈");
}
if (KelInButton.isSelected()){
temp = ((Number)value.getValue()).floatValue();
DecimalFormat df =new DecimalFormat("0.00");
String convert = df.format(KtoC(temp));
output.setText(temp +"K converts to " + convert +"癈");
}
}elseif (FahOutButton.isSelected()){
if (FahInButton.isSelected()){
temp = ((Number)value.getValue()).floatValue();
output.setText(temp +"癋 converts to " + temp +"癋");
}
if (CelInButton.isSelected()){
temp = ((Number)value.getValue()).floatValue();
DecimalFormat df =new DecimalFormat("0.00");
String convert = df.format(CtoF(temp));
output.setText(temp +"癈 converts to " + convert +"癋");
}
if (KelInButton.isSelected()){
temp = ((Number)value.getValue()).floatValue();
DecimalFormat df =new DecimalFormat("0.00");
String convert = df.format(KtoF(temp));
output.setText(temp +"K converts to " + convert +"癋");
}
}elseif (KelOutButton.isSelected()){
if (KelInButton.isSelected()){
output.setText(temp +"K converts to " + temp +"K");
}
if (CelInButton.isSelected()){
temp = ((Number)value.getValue()).floatValue();
DecimalFormat df =new DecimalFormat("0.00");
String convert = df.format(CtoK(temp));
output.setText(temp +"癈 converts to " + convert +"K");
}
if (FahInButton.isSelected()){
temp = ((Number)value.getValue()).floatValue();
DecimalFormat df =new DecimalFormat("0.00");
String convert = df.format(FtoK(temp));
output.setText(temp +"癋 converts to " + convert +"K");
}
}
}
}

