Often Java Developers ask how they can center their JFrame window to the screen. I have previously used JFrame.setLocationRelativeTo(nu...
Often Java Developers ask how they can center their JFrame window to the screen. I have previously used JFrame.setLocationRelativeTo(null) method to center the screen but i have realized that depending on screen size, itI need to make the JFrame window to fill the screen depending on its size.
Below codes shows how to use java.awt.Toolkit to set JFrame window to occupy screen size of device
import javax.swing.JFrame; import javax.swing.JButton; import java.awt.Dimension; import java.awt.Toolkit; import javax.swing.UIManager; public class Pointofsale { /** * @param args the command line arguments */ public static void main(String[] args) { try { UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel"); } catch(Exception e) { } // TODO code application logic here JFrame p=new JFrame(); Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); p.setSize(screenSize.width, screenSize.height); p.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); p.setVisible(true); } }