Get the current size of the desktop and the size of current JFrame and divide by 2 the size difference.
Full example code
import javax.swing.*;
import java.awt.*;
public class MyWindow extends JFrame
{
public MyWindow()
{
setSize(300,300);
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
int x = (int) ((screen.getWidth()-getWidth()) /2);
int y = (int) ((screen.getHeight()-getHeight()) /2);
setLocation(x, y);
setVisible(true);
}
public static void main(String args[])
{
new MyWindow();
}
}
Full example code
import javax.swing.*;
import java.awt.*;
public class MyWindow extends JFrame
{
public MyWindow()
{
setSize(300,300);
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
int x = (int) ((screen.getWidth()-getWidth()) /2);
int y = (int) ((screen.getHeight()-getHeight()) /2);
setLocation(x, y);
setVisible(true);
}
public static void main(String args[])
{
new MyWindow();
}
}
No comments:
Post a Comment