Warm tip: This article is reproduced from serverfault.com, please click

JFrame very small

发布于 2013-10-30 21:54:01

I'm following a tutorial and made a JFrame, but it's very tiny. Iv'e searched for this problem on this site, but nothing has helped. Does anybody know the problem? I did it exactly like the tutorial, and it worked for him! I'll post a picture of it also.

Here is the code:

    package net.trails.std;
import java.applet.Applet;
import java.awt.Dimension;
import java.awt.Image;

import javax.swing.JFrame;

public class Core extends Applet implements Runnable {

    private static final long serialVersionUID = 1L;
    private static JFrame frame;

    public static double dY = 0, dX = 0;
    public static final int res = 1;
    public static int dir = 0;

    public static boolean moving = false;
    public static boolean run = false;

    private Image screen;

    public static Dimension screenSize = new Dimension(700, 560);
    public static Dimension pixel = new Dimension(screenSize.width, screenSize.height);
    public static Dimension size;

    public static String name = "Trails";

    public Core(){



    }

    public static void main(String[] args) {

        Core core = new Core();
        frame = new JFrame();
        frame.add(core);
        size = new Dimension(frame.getWidth(), frame.getHeight());
        frame.setSize(700, 560);
        frame.setTitle(name);
        frame.setResizable(false);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
        core.start();

    }


    public void run() {



    }

}

enter image description here

Questioner
zoomerzoom25
Viewed
11
yamafontes 2013-10-31 05:57:49

You're calling frame.pack() with no sized components in your JFrame. This is going to squish the whole thing down to the tiny window you currently have.

Look at the docs for pack():

http://docs.oracle.com/javase/7/docs/api/java/awt/Window.html#pack()

Basically, you need to call setSize() in your Core class (which is an applet).