-1

I have the following class which is a simple gui, and I would like to make it an applet so it can be displayed in the browser. I know how to embed the code into an html page(got that done)... but how can make my class an applet? Also, I assuming I don't need a web server just to display the applet in my browser...

package tester1;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class PanelTest implements ActionListener {

    JFrame frame;
    JLabel inputLabel;
    JLabel outputLabel;
    JLabel outputHidden;
    JTextField inputText;
    JButton button;
    JButton clear;
    JButton about;

    public PanelTest() {
       frame = new JFrame("User Name");
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       frame.setLayout(new GridLayout(3, 2, 10, 10));

       //creating first row
       JPanel row1 = new JPanel();
       inputLabel = new JLabel("Your Name");
       inputText = new JTextField(15);
//       FlowLayout flow1 = new FlowLayout(FlowLayout.CENTER, 10, 10);
//       row1.setLayout(flow1);
       row1.add(inputLabel);
       row1.add(inputText);
       frame.add(row1);
       //creating second row
       JPanel row2 = new JPanel();
       button = new JButton("Display");
       clear = new JButton("Clear");
       about = new JButton("About");
       button.addActionListener(this);
       clear.addActionListener(this);
       about.addActionListener(new displayAbout());
       row2.add(button);
       row2.add(clear);
       row2.add(about);
       frame.add(row2);
       //creating third row
       JPanel row3 = new JPanel();
       outputLabel = new JLabel("Output:", JLabel.LEFT);
       outputHidden = new JLabel("", JLabel.RIGHT);
//       FlowLayout flow2 = new FlowLayout(FlowLayout.CENTER, 10, 10);
//       row3.setLayout(flow2);
       row3.add(outputLabel);
       row3.add(outputHidden);
       frame.add(row3); 

       frame.pack();
       frame.setVisible(true);

    }  

    //same method listen for two different events
    @Override
    public void actionPerformed(ActionEvent e) {
        String command = e.getActionCommand();
        if(command.equals("Display")) {
            outputHidden.setText(inputText.getText());
        }
        if(command.equals("Clear")) {
            outputHidden.setText("");
            inputText.setText("");
        }        
    }

    //another way to listen for events
    class displayAbout implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            JOptionPane.showMessageDialog(frame, "Username 1.1 \n by Jorge L. Vazquez");
        }
    }    

    public static void main(String[] args) {
        PanelTest frameTest = new PanelTest();
    }

}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
miatech
  • 2,150
  • 8
  • 41
  • 78
  • You need to read the [applet tutorial](http://docs.oracle.com/javase/tutorial/deployment/applet/index.html), pure and simple. Sorry, but your post isn't an answerable question other than that it's time to do your studying. Voting to close. – Hovercraft Full Of Eels Aug 15 '12 at 21:33
  • 2
    *"I would like to make it an applet"* Inadvisable. Instead, launch the app. from a link using [Java Web Start](http://stackoverflow.com/tags/java-web-start/info). – Andrew Thompson Aug 15 '12 at 22:15
  • 1
    See also this [answer](http://stackoverflow.com/a/11479113/230513). – trashgod Aug 16 '12 at 01:06

2 Answers2

0

Use a JApplet rather than a JFrame. Make sure you read the relevant Java Tutorial, which covers the applet lifecycle methods like init, start, stop, and destroy.


As a side note, you should not be building your UI outside of the event dispatch thread.

obataku
  • 29,212
  • 3
  • 44
  • 57
0

Use a JApplet instead of a JFrame like veer said, but you must also remove frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);, frame.pack();, and frame.setVisible(true);

Also, replace main(String[] args) with init().

tckmn
  • 57,719
  • 27
  • 114
  • 156