JFrame太小而pack()似乎不起作用

时间:2018-09-30 00:40:21

标签: java swing canvas jframe

由于某些原因,JFrame运行时太小,并且使用pack();似乎无法解决问题。有什么想法吗?

   public class Window {

    public Window(String title, Game game) {

        // Creates new JFrame
        JFrame frame = new JFrame(title);

        // Adds Game to window
        frame.add(game);
        frame.pack();   

        // Settings of Window
        frame.setResizable(false);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

    }

}

Screenshot of the program.

青色背景应该是底部和右侧的64 x 64边框。绘制的框是正确的,我通过调整窗口大小来检查。但是,窗口太小,无法容纳画布。

游戏类:

public class Game extends Canvas implements Runnable {

private Handler handler;

public Game() {

    // Create new window
    Window window = new Window("Horses Aren't Real", this);

    handler = new Handler();
    this.addKeyListener(new KeyInput(handler));

    handler.addObject(new Daanish(100, 100, ID.Player, handler));

}

public static void main (String args[]) {

    // Creates new game
    new Game();

}

public void draw() {

    // Creates a new BufferStrategy
    BufferStrategy bs = this.getBufferStrategy();

    if (bs == null) {

        // This allows the game to preload 3 frames in order to prevent choppy framrate
        this.createBufferStrategy(3);

        return;

    }

    // Create Graphics
    Graphics g = bs.getDrawGraphics();

    g.setColor(Color.cyan);
    g.fillRect(0, 0, 1024, 640);

    g.setColor(Color.black);
    g.fillRect(0, 0, 960, 576);

    handler.draw(g);

    // Remove frame from queue
    g.dispose();
    bs.show();

}

@Override
public Dimension getPreferredSize() {

    return new Dimension(1024, 640);

}

}

2 个答案:

答案 0 :(得分:2)

因此您遇到了许多问题。首先,窗口的大小还必须考虑框架装饰,这意味着内容的可用空间是窗口的大小减去框架装饰的大小。

不建议直接设置窗口的大小。

pack询问框架的内容以获取其首选尺寸,并使用该尺寸将窗口包裹在其周围。因此,您有了window size - decorations = content size

,而不是content size + decorations = window size

JavaDocs

  

使此窗口的大小适合其子组件的首选大小和布局。如果任意一个尺寸小于上次调用setMinimumSize方法指定的最小尺寸,则窗口的宽度和高度将自动放大。

因此,相反,重写preferredSize类的Game并返回所需的空间量,可能类似于...

public class Game extends JPanel {
    //...
    @Override
    public Dimension getPreferredSize() {
        return new Dimension(800, 600);
    }
    //...
}

您的下一个问题是WindowJFrame扩展而来,但是在构造函数中,您正在创建另一个Frame ...那么哪个实际上在做什么?

消除混乱。避免从诸如JFrame之类的顶级容器中扩展,因为您没有为其添加任何新功能

public class Window {

    public Window(String title, Game game) {

        // Creates new JFrame
        JFrame frame = new JFrame(title);

        // Adds Game to window
        frame.add(game);
        frame.pack();

        // Settings of Window
        frame.setResizable(false);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

    }

}

已更新...

因此,我拿走了您的“更新”代码,对其进行了修改,使其可以运行,运行并且没有任何问题...

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                Game game = new Game();
                Window window = new Window("Help", game);

                game.start();
            }
        });
    }

    public class Window {

        public Window(String title, Game game) {

            // Creates new JFrame
            JFrame frame = new JFrame(title);

            // Adds Game to window
            frame.add(game);
            frame.setResizable(false);
            frame.pack();

            // Settings of Window
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);

        }

    }

    public class Game extends Canvas { //implements Runnable {

//      private Handler handler;

        public Game() {
//
//          handler = new Handler();
//          this.addKeyListener(new KeyInput(handler));
//
//          handler.addObject(new Daanish(100, 100, ID.Player, handler));

        }

        public void start() {
            Thread thread = new Thread(new Runnable() {
                @Override
                public void run() {
                    while (true) {
                        draw();
                        try {
                            Thread.sleep(40);
                        } catch (InterruptedException ex) {
                        }
                    }
                }
            });
            thread.start();
        }

        public void draw() {

            // Creates a new BufferStrategy
            BufferStrategy bs = this.getBufferStrategy();

            if (bs == null) {
                // This allows the game to preload 3 frames in order to prevent choppy framrate
                this.createBufferStrategy(3);
                return;
            }

            // Create Graphics
            Graphics g = bs.getDrawGraphics();

            g.setColor(Color.cyan);
            g.fillRect(0, 0, 1024, 640);

            g.setColor(Color.black);
            g.fillRect(0, 0, 960, 576);

//          handler.draw(g);

            // Remove frame from queue
            g.dispose();
            bs.show();

        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(1024, 640);
        }

    }

}

但是,我正在MacOS上运行。我在Windows上遇到过setResizable的一个鲜为人知的问题,在Windows上,调用setResizable时,窗口装饰会更改大小。

“基本”解决方案是在调用setResziable之前先调用pack,以便在API决定如何更新窗口大小之前修改框架装饰。

我以为这已在Java的更高版本(8+)中修复,但是由于我没有运行Windows,因此我无法进行测试

有关更多详细信息,请参见My JFrame always becomes a few pixels too big.

答案 1 :(得分:1)

public class Window extends JFrame {

public Window(final int width, final int height, String title, Game game) {
    super(title);
    // Set dimensions
    Dimension d = new Dimension(width, height);
    game.setPreferredSize(d);

    // Adds Game to window
    add(game);
    // Settings of Window
    setResizable(false);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLocationRelativeTo(null);
    pack();
    setVisible(true);

}

}

使用包时,框架通常会调整为内容大小