I am trying to turn a console application into an applet I have code that builds a GUI for a calculator that compiles and runs as expected:
import java.awt.GridBagLayout; import java.awt.GridBagConstraints; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JTextField; import java.lang.*; public class CalculatorGUI extends JFrame { JButton button; JTextField screen; //OperandListener opnLis = new OperandListener(this); //OperatorListener optLis = new OperatorListener(opnLis); CalculatorGUI() { setSize(150, 150); setDefaultCloseOperation(EXIT_ON_CLOSE); setResizable(false); setVisible(true); setLayout(new GridBagLayout()); GridBagConstraints gbc = new GridBagConstraints(); screen = new JTextField("0", 15); gbc.gridx = 0; gbc.gridy = 0; gbc.gridwidth = 4; //button.addActionListener(opnLis); add(button); button = new JButton("7"); gbc.gridx = 0; gbc.gridy = 0; gbc.gridwidth = 1; //button.addActionListener(opnLis); add(button); button = new JButton("8"); gbc.gridx = 0; gbc.gridy = 1; gbc.gridwidth = 1; //button.addActionListener(opnLis); add(button); button = new JButton("9"); gbc.gridx = 0; gbc.gridy = 2; gbc.gridwidth = 1; //button.addActionListener(opnLis); add(button); button = new JButton("+"); gbc.gridx = 0; gbc.gridy = 3; gbc.gridwidth = 1; //button.addActionListener(optLis); add(button); button = new JButton("4"); gbc.gridx = 1; gbc.gridy = 0; gbc.gridwidth = 1; button.addActionListener(opnLis); add(button); //button = new JButton("5"); gbc.gridx = 1; gbc.gridy = 1; gbc.gridwidth = 1; button.addActionListener(opnLis); add(button); //button = new JButton("6"); gbc.gridx = 1; gbc.gridy = 2; gbc.gridwidth = 1; button.addActionListener(opnLis); add(button); //button = new JButton("-"); gbc.gridx = 1; gbc.gridy = 3; gbc.gridwidth = 1; button.addActionListener(opnLis); add(button); button = new JButton("1"); gbc.gridx = 2; gbc.gridy = 0; gbc.gridwidth = 1; //button.addActionListener(opnLis); add(button); button = new JButton("2"); gbc.gridx = 2; gbc.gridy = 1; gbc.gridwidth = 1; //button.addActionListener(opnLis); add(button); button = new JButton("3"); gbc.gridx = 2; gbc.gridy = 2; gbc.gridwidth = 1; //button.addActionListener(opnLis); add(button); button = new JButton("*"); gbc.gridx = 2; gbc.gridy = 3; gbc.gridwidth = 1; //button.addActionListener(optLis); add(button); button = new JButton("."); gbc.gridx = 3; gbc.gridy = 0; gbc.gridwidth = 1; button.addActionListener(opnLis); add(button); button = new JButton("0"); gbc.gridx = 3; gbc.gridy = 1; gbc.gridwidth = 4; //button.addActionListener(opnLis); add(button); button = new JButton("="); gbc.gridx = 3; gbc.gridy = 2; gbc.gridwidth = 1; //button.addActionListener(opnLis); add(button); button = new JButton("/"); gbc.gridx = 3; gbc.gridy = 3; gbc.gridwidth = 1; //button.addActionListener(optLis); add(button); } }
The code for the applet is:
import java.awt.GridBagLayout; import java.awt.GridBagConstraints; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JApplet; import javax.swing.JTextField; import java.lang.*; public class CalculatorApplet extends JApplet { JButton button; JTextField screen; //OperandListener opnLis = new OperandListener(this); //OperatorListener optLis = new OperatorListener(opnLis); CalculatorApplet() { setSize(150, 150); //setDefaultCloseOperation(EXIT_ON_CLOSE); //setResizable(false); setVisible(true); setLayout(new GridBagLayout()); GridBagConstraints gbc = new GridBagConstraints(); screen = new JTextField("0", 15); gbc.gridx = 0; gbc.gridy = 0; gbc.gridwidth = 4; //button.addActionListener(opnLis); add(button); button = new JButton("7"); gbc.gridx = 0; gbc.gridy = 0; gbc.gridwidth = 1; //button.addActionListener(opnLis); add(button); button = new JButton("8"); gbc.gridx = 0; gbc.gridy = 1; gbc.gridwidth = 1; //button.addActionListener(opnLis); add(button); button = new JButton("9"); gbc.gridx = 0; gbc.gridy = 2; gbc.gridwidth = 1; //button.addActionListener(opnLis); add(button); button = new JButton("+"); gbc.gridx = 0; gbc.gridy = 3; gbc.gridwidth = 1; //button.addActionListener(optLis); add(button); button = new JButton("4"); gbc.gridx = 1; gbc.gridy = 0; gbc.gridwidth = 1; //button.addActionListener(opnLis); add(button); button = new JButton("5"); gbc.gridx = 1; gbc.gridy = 1; gbc.gridwidth = 1; //button.addActionListener(opnLis); add(button); button = new JButton("6"); gbc.gridx = 1; gbc.gridy = 2; gbc.gridwidth = 1; //button.addActionListener(opnLis); add(button); button = new JButton("-"); gbc.gridx = 1; gbc.gridy = 3; gbc.gridwidth = 1; //button.addActionListener(opnLis); add(button); button = new JButton("1"); gbc.gridx = 2; gbc.gridy = 0; gbc.gridwidth = 1; //button.addActionListener(opnLis); add(button); button = new JButton("2"); gbc.gridx = 2; gbc.gridy = 1; gbc.gridwidth = 1; //button.addActionListener(opnLis); add(button); button = new JButton("3"); gbc.gridx = 2; gbc.gridy = 2; gbc.gridwidth = 1; //button.addActionListener(opnLis); add(button); button = new JButton("*"); gbc.gridx = 2; gbc.gridy = 3; gbc.gridwidth = 1; //button.addActionListener(optLis); add(button); button = new JButton("."); gbc.gridx = 3; gbc.gridy = 0; gbc.gridwidth = 1; //button.addActionListener(opnLis); add(button); button = new JButton("0"); gbc.gridx = 3; gbc.gridy = 1; gbc.gridwidth = 4; //button.addActionListener(opnLis); add(button); button = new JButton("="); gbc.gridx = 3; gbc.gridy = 2; gbc.gridwidth = 1; //button.addActionListener(opnLis); add(button); button = new JButton("/"); gbc.gridx = 3; gbc.gridy = 3; gbc.gridwidth = 1; //button.addActionListener(optLis); add(button); } public void init() { new CalculatorApplet(); } }
The appletviewer throws the following error:
java.lang.ClassFormatError: Incompatible magic value 1181314149 in class file Project at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClassCond(Unknown Source) at java.lang.ClassLoader.defineClass(Unknown Source) at java.security.SecureClassLoader.defineClass(Unknown Source) at sun.plugin2.applet.Applet2ClassLoader.findClass(Unknown Source) at sun.plugin2.applet.Plugin2ClassLoader.loadClass0(Unknown Source) at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Unknown Source) at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.plugin2.applet.Plugin2ClassLoader.loadCode(Unknown Source) at sun.plugin2.applet.Plugin2Manager.createApplet(Unknown Source) at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source) at java.lang.Thread.run(Unknown Source) Exception: java.lang.ClassFormatError: Incompatible magic value 1181314149 in class file Project
Due to personal technology woes, I am using the compilr.com online compiler and embedded appletviewer, so it may be a problem with how the specific compiler builds the class file. I don't have the option of using the JDK compiler, so I can't figure out I have a compiler-specific problem.