いくつかのグラフィックスと物事を制御するためのいくつかのボタン用に JPanel を含むフォームを作成しています。なんらかの理由で、JPanel の幅を 10 ピクセル低く、高さを 30 ピクセル低く指定する必要があります。この問題の原因は何ですか?
これはコードです:
public class Window {
public Sheepness sheepness;
public ButtonPanel buttonPanel;
public PaintPanel paintPanel;
public JFrame frame;
public Window(Sheepness sheepness) {
this.sheepness = sheepness;
frame = new JFrame("Sheepness simulation");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//frame.setSize(width, height);
BorderLayout frameLayout = new BorderLayout();
JPanel background = new JPanel(frameLayout);
background.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
buttonPanel = new ButtonPanel(this);
background.add(BorderLayout.SOUTH, buttonPanel.buttonBox);
paintPanel = new PaintPanel(this);
paintPanel.setPreferredSize(new Dimension(320, 240));
background.add(BorderLayout.CENTER, paintPanel);
frame.getContentPane().add(background);
frame.pack();
frame.setResizable(false);
frame.setVisible(true);
}
}
public class PaintPanel extends JPanel {
public Window window;
public PaintPanel(Window window) {
this.window = window;
}
@Override
public void paintComponent(Graphics g) {
g.setColor(Color.blue);
g.fillRect(0, 0, 320, 240);
}
}
preferredSize が 320 x 240 のスクリーンショット:
ソース画像 http://www.cmbi.ru.nl/~estens/Sheepness/Sheepness_simulation_border.png が見つかりません。
320 x 240 の fillRect が JPanel を完全に塗りつぶさず、幅 10 ピクセル、高さ 30 ピクセルの境界線が残っていることがわかります。
preferredSize が 310 x 210 のスクリーンショット:
ソース画像 http://www.cmbi.ru.nl/~estens/Sheepness/Sheepness_simulation_noBorder.png が見つかりません。
320 x 240 の fillRect にぴったり収まるようになりました。
何か案は?