0

我在设置绘图面板的大小时遇到​​问题。我想要一个大小为 0f 600,600 的绘图面板。但是我发现绘图面板的大小小于 600,600。似乎框架大小为 600,600,这使得绘图面板更小。如何设置绘图面板大小 600,600 ?

....
public class DrawingBoardWithMatrix extends JFrame {
   public static void main(String[] args) {
      new DrawingBoardWithMatrix();
   }

   public DrawingBoardWithMatrix(){     
      this.getContentPane().setBackground(Color.WHITE);
      this.setSize(600, 600);
      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      this.add(new PaintSurface(), BorderLayout.CENTER);
      this.setVisible(true);
      setResizable(false);    
   }

我已经更改了代码,但问题仍然存在。此时绘图面板的尺寸大于预期的尺寸尺寸。

public class DrawingBoardWithMatrix extends JFrame {
  public static void main(String[] args) {
      new DrawingBoardWithMatrix();
  }

  public DrawingBoardWithMatrix(){  
      Container c = getContentPane();
      c.add(new PaintSurface(), BorderLayout.CENTER);
      c.setPreferredSize(new Dimension(600,600));
      pack();
      this.setVisible(true);
      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    
      setResizable(false); 
  }
4

4 回答 4

3

原因是为“插图”保留了一些空间,例如边框。要知道需要多少额外空间,称为框架' getInsets()'。

于 2009-09-26T14:54:00.857 回答
2

将组件的首选大小设置为 600,600,然后在框架上调用 pack()。

于 2009-09-26T14:47:57.653 回答
0

按照 NawaMan 的建议进行操作,并考虑插图,或者将“PaintSurface”对象的大小设置为 600x600。当然,您可能需要一个滚动窗格来获得完整尺寸。

于 2009-09-26T22:32:24.390 回答
0

问题已解决

  public DrawingBoardWithMatrix(){  
      Container c = getContentPane();
      c.add(new PaintSurface(), BorderLayout.CENTER);
      setResizable(false);      //put the code setResizable(false) before pack();
      pack();
      this.setVisible(true);
      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    

  }

public PaintSurface() {
    this.setPreferredSize(new Dimension (600,600)); 
.....
}
于 2009-09-27T14:04:00.393 回答