简体   繁体   中英

Applet not showing anything

How come this does not print show anything on the Applet? I've tried editing it many times and nothing seems to show. Does anyone know what is happening? I tried to paint but it did not work as well. Any tips would be greatly appreciated!

import java.awt.*;
import java.applet.Applet;
import javax.swing.*;
import java.awt.event.*;
import java.util.Arrays;
import java.util.StringTokenizer;
import java.awt.Color;

public class StartingPoint extends Applet{
    private final int pictureWidth = 1001;  
    private final int pictureHeight = 50;
    private static FormCanvas picture;
    private JPanel panel;
    private JTextField numLights;
    private JTextField whichLight;
    private JTextField howLong;



    public void lightCreator(){
        panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        picture = new FormCanvas();
        panel.add(picture);
        panel.setBackground(Color.BLUE);
        numLights = new JTextField("Enter Number of lights total", 20);
        whichLight = new JTextField("Enter which lights you want off separated by commas", 30);
        howLong = new JTextField("Interval time", 10);
        panel.add(numLights);
        panel.add(whichLight);
        panel.add(howLong);
        int light = Integer.parseInt(numLights.getText());
        String lights = whichLight.getText();
        int lightsOn[] = new int[light];
        StringTokenizer str = new StringTokenizer(lights, ",");
        int n = 0;
        while(str.hasMoreElements()){
            lightsOn[n] = Integer.parseInt((String)str.nextElement());
            n++;
        }
        add(panel);
    }

    class FormCanvas extends Canvas {
        // this class paints the GUI 

        FormCanvas() {
            setSize(pictureWidth, pictureHeight);
            setBackground(Color.blue);
        }
    }
}

Problems:

  • Why extend Applet instead of JApplet?
  • Why mix Swing and AWT components at all (including Canvas)?
  • Where is the lightCreator() method called?
  • If this is an Applet/JApplet, where is your init() method override?

Have you gone through the applet tutorials before trying to create and display an applet? If not, that's the first place that I'd start.

Change:

public void lightCreator(){

To:

@Override
public void init() {

How come this does not print show anything on the Applet? .. Does anyone know what is happening?

As alluded to in @Hover's 3rd & 4th comments, the lightCreator method is never called in that code. Use instead the applet life-cycle method init() and it will be called a single time when the applet is first loaded.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM