Results 1 to 5 of 5

Thread: Creating a Frame of an exact size

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jun 2002
    Location
    Far.. far away! (Netherlands)
    Posts
    169

    Resolved Creating a Frame of an exact size

    Hello all,

    I'm creating a simple Frame object by extending it in my own class, and I've set the size using the setSize function, but it turns out the size required by the function has the title bar and border included, does anyone know how to create a window with a client area of, say 500,400? There must be some function the get the sizes of the borders and all, as they can change.

    Thanks in advance,

    Shel
    Last edited by Dilenger4; Oct 7th, 2004 at 05:04 PM.

  2. #2
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Code:
      import java.awt.*;  
      import javax.swing.*; 
    
      public class Client{
       public static void main(String[] args){  
        
        JFrame jf = new JFrame();
        jf.setSize(500,400);
        jf.setVisible(true);
        Dimension size = jf.getSize();
        System.out.println(size);
        Insets insets = jf.getInsets();
        int drawwidth = size.width - insets.left - insets.right;
        int drawheight = size.height - insets.top - insets.bottom;
        jf.setSize(drawwidth,drawheight);
        System.out.println(jf.getSize()); // dimensions without insets
        
      }
    }

  3. #3
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    This should work. The insets are just added to the original size of the JFrame.
    Code:
     import java.awt.*;  
     import javax.swing.*; 
    
     public class Client{
      public static void main(String[] args){  
        
        JFrame jf = new JFrame();
        jf.setSize(500,400);
        jf.setVisible(true);
        Dimension size = jf.getSize();
        System.out.println(size);
        Insets insets = jf.getInsets();
        int insetwidth = insets.left + insets.right;
        int insetheight = insets.top + insets.bottom;
        jf.setSize(500 + insetwidth,400 + insetheight);
        System.out.println(jf.getSize());
      }
    }

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jun 2002
    Location
    Far.. far away! (Netherlands)
    Posts
    169
    Thanks! Works like a dream

  5. #5
    Fanatic Member brown monkey's Avatar
    Join Date
    Jun 2004
    Location
    Cebu
    Posts
    552
    ... or simply Extends from JFrame object
    Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class sample extends JFrame{
        public sample(){
            setSize(500,500);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
        }
        public static void main(String[] args){
            new sample().show();
        }
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width