I'm creating a simple Break Out style game. The main game extends JFrame and I'm adding a JPanel to the frame.When I was using paint() to draw the game graphics the items sat within the window as expected (i.e by their x, y coordinates).I've updated the code to use BufferStrategy as I was getting flickering. Since the, the graphics that are rendered are offset by 22px.This means the Bricks are off the top of the screen!The code is as follows:package BreakOut;import javax.swing.*;import java.awt.*;import java.awt.event.KeyEvent;import java.awt.event.KeyListener;import java.awt.image.BufferStrategy;public class Game extends JPanel implements KeyListener{GameStateManager gsm = new GameStateManager();BufferStrategy strategy;public Game() { //add menu state to GameStateManager gsm.add(new MenuState(gsm)); createFrame(); while(true) { gsm.update(); //repaint(); render(); try{ Thread.sleep(10); } catch(InterruptedException e) { } }}public void createFrame(){ JFrame frame = new JFrame("Mini Tennis"); frame.setLayout(new BorderLayout()); this.setPreferredSize(new Dimension(400,400)); frame.add(this); frame.pack(); frame.setBackground(Color.BLACK); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); addKeyListener(this); this.setFocusable(true); frame.createBufferStrategy(2); strategy = frame.getBufferStrategy(); frame.setVisible(true); System.out.println(frame.getInsets());}public void render(){ Graphics g = strategy.getDrawGraphics(); super.paint(g); gsm.render(g); g.dispose(); strategy.show();}public void keyPressed(KeyEvent k) { switch(gsm.getState()) { case MAIN_MENU: if(k.getKeyCode()==KeyEvent.VK_ENTER) { //add the PlayState to the Stack and update enum value gsm.add(new PlayState(gsm, this)); } break; case PLAYING: if(k.getKeyCode()==KeyEvent.VK_P) { //add the PlayState to the Stack and update enum value gsm.add(new PauseState(gsm)); } break; case PAUSE: if(k.getKeyCode()==KeyEvent.VK_P) { gsm.pop(); } break; case GAME_OVER: if(k.getKeyCode()==KeyEvent.VK_ENTER) { gsm.pop(); } break; } //send input to GameStateManager gsm.keyPressed(k.getKeyCode());}public void keyReleased(KeyEvent k) { gsm.keyReleased(k.getKeyCode());}public void keyTyped(KeyEvent k) { gsm.keyTyped(k.getKeyCode());}public static void main(String[] args) { new Game();}}When I output System.out.println(frame.getInsets()); I getjava.awt.Insets[top=22,left=0,bottom=0,right=0]I'm obviously doing something wrong but can't figure out why adding BufferStrategy would create the JPanel to be offset by 22pxAny help would be appreciated :) 解决方案 Frames have borders and decorations, which is included within the bounds of the frame (they don't get added to the outside), from the looks of things you're using MacOS and the 22pixels to the top is the window title.Best solution is, don't use the frame as the render surface, instead, use the Game class. This will mean it will need to extend from java.awt.Canvas instead of javax.swing.JPanel and you will need to create the BufferStrategy from itIf you override the Canvas's getPreferredSize method, you can use pack on the frame it will pack the window around this, so the physical frame will be larger then the content, but the content will be the size you would preferYou will also want to move you main/game loop to separate thread, as this is current in the risk of blocking the Event Dispatching Thread, which could cause you no end of issues (like never getting a key event) 这篇关于JFrame将22像素偏移量(java.awt.Insets)添加到Frame的顶部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!