我认为 Java 在计算窗口总大小时存在一个错误,它将顶部和右边框值第二次添加到整个窗口大小中。如果这是一个已知问题,我深表歉意,但我没有在所有材料中找到它。
我对最新的 java 7 21(使用 Netbeans 7.3)有这个问题。
我正在尝试创建一个内容区域正好为 1024x768(在更大的显示器上)的窗口,以在用于游戏开发的窗口中模拟 1024x768 显示器的全屏。我只是创建一个框架并在窗口中绘制一个 1024x768 黑色矩形。
内容区域太大,正好是标题栏的高度(顶部边框插入值),宽度正好是右侧边框的大小。这让我大吃一惊,因为右边框紧挨着一条宽度完全相同的条纹。
在调试中,pack()/setVisible(true) 之后的插图是 30 顶部、8 边和底部。
在黑色矩形下方的窗口边框底部内侧有一个 30 像素的白色条带,右侧有一个 8 像素的边框。
我尝试在窗口中使用内容窗格 JPanel,将其添加到 BorderLayout.CENTER 的结果完全相同。
这应该是正确的,而不是: setPreferredSize(contentDim.width + insets.left + insets.right, contentDim.height + insets.top + insets.bottom); 这有效且不应该: setPreferredSize(contentDim.width + insets.left, contentDim.height + insets.bottom);
这是完整的代码。
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package jframesizebug;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.image.BufferStrategy;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
/**
*
* @author liblabs-guest
*/
public class JFrameBug extends JFrame {
public JFrameBug() {
setTitle("JFrameSizeBug");
addWindowListener(new java.awt.event.WindowAdapter() {
@Override
public void windowClosing(java.awt.event.WindowEvent e) {
System.exit(0);
}
});
pack();
Insets insets = getInsets();
contentDim = new Dimension(500,400);
/*
* This SHOULD be correct to the pixel, but it creates a window
* that is too big vertically by the top inset, and horizontally
* by the right inset.
* When run there is a white strip along the bottom of the content area
* inside the outer border that is the same size as the title bar at
* the top of the window. Also there is a white stripe inside the
* right border of the content area the same width as the outer border.
*/
windowDim = new Dimension(
contentDim.width + insets.left + insets.right,
contentDim.height + insets.top + insets.bottom);
/*
* This works and shouldn't
windowDim = new Dimension(
contentDim.width + insets.right,
contentDim.height + insets.bottom);
*/
/*
* This shows the problem with a white border of 2 pixels at the
* bottom and right.
windowDim = new Dimension(
contentDim.width + insets.right,
contentDim.height + insets.bottom);
*/
setSize(windowDim);
setPreferredSize(windowDim);
setMinimumSize(windowDim);
setMaximumSize(windowDim);
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrameBug frame = new JFrameBug();
frame.renderLoop();
}
});
}
private void renderLoop() {
setVisible(true);
createBufferStrategy(1);
bufferStrategy = getBufferStrategy();
g = bufferStrategy.getDrawGraphics();
if (!bufferStrategy.contentsLost()) {
g.setColor(Color.BLACK);
g.fillRect(0,0,contentDim.width, contentDim.height);
bufferStrategy.show();
}
try {
Thread.sleep(5000);
} catch (InterruptedException ex) {
Logger.getLogger(JFrameBug.class.getName()).log(
Level.SEVERE, null, ex);
}
}
private BufferStrategy bufferStrategy;
private Dimension contentDim;
private Dimension windowDim;
private Graphics g;
}