How to get the maximum frame size?

I set the maximum frame: setExtendedState(getExtendedState() | JFrame.MAXIMIZED_BOTH);

Now, how can I get this size, because when I call getSize() or getPreferredSize , it returns 0 0 ?

+4
source share
3 answers

You will get the maximum size correctly after doing setVisible(true); .

 public NewJFrame() { // Constructor initComponents(); this.setExtendedState( getExtendedState() | JFrame.MAXIMIZED_VERT | Frame.MAXIMIZED_HORIZ); // height and width still prints the original values System.out.println(this.getSize().height + " " + this.getSize().width); } .... public static void main(String args[]) { // main java.awt.EventQueue.invokeLater(new Runnable() { public void run() { NewJFrame foo = new NewJFrame(); foo.setVisible(true); // after setVisible(true) actual maximized values System.out.println(foo.getSize().height + " " + foo.getSize().width); } }); } 
+5
source

I want the size of the panel that is in the frame as large (large) as the frame.

 import java.awt.*; import javax.swing.*; class FullSizePanel { public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { // this is the panel we want to fill the entire content area // of the parent frame. JPanel redFullSizePanel = new JPanel(new BorderLayout()); // make it true to the first part of the attribute name redFullSizePanel.setBackground(Color.RED); JFrame f = new JFrame("Full Size Panel"); // 1) The default layout for a content pane is BorderLayout // 2) A component added to a BL with no layout constraint ends // up in the CENTER // 3) A component in the CENTER of a BL takes the full space // (that is not used up by components in the EAST, WEST, // NORTH & SOUTH). f.add( redFullSizePanel ); f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); f.pack(); f.setSize(400,400); f.setLocationByPlatform(true); f.setVisible(true); } }); } } 

Which layout is useful?

grid or box layout with 1 column and x rows

 import java.awt.*; import javax.swing.*; class FullSizePanel { public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { // grid or box layout with 1 column and x rows LayoutManager lm = new GridLayout(0,1,5,5); // this is the panel we want to fill the entire content area // of the parent frame. JPanel redFullSizePanel = new JPanel(lm); // make it true to the first part of the attribute name redFullSizePanel.setBackground(Color.RED); for (int ii=1; ii<6; ii++) { redFullSizePanel.add(new JLabel("Label " + ii)); redFullSizePanel.add(new JButton("Button " + ii)); } JFrame f = new JFrame("Full Size Panel"); // 1) The default layout for a content pane is BorderLayout // 2) A component added to a BL with no layout constraint ends // up in the CENTER // 3) A component in the CENTER of a BL takes the full space // (that is not used up by components in the EAST, WEST, // NORTH & SOUTH. f.add( redFullSizePanel ); f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); f.pack(); f.setSize(400,400); f.setLocationByPlatform(true); f.setVisible(true); } }); } } 
0
source

You can try to get the maximum "overall" window size. Since your application is maximized, it should give the same result:

 GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment(); Dimension screenDimension = env.getMaximumWindowBounds().getSize(); 

Do not forget that your window also has the so-called inserts:

 Insets insets = frame.getInsets(); final int left = insets.left; final int right = insets.right; final int top = insets.top; final int bottom = insets.bottom; final int width = screenDimension.width - left - right; final int height = screenDimension.height - top - bottom; 
0
source

Source: https://habr.com/ru/post/1379388/


All Articles