user1809295
user1809295

Reputation: 1451

Applet Stand alone issue

I attempted to make an applet program I have Stand alone by adding in:

            public static void main(String[] args) {
    JFrame frame = new JFrame("StartingPoint");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    StartingPoint sp = new StartingPoint();
    frame.getContentPane().add(sp);
    sp.init();
    frame.pack();
    frame.setVisible(true);
    sp.start();
    }

Right after my public class. When running as just the applet this does nothing, but when Running it as an application it runs as a very small, nearly flat box aside from the heading, and when manually resized, the screen is blank other then the backround color. Any idea what may cause this?

I have also noticed, each time I resize the frame, what is on it freezes,as if a screen shot of what should happen, and when the screen is resized to nearly full screen I can see at the tip top of the screen a sliver of what should be moving.

Upvotes: 0

Views: 87

Answers (2)

MadProgrammer
MadProgrammer

Reputation: 347334

I'd just like to say, that dropping an applet into a frame is a really bad idea. You are better off writing the application contents into a separate container (such as JPanel) and adding that to your applet or frame - IMHO.

From the Java Docs...

Window#pack

Causes this Window to be sized to fit the preferred size and layouts of its subcomponents. The resulting width and height of the window are automatically enlarged if either of dimensions is less than the minimum size as specified by the previous call to the setMinimumSize method.

If the window and/or its owner are not displayable yet, both of them are made displayable before calculating the preferred size. The Window is validated after its size is being calculated.

This would suggest that your applet needs to provide a preferredSize if you wish to use pack

Upvotes: 1

Reimeus
Reimeus

Reputation: 159864

You need to set the size of the JFrame:

frame.setSize(500, 400);

It sounds as if you are overriding the paint() method. If so, you will need to call

super.paint(g);

to repaint all child components of the applet container on resize.

Upvotes: 0

Related Questions