Reputation: 11
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");
}}
Upvotes: 0
Views: 924
Reputation: 201497
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);
}
}
Upvotes: 1