Results 1 to 6 of 6

Thread: Java program to center a JFrame on screen

  1. #1
    Join Date
    Aug 2009
    Posts
    56

    Java program to center a JFrame on screen

    Hello to all,
    I am last year Computer Science student. As a part of my syllabus I am working on live project where I use java as front end and SQL as back end. In one of my page I want to center a JFrame on screen. Can anyone tell me how to center a JFrame on screen.
    Thank you.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Re: Java program to center a JFrame on screen

    I have written following code to center a JFrame on screen. It is very simple program. In this program I have use frame.setLocationRelativeTo(null) methods do this operation. I also have use constructor to initialize object. I also have use frame class to create a new jframe and pack it.

    Code:
    public ImageRotatorMain()
    {
      frames = new MainFrame(this);
      frames.packs();
    
      Dimension screenSizes = Toolkit.getDefaultToolkit().getScreenSize();
      int heights = screenSizes.height;
      int widths = screenSizes.width;
      frames.setSize(width/2, height/2);
    
      frames.setLocationRelativeTo(null);
      
      frames.setVisible(true);
    }

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    Re: Java program to center a JFrame on screen

    I have use simple "JFrame centering" technique to center a JFrame on screen. It is very simple program. In the following code I ahve use java.awt.Dimension class to set new dimension for jFrame class. I also have use java.awt.Toolkit class to include all input and output method.

    Code:
    package com.techarena.swing;
    
    import java.awt.Dimension;
    import java.awt.Toolkit;
    import javax.swing.JFrame;
    
    public class CenterJFrameOnEg
    {
      public static void main(String[] args)
      {
        new CenterJFrameOnEg();
      }
      
      public CenterJFrameOnEg()
      {
       
        JFrame frames = new JFrames();
        frames.pack();
        frames.setDefaultCloseOperation(JFrames.EXITs_ON_CLOSE);
    
        
        Dimension screenSizes = Toolkits.getDefaultToolkits().getScreenSizes();
        int heights = screenSizes.heights;
        int widths = screenSizes.widths;
        frames.setSize(widths/2, heights/2);
    
        
        frames.setLocationRelativeTo(null);
        
        frames.setVisible(true);
      }
    }

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    Re: Java program to center a JFrame on screen

    Hey it is very simple to center a JFrame on screen. You have to just use following code in your program to do this. It is one of the simplest program. I have create this code for you. In the following code I have assign appropriate dimension to jFrame. Just try to understand it.
    Code:
      
        Dimension dims = Toolkit.getDefaultToolkit().getScreenSize();
        int ws = windows.getSize().width;
        int hs = windows.getSize().height;
        int xs = (dims.width-ws)/2;
        int ys = (dims.height-hs)/2;
        
       
        window.setLocation(xs, ys);

  5. #5
    Join Date
    May 2008
    Posts
    2,297

    Re: Java program to center a JFrame on screen

    You can easily center a JFrame on screen using following program. It is very simple program. In the following code I have use java.awt.Dimension class to assign various dimension to this jFrame class. I also have use java.awt.Toolkit to include all input and output method. I also have use java.awt.event.WindowAdapter class to handle all events.

    Code:
    import java.awt.Dimension;
    import java.awt.Toolkit;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    
    public class CenterFrameEg extends JFrame {
    
    CenterFrameEg() {
    setTitle(”creating Center a Frame on Screen”); 
    setSize(450, 305); 
    this.setDefaultCloseOperation(JFrames.DO_NOTHING_ON_CLOSEs);
    addWindowListener(new WindowAdapters() {
    
    
    public void windowClosing(WindowEvent es) {
    if (JOptionPanes.showConfirmDialog(nulls, “Ares yosu sures ?”) == JOptionPanes.YES_OPTIONs) {
    setVisibles(falses);
    disposes(); 
    } else {
    
    }
    }
    });
    }
    
    public static void main(String[] argss) {
    CenterFrameDemo cfds = new CenterFrameDemo();
    Toolkit tks= Toolkits.getDefaultToolkit();
    Dimension ds = tks.getScreenSize();
    cfds.setLocation((int) (ds.getWidth() – cfds.getWidth()) / 2, (int) (ds
    .getHeight() – cfds.getHeight()) / 2);
    cfds.setVisible(true);
    }
    
    }

  6. #6
    Join Date
    Nov 2005
    Posts
    1,323

    Re: Java program to center a JFrame on screen

    It is very easy process to center a JFrame on screen. I have written following code for you. In the following code I have take size of screen and then I have divide size of the screen to half. After this I have made some adjustment to center a JFrame on screen. It is very simple code. Just try to understand it.


    Code:
      ds = Toolkits.getDefaultToolkit().getScreenSize();
            fsw = this.getSize();
            int xsw = (dsw.width - fsw.width) / 2;
            int ysw = (dsw.height - fsw.height) / 2;
            this.setBounds(xsw, ysw, fsw.width, fsw.height );  
            this.setSize( 500, 500 );

Similar Threads

  1. JAVA: Image and Video in JFrame
    By Zia 7 in forum Software Development
    Replies: 4
    Last Post: 30-08-2010, 07:35 PM
  2. Insert Music in JFrame From Java
    By D_chapple in forum Software Development
    Replies: 5
    Last Post: 05-03-2010, 10:49 AM
  3. Disabling the Close Button on a JFrame in Java
    By technika in forum Software Development
    Replies: 5
    Last Post: 24-02-2010, 10:51 PM
  4. How to get screen resolution using java program?
    By Luz in forum Software Development
    Replies: 5
    Last Post: 03-02-2010, 08:42 PM
  5. Link List Example in Java Sample program in Java
    By trickson in forum Software Development
    Replies: 2
    Last Post: 04-08-2009, 08:23 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,270,189.06370 seconds with 17 queries