简体   繁体   English

调整JFrame的大小以适合调整后的JPanel

[英]Resizing JFrame to fit resized JPanel

I am pretty new with JPanel and JFrame and probably not working with them the way I should. 我对JPanel和JFrame很陌生,可能无法按我应该的方式使用它们。 I'm trying to create Pong game and after creating JFrame and adding JPanel to it, I'm able to resize my JPanel but I don't know how to resize my JFrame to fit it. 我正在尝试创建Pong游戏,并在创建JFrame并将其添加到JPanel之后,我能够调整JPanel的大小,但是我不知道如何调整JFrame的大小以适合它。 Game class extends JPanel. 游戏类扩展了JPanel。

main: 主要:

    public static void main(String[] args) throws InterruptedException {
        int height = 500, width =(int) (height*1.56); //height = 500, width = 780;
        JFrame frame = new JFrame("Pong");
        Game game = new Game();
        frame.add(game);
        frame.setVisible(true);
        game.setSize(width, height);
        System.out.println(game.getHeight());
        System.out.println(game.getWidth());
        game.setBackground(Color.BLACK);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        System.out.println(frame.getHeight());
        System.out.println(frame.getWidth());
    }

output: 输出:

500
780
39
136

The output should be somthing like: 输出应该是这样的:

500
780
*above* 500
*above* 780

EDIT: 编辑:

    public static void main(String[] args) throws InterruptedException {
        int height = 500, width =(int) (height*1.56); //height = 500, width = 780;
        JFrame frame = new JFrame("Pong");
        Game game = new Game();
        frame.add(game);
        frame.setVisible(true);
        game.setPreferredSize(new Dimension(width, height));
        frame.pack();
        System.out.println(game.getHeight());
        System.out.println(game.getWidth());
        game.setBackground(Color.BLACK);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        System.out.println(frame.getHeight());
        System.out.println(frame.getWidth());
    }

Changing the setSize to setPreferredSize and then call pack() for the JFrame fixed it all. 将setSize更改为setPreferredSize,然后为JFrame调用pack()修复了所有问题。

Again, deal with preferred sizes and make sure to call pack() on your JFrame after adding components. 同样,处理首选大小,并确保添加组件在JFrame上调用pack() Also, it's better to override getPreferredSize rather than calling setPreferredSize . 另外,最好重写getPreferredSize而不是调用setPreferredSize For example: 例如:

import java.awt.Dimension;
import javax.swing.*;

public class Game extends JPanel {
    private static final int PREF_W = 400;
    private static final int PREF_H = 300;
    private int prefW;
    private int prefH;

    public Game(int prefW, int prefH) {
        this.prefW = prefW;
        this.prefH = prefH;
    }

    public Game() {
        this(PREF_W, PREF_H);
    }

    @Override
    public Dimension getPreferredSize() {
        if (isPreferredSizeSet()) {
            return super.getPreferredSize();
        }
        return new Dimension(prefW, prefH);
    }

    private static void createAndShowGui() {
        int height = 500; 
        int width =(int) (height*1.56); //height = 500, width = 780;

        Game game = new Game(width, height);

        JFrame frame = new JFrame("Game");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.getContentPane().add(game);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);

        System.out.println("Frame size: " + frame.getSize());
        System.out.println("game size: " + game.getSize());
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> createAndShowGui());
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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