Swing Game,如何放置游戏窗口布局?

时间:2017-01-15 14:17:21

标签: java swing layout jpanel layout-manager

我正在使用Swing制作俄罗斯方块,我想将游戏板放在屏幕中间,尺寸固定,旁边有1或2个面板。 我想让俄罗斯方块游戏从JFrame获得一些余量,如果可能的话,仍然从容器内的0,0个位置开始。

目标:

我已尝试将游戏添加到JPanel并将其居中,但游戏不会显示。

我的代码:

public class Window {

public Window(int width, int height, String title, Game game) {

    JFrame frame = new JFrame(title);
    frame.setPreferredSize(new Dimension(width, height));
    frame.setMaximumSize(new Dimension(width, height));
    frame.setMinimumSize(new Dimension(width, height));
    frame.setVisible(true);

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setResizable(false);
    frame.setLocationRelativeTo(null);
    frame.add(game);
    game.start();
}
} 

我找到了居中的固定布局,但没有奏效:

JFrame Frame = new JFrame("CenterFrame");
    Frame.setLocation(0, 0);
    Frame.setSize(new Dimension(400, 400));// dim

    JPanel FixedPanel = new JPanel(new GridBagLayout());
    FixedPanel.setPreferredSize(Frame.getSize());

    JPanel myPanel = new JPanel();
    myPanel.setPreferredSize(new Dimension(100, 100));
    myPanel.setBackground(Color.BLACK);

    FixedPanel.add(myPanel);
    Frame.add(FixedPanel);
    Frame.setVisible(true);

1 个答案:

答案 0 :(得分:0)

这是你想要的吗?

private void initGameFrame(int gameWidth, int height, int scoreWidth, int border, String title) {
    //game frame
    JFrame frame = new JFrame(title);
    frame.setSize(new Dimension(gameWidth + scoreWidth, height));

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocationRelativeTo(null);
    frame.getRootPane().setBorder(BorderFactory.createLineBorder(Color.RED, 10));
    frame.getContentPane().setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.X_AXIS));

    // Game panel
    JPanel gamePanel = new JPanel();
    gamePanel.setMinimumSize(new Dimension(0, 0));
    gamePanel.setMaximumSize(new Dimension(Short.MAX_VALUE, Short.MAX_VALUE));
    gamePanel.setPreferredSize(new Dimension(gameWidth, height));
    gamePanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
    gamePanel.setBackground(Color.BLUE);

    // Score panel
    JPanel scorePanel = new JPanel();
    scorePanel.setMinimumSize(new Dimension(0, 0));
    scorePanel.setMaximumSize(new Dimension(Short.MAX_VALUE / 2, Short.MAX_VALUE));
    scorePanel.setPreferredSize(new Dimension(scoreWidth, height));
    scorePanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
    scorePanel.setBackground(Color.GREEN);

    // game canvas
    RunnableCanvas game = new RunnableCanvas(gameWidth, height);
    game.setMinimumSize(new Dimension(0, 0));
    game.setMaximumSize(new Dimension(Short.MAX_VALUE, Short.MAX_VALUE));
    game.setBackground(Color.YELLOW);

    gamePanel.setLayout(new BorderLayout());
    gamePanel.add(game, BorderLayout.CENTER);

    frame.getContentPane().add(gamePanel);
    frame.getContentPane().add(scorePanel);
    frame.pack();
    frame.setVisible(true);

    // your real game thread should be here 
    Thread gameThread = new Thread(game);
    gameThread.start(); 
}

你的Runnable画布:

public class RunnableCanvas extends Canvas implements Runnable {
    int width = 300;
    int height = 400; 

    public RunnableCanvas(int width, int height) {
      this.width = width;
      this.height = height;
    }

    @Override
    public void run() {
        System.out.println("Running");
    }

    @Override
   public Dimension getPreferredSize() {
       return new Dimension(width, height);
   }

   @Override
   public void paint(Graphics g) {
      g.setColor(Color.ORANGE);
      g.fillRect(0, 0, width, height);
   }
}