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