Convert Application to JApplet
I made an application and it works perfectly when I compile it in the command window with javac. Here is the code for that:
Code :
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.AbstractButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class RegF extends JFrame {
protected static JTextField textfield1, textfield2, textfield3, textfield4, textfield5;
protected static JButton ok, cancel;
protected static JLabel label1, label2, label3, label4, label5;
public static void main(String args[]) {
JFrame frame = new JFrame("Registration Form");
GridBagLayout lay = new GridBagLayout();
JPanel pane = new JPanel(lay);
GridBagConstraints c = new GridBagConstraints();
textfield1 = new JTextField(20);
textfield2 = new JTextField(20);
textfield3 = new JTextField(30);
textfield4 = new JTextField(20);
textfield5 = new JTextField(20);
ok = new JButton("Ok");
ok.setMnemonic(KeyEvent.VK_O);
cancel = new JButton("Cancel");
cancel.setMnemonic(KeyEvent.VK_C);
ok.setToolTipText("Click if you you are ok with the your entries");
cancel.setToolTipText("Click if would like to cancel");
label1 = new JLabel("First Name");
label2 = new JLabel("Last Name");
label3 = new JLabel("Address");
label4 = new JLabel("City");
label5 = new JLabel("State");
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 1;
c.gridy = 0;
pane.add(textfield1, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 1;
c.gridy = 1;
pane.add(textfield2, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 1;
c.gridy = 2;
pane.add(textfield3, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 1;
c.gridy = 3;
pane.add(textfield4, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 1;
c.gridy = 4;
pane.add(textfield5, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 0;
c.gridy = 0;
pane.add(label1, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 0;
c.gridy = 1;
pane.add(label2, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 0;
c.gridy = 2;
pane.add(label3, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 0;
c.gridy = 3;
pane.add(label4, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 0;
c.gridy = 4;
pane.add(label5, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 0;
c.gridy = 6;
pane.add(ok, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 2;
c.gridy = 6;
pane.add(cancel, c);
frame.setDefaultCloseOperation(JFrame.EX…
frame.getContentPane().add(pane);
frame.pack();
frame.setVisible(true);
}
}
So that one compiles fine and the ok and cancel buttons aren't supposed to do anything. If anyone has any ideas for those that would be cool but I don't even need that so don't really worry about it. The main question here is how do I convert that to work in a browser window because I tried and I searched Google more than I like to say and I can't figure out how to get it to work.
Thank you to anyone that can help I am just learning this stuff and it is driving me nuts!
Re: Convert Application to JApplet
I'd recommend going through Sun Java Lesson: Applets tutorials. They explain in great detail how to write applets.
A short tutorial to get you started:
The init() method basically replaces the constructor (put your initialization code here)
Most applets over-ride the paint method, but I think in your case you shouldn't since you don't need to do any custom painting. Instead, simply get back the content pane of the applet and add your components as necessary to the content pane.
Also, if you want your buttons to work, you need to add an ActionListener. This can either be it's own separate class that implements the ActionListener interface, or your Applet class can implement the ActionListener class.
Code :
// sample ActionPerformed method which prints out the action command.
// unfortunately, it prints out to the println stream, which you can't see directly from the web browser (you'll need to open the console, in Firefox go to [i]Tools/Java console[/i])
actionPerformed(ActionEvent e)
{
System.out.println("The button pressed was: " + e.getActionCommand());
}
Re: Convert Application to JApplet
So then what would be classified as the constructor in the code of the program that I posted then? In other words where would I put the init()?