【发布时间】:2018-09-30 00:40:21
【问题描述】:
由于某种原因,运行时 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);
}
}
青色背景应该是底部和右侧的 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);
}
}
【问题讨论】:
-
这段代码存在许多问题,包括假设框架应该与其中包含的“内容区域”大小相同。框架装饰的大小因操作系统而异。
-
@AndrewThompson 你会如何解决这个问题?
-
如需尽快获得更好的帮助,请发帖minimal reproducible example 或Short, Self Contained, Correct Example。