0

I made a program for my java class and a part of the assignment is that I need to make a HTML document that will display my applet. I haven't learned much about HTML so I have no idea how to make the document. Can someone please help me? Here is my code:

import java.awt.*;
import java.awt.event.*;

public class colors{
Button button1;
Button button2;
Button button3;
TextField textbox;
Label label1;


public static void main (String args[]){
colors c = new colors();
}
public colors() {
Frame f = new Frame ("Colors");
Button button1 = new Button("Blue");
button1.setBounds(0,205,100,75);
Button button2 = new Button("Red");
button2.setBounds(100,205,100,75);
Button button3 = new Button("Yellow");
button3.setBounds(200,205,100,75);

f.add(button1);
f.add(button2);
f.add(button3);

textbox = new TextField("", 0);
textbox.setBounds(80,105,125,25);
textbox.setText("Which Color?");
f.add(textbox);
label1 = new Label("Click on one of the Buttons to Choose a Color");
f.add(label1);

f.addWindowListener(new WindowAdapter()
{
    public void windowClosing(WindowEvent we){
        System.exit(0);
    }
});
f.setSize(300,300);
f.setVisible(true);
button1.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent evt){
        textbox.setText("Blue");
        textbox.setForeground(Color.blue);
    }
});
button2.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent evt){
        textbox.setText("Red");
        textbox.setForeground(Color.red);
    }
});
button3.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent evt){
        textbox.setText("Yellow");
        textbox.setForeground(Color.yellow);
    }
});
}
}
Alyssa
  • 27
  • 1
  • 5
  • [Java Applets](http://docs.oracle.com/javase/tutorial/deployment/applet/) – MadProgrammer Jul 27 '14 at 11:03
  • Use the [deployment toolkit script](http://docs.oracle.com/javase/7/docs/technotes/guides/jweb/deployment_advice.html#deplToolkit) to deploy the applet. Also, please refer your instructor to [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). – Andrew Thompson Jul 27 '14 at 14:38
  • BTW - Swing GUIs might have to work on different platforms, using different PLAFs, on different screen sizes and resolutions with different default settings for font size. As such, they are not conducive to exact placement of components. Instead use layout managers, or [combinations of layout managers](http://stackoverflow.com/a/5630271/418556) as well as [layout padding and borders](http://stackoverflow.com/q/17874717/418556) for white space. – Andrew Thompson Jul 27 '14 at 14:39
  • Also.. that is ***not an applet*** and therefore it cannot be deployed as one. And.. why use AWT components rather than Swing? See [this answer](http://stackoverflow.com/a/6255978/418556) for many good reasons to abandon AWT. – Andrew Thompson Jul 27 '14 at 14:40

3 Answers3

0

You can use HTML's applet tag. Something like this

  <html>
  <head></head>
  <body>
  <applet code="Colors.class" width="350" height="350"></applet>
  </body>
  </html>

However, some changes needs to be done in your code so that it can be shown as an Applet. Currently, your code is usign Frame. It should be changed to Applet. Second, you need to remove/comment f.setSize() and f.setVisible() methods.

Bipul Sinha
  • 266
  • 2
  • 9
0

The simplest one would be something like:

<html>
  <head>
    <title>Colors</title>
  </head>
  <body>
      <applet code=colors.class width=300 height=300>
        alt="Your browser understands the &lt;APPLET&gt; tag but isn't running the applet, for some reason."
        Your browser is completely ignoring the &lt;APPLET&gt; tag!
      </applet>
  </body>
</html>
alex
  • 12
  • 1
0

Compile the applet Applet is a normal Java class so it is compiled as normal as using the javac command:

javac Colors.java

You should get a compiled class file, Colors.class.

Embed into web page

To display the applet in a web page, there are three HTML tags that you can use: , and . The HTML specification states that the applet tag is deprecated, and that you should use the object tag instead. However, the specification is vague about how browsers should implement the object tag to support Java applets, and browser support is currently inconsistent. Oracle therefore recommends that you continue to use the applet tag as a consistent way to deploy Java applets across browsers on all platforms.

<html>
<head>
    <title>My colors Applet</title>
</head>
<body>
<div align="center">
    <applet name="SimpleApplet"
        code="colors.class"
        width="300" height="300"
    />
</div>
</body>
</html>

Running the applet

It’s time to test our applet in a browser. Make sure that you put the MyApplet.html file at the same folder as the compiled applet class file, colors.class. Double click on the MyApplet.html file, it should be opened by your default browser.

read more...

Deepanshu J bedi
  • 1,530
  • 1
  • 11
  • 23