JFrame window extension

I collect fast and dirty animations using swing. I would like the window to be maximized. How can i do this?

+84
java swing maximize-window
Jan 26 '09 at 11:48
source share
8 answers

If you extend JFrame:

public void run() { MyFrame myFrame = new MyFrame(); myFrame.setVisible(true); myFrame.setExtendedState(myFrame.getExtendedState() | JFrame.MAXIMIZED_BOTH); } 
+127
Jan 26 '09 at 11:58
source share

Something like this.setExtendedState(this.getExtendedState() | this.MAXIMIZED_BOTH);

 import java.awt.*; import javax.swing.*; public class Test extends JFrame { public Test() { GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment(); this.setMaximizedBounds(env.getMaximumWindowBounds()); this.setExtendedState(this.getExtendedState() | this.MAXIMIZED_BOTH); } public static void main(String[] args) { JFrame.setDefaultLookAndFeelDecorated(true); Test t = new Test(); t.setVisible(true); } } 
+17
Jan 26 '09 at 11:54
source share

If you use JFrame, try this.

 JFrame frame = new JFrame(); //... frame.setExtendedState(JFrame.MAXIMIZED_BOTH); 
+12
Jan 26 '09 at 11:54
source share
+7
Jan 26 '09 at 11:54
source share

The way to set the JFrame to full screen mode is to set the MAXIMIZED_BOTH parameter which stands for MAXIMIZED_VERT | MAXIMIZED_HORIZ MAXIMIZED_VERT | MAXIMIZED_HORIZ MAXIMIZED_VERT | MAXIMIZED_HORIZ MAXIMIZED_VERT | MAXIMIZED_HORIZ , which respectively set the frame to maximize vertically and horizontally

 package Example; import java.awt.GraphicsConfiguration; import javax.swing.JFrame; import javax.swing.JButton; public class JFrameExample { static JFrame frame; static GraphicsConfiguration gc; public static void main(String[] args) { frame = new JFrame(gc); frame.setTitle("Full Screen Example"); frame.setExtendedState(MAXIMIZED_BOTH); JButton button = new JButton("exit"); b.addActionListener(new ActionListener(){@Override public void actionPerformed(ActionEvent arg0){ JFrameExample.frame.dispose(); System.exit(0); }}); frame.add(button); frame.setVisible(true); } } 
+2
Jan 26 '09 at 11:56
source share

I like this version:

 import java.awt.Dimension; import java.awt.GraphicsConfiguration; import java.awt.Toolkit; import javax.swing.JFrame; public class Test { public static void main(String [] args) { final JFrame frame = new JFrame(); final GraphicsConfiguration config = frame.getGraphicsConfiguration(); final int left = Toolkit.getDefaultToolkit().getScreenInsets(config).left; final int right = Toolkit.getDefaultToolkit().getScreenInsets(config).right; final int top = Toolkit.getDefaultToolkit().getScreenInsets(config).top; final int bottom = Toolkit.getDefaultToolkit().getScreenInsets(config).bottom; final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); final int width = screenSize.width - left - right; final int height = screenSize.height - top - bottom; frame.setResizable(false); frame.setSize(width,height); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } } 
+2
May 02 '10 at 11:52 a.m.
source share

I ended up using this code:

 public void setMaximized(boolean maximized){ if(maximized){ DisplayMode mode = this.getGraphicsConfiguration().getDevice().getDisplayMode(); Insets insets = Toolkit.getDefaultToolkit().getScreenInsets(this.getGraphicsConfiguration()); this.setMaximizedBounds(new Rectangle( mode.getWidth() - insets.right - insets.left, mode.getHeight() - insets.top - insets.bottom )); this.setExtendedState(this.getExtendedState() | JFrame.MAXIMIZED_BOTH); }else{ this.setExtendedState(JFrame.NORMAL); } } 

These settings worked better than all settings, including support for multiple monitors. The only drawback to this is that the taskbar offset is used on all monitors - these are some configurations.

0
Nov 23 '14 at 11:35
source share

I tried the solutions in this thread and the ones here , but just called setExtendedState(getExtendedState()|Frame.MAXIMIZED_BOTH); immediately after calling setVisible(true); obviously not working for my environment (Windows 10, JDK 1.8, my taskbar is on the right side of my screen). By doing this this way, there is still a tiny space left, right and bottom.

However, what worked for me was calling setExtendedState(... when the window is activated, like this:

 public class SomeFrame extends JFrame { public SomeFrame() { // ... setVisible(true); setResizable(true); // if you are calling setSize() for fallback size, do that here addWindowListener ( new WindowAdapter() { private boolean shown = false; @Override public void windowActivated(WindowEvent we) { if(shown) return; shown = true; setExtendedState(getExtendedState()|JFrame.MAXIMIZED_BOTH); } } ); } } 
0
Apr 9 '19 at 12:02
source share



All Articles