views:

179

answers:

4

Hello, I have problem setting the size of drawing panel. I want a drawing panel with the size 0f 600,600.However I found that, the size of the drawing panel is smaller than 600,600. It seems that the frame size is 600,600 which make the drawing panel smaller. How can I set the drawing panel size 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);    
   }


I have changed the code,however the problem still exist. At this time the size of the drawing panel is larger than the intended size dimension.

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); 
  }
+1  A: 

Set the preferred size of your component to 600,600, and call pack() on the frame.

Zed
+2  A: 

The reason is that there is some space reserved for 'inset' such as border. To know how much extra space is needed called the frame 'getInsets()'.

NawaMan
A: 

do as NawaMan suggested, and take the insets into account, or set the size of your 'PaintSurface' Object to 600x600. Of course you may then need a scroll pane to get the full size.

broschb
A: 


The problem SOLVED

  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)); 
.....
}
Jessy