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");

}

}

}

}

[19615 byte] By [tristanlee85a] at [2007-11-27 0:45:25]
# 1

add this (use meaningful class name)

public class TestApplet extends JApplet

{

public void init()

{

getContentPane().add(new Converter());

}

}

and remove all of main() and createAndShowGui()

Michael_Dunna at 2007-7-11 23:10:35 > top of Java-index,Java Essentials,Java Programming...
# 2

Alright, I did so and this is my final output:

/**

*

* @author tristan

*/

import java.awt.*;

import java.awt.event.*;

import java.text.DecimalFormat;

import java.util.*;

import javax.swing.*;

public class Converter extends JPanel implements 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();

private float 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);

}

public class Main extends JApplet

{

public void init()

{

getContentPane().add(new Converter());

}

}

public static float CtoF(float degCelsius) {

float degFahrenheit;

degFahrenheit = degCelsius * 9/5 + 32;

return degFahrenheit;

}

public static double CtoK(float degCelsius) {

double degKelvin;

degKelvin = degCelsius + 273.15;

return degKelvin;

}

// Method to convert from degrees Fahrenheit to degrees Celsius

public static float FtoC(float degFahrenheit) {

float degCelsius;

degCelsius = (degFahrenheit - 32) * 5/9;

return degCelsius;

}

public static double FtoK(float degFahrenheit) {

double degKelvin;

degKelvin = (degFahrenheit + 459.67) / 1.8;

return degKelvin;

}

public static double KtoC(float degKelvin) {

double degCelsius;

degCelsius = degKelvin - 273.15;

return degCelsius;

}

public static double KtoF(float degKelvin) {

double degFahrenheit;

degFahrenheit = (degKelvin * 1.8) / 459.67;

return degFahrenheit;

}

public void 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");

}

} else if (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");

}

} else if (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 + "癋");

}

} else if (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 + "癈");

}

} else if (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 + "癋");

}

} else if (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");

}

}

}

}

I built the class and copied Converter.class to the same directory as my applet.htm file. applet.htm contains <html><body><applet code="Converter.class"></applet></body></html>

When I go to view the page, an X is showing up. I right-click and open the Java console to see this error:

java.lang.UnsupportedClassVersionError: Bad version number in .class file

at java.lang.ClassLoader.defineClass1(Native Method)

at java.lang.ClassLoader.defineClass(Unknown Source)

at java.security.SecureClassLoader.defineClass(Unknown Source)

at sun.applet.AppletClassLoader.findClass(Unknown Source)

at java.lang.ClassLoader.loadClass(Unknown Source)

at sun.applet.AppletClassLoader.loadClass(Unknown Source)

at java.lang.ClassLoader.loadClass(Unknown Source)

at sun.applet.AppletClassLoader.loadCode(Unknown Source)

at sun.applet.AppletPanel.createApplet(Unknown Source)

at sun.plugin.AppletViewer.createApplet(Unknown Source)

at sun.applet.AppletPanel.runLoader(Unknown Source)

at sun.applet.AppletPanel.run(Unknown Source)

at java.lang.Thread.run(Unknown Source)

From what I understand, that's because I'm compiling it with a newer version of JDK than the JDK version that is running the applet in my browser?

tristanlee85a at 2007-7-11 23:10:35 > top of Java-index,Java Essentials,Java Programming...
# 3
start with this changeapplet code="Converter.class"should beapplet code="Main.class"
Michael_Dunna at 2007-7-11 23:10:35 > top of Java-index,Java Essentials,Java Programming...
# 4

I don't have a file named Main.class though. I did try it just because.

load: class Main.class not found.

java.lang.ClassNotFoundException: Main.class

at sun.applet.AppletClassLoader.findClass(Unknown Source)

at java.lang.ClassLoader.loadClass(Unknown Source)

at sun.applet.AppletClassLoader.loadClass(Unknown Source)

at java.lang.ClassLoader.loadClass(Unknown Source)

at sun.applet.AppletClassLoader.loadCode(Unknown Source)

at sun.applet.AppletPanel.createApplet(Unknown Source)

at sun.plugin.AppletViewer.createApplet(Unknown Source)

at sun.applet.AppletPanel.runLoader(Unknown Source)

at sun.applet.AppletPanel.run(Unknown Source)

at java.lang.Thread.run(Unknown Source)

Caused by: java.io.FileNotFoundException: /home/tristan/Desktop/applet/Main/class.class (No such file or directory)

at java.io.FileInputStream.open(Native Method)

at java.io.FileInputStream.<init>(Unknown Source)

at java.io.FileInputStream.<init>(Unknown Source)

at sun.net.www.protocol.file.FileURLConnection.connect(Unknown Source)

at sun.net.www.protocol.file.FileURLConnection.getInputStream(Unknown Source)

at sun.applet.AppletClassLoader.getBytes(Unknown Source)

at sun.applet.AppletClassLoader.access$100(Unknown Source)

at sun.applet.AppletClassLoader$1.run(Unknown Source)

at java.security.AccessController.doPrivileged(Native Method)

... 10 more

tristanlee85a at 2007-7-11 23:10:35 > top of Java-index,Java Essentials,Java Programming...
# 5
looks like Main.class is an inner classit needs to be separated, and compiled from Main.java
Michael_Dunna at 2007-7-11 23:10:35 > top of Java-index,Java Essentials,Java Programming...