0

I am very new to Java and am working on a simple applet as a homework project... I compile it without any syntax errors, but it doesn't show up in Main as anything but a blank screen and the words "applet started". Could I please have some proofreading/advice on how to fix it? Thanks

import java.applet.*;
import java.awt.*;
import javax.swing.*;

public class Module6Project extends JApplet
{
public static void main (String[] args) {
    JFrame f=new JFrame("Module6Project"); 
    f.setBackground(Color.blue); 
    f.getContentPane().setLayout(null);
    f.setSize(500,500);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 



JTextField first = new JTextField("First name", 150);
JTextField second = new JTextField("Last name", 200);
JTextField third = new JTextField("DoB", 75);
JTextField fourth = new JTextField("Address", 350);
JTextField fifth = new JTextField("Additional comments", 250);
JButton OK = new JButton("OK");
JButton Cancel = new JButton("Cancel");
}}
  • 1) `JTextField third = new JTextField("DoB", 75);` The `75` represents columns/characters, ***not*** pixels. 2) `f.getContentPane().setLayout(null);` 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 25 '14 at 11:50
  • 3) Why code an applet? If it is due to spec by your instructor, please refer them 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 25 '14 at 11:51

1 Answers1

1

You seem confused. Applet(s) run in a web browser, not by main(). Next, you have an Applet (you don't want a JFrame). Finally, you need to add your components to a container. I'd normally use a JPanel, but you can use an JApplet or a JFrame.

class Module6Project extends JApplet {
  @Override
  public void init() {
    // Not a new Frame, "this" applet.
    this.setBackground(Color.blue);
    this.getContentPane().setLayout(null);
    // this.setSize(500, 500);

    JTextField first = new JTextField("First name",
        150);
    JTextField second = new JTextField("Last name",
        200);
    JTextField third = new JTextField("DoB", 75);
    JTextField fourth = new JTextField("Address", 350);
    JTextField fifth = new JTextField(
        "Additional comments", 250);
    JButton OK = new JButton("OK");
    JButton Cancel = new JButton("Cancel");
    // add everything to the container (or it won't be visible)
    this.add(first);
    this.add(second);
    this.add(third);
    this.add(fourth);
    this.add(fifth);
    this.add(OK);
    this.add(Cancel);
  }
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249