如果我用JFrame创建一个窗口底部有几行像素右边有几列在窗口外面所以它们不会显示出来。例如,当我创建一个800x600的窗口时,窗口将只显示左上角786x563区域的内容。被切断的数量似乎也随着窗口的大小而变化。到目前为止,我只是手动调整了东西的大小和位置。目前在我的代码是这样设置的:
import java.awt.*; //Canvas and Dimension
import java.util.*;
import javax.swing.JFrame;
import java.awt.image.BufferStrategy;
import java.awt.event.*;
public class Main extends Canvas implements Runnable {
static final int WIDTH = 800, HEIGHT = WIDTH/4*3; //800, 600
private Thread thread;
private boolean running = false;
public Main() { //constructor
new Window(WIDTH, HEIGHT, "Game", this);
requestFocus();
}
public synchronized void start() {
thread = new Thread(this);
thread.start();
running = true;
}
public synchronized void stop() {
try {
thread.join();
running = false;
}
catch(Exception e) {
e.printStackTrace();
}
}
public void run() {
long lastTime = System.nanoTime();
double amountOfTicks = 60.0;
double ns = 1000000000 /amountOfTicks;
double delta = 0;
long timer = System.currentTimeMillis();
int frames =0;
while(running) {
long now = System.nanoTime();
delta+= (now - lastTime) / ns;
lastTime = now;
while(delta>=1) {
tick();
delta--;
}
if(running)
render();
frames++;
if(System.currentTimeMillis() - timer > 1000) {
timer+=1000;
frames = 0;
}
}
stop();
}
public void render() {
BufferStrategy bs = this.getBufferStrategy();
if(bs==null) {
this.createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();
//creates a line that should be 38 units above the bottom but instead is right along the bottom
g.setColor(Color.GREEN);
g.fillRect(1, 562, 30, 1);
g.dispose();
bs.show();
try{Thread.sleep(5);} catch(Exception e) {}
}
public void tick() {
}
public static void main(String args[]) {
new Main();
}
}
class Window extends Canvas {
public Window(int w, int h, String t, Main game) {
JFrame frame = new JFrame(t);
frame.setPreferredSize(new Dimension(w,h));
frame.setMinimumSize(new Dimension(w,h));
frame.setMaximumSize(new Dimension(w,h));
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(game);
frame.setLocationRelativeTo(null);
frame.pack();
frame.setVisible(true);
game.start();
}
}
如果我没有包含足够的代码来诊断问题,请告诉我,以便我可以把它的其余部分。