While investigating some issue in my application, I just found out some weird thing.
Basically this SSCCE should demonstrate the problem:
public class MainFrame extends JFrame {
public MainFrame() {
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setExtendedState(JFrame.MAXIMIZED_BOTH);
pack();
}
}
public class Main {
public static void main(String[] args) {
MainFrame mf = new MainFrame();
mf.setVisible(true);
System.out.println(mf.getSize());
}
}
Somehow, on my 1280x1024 resolution monitor, this results in:
java.awt.Dimension[width=1296,height=1010]
Anyone knows how this is happening? Especially the fact that the width is higher than as what should happen.
Regards.
Setting a JFrame to fill half the screen For instance, if you want to create a JFrame that is half the width and half the height of the screen, you can just divide the dimensions in half, as shown in this code: Dimension screenSize = Toolkit. getDefaultToolkit(). getScreenSize(); aFrame.
Change window Size of a JFrame To resize a frame, There is a method JFrame. setSize(int width, int height) which takes two parameters width and height.
By default, we can minimize a JFrame by clicking on minimize button and maximize a JFrame by clicking on maximize button at the top-right position of the screen. We can also do programmatically by using setState(JFrame. ICONIFIED) to minimize a JFrame and setState(JFrame. MAXIMIZED_BOTH) to maximize a JFrame.
First a JFrame is created by invoking its constuctor. The argument to the constructor sets the title of the frame. The setSize(200,100) method makes the rectangular area 200 pixels wide by 100 pixels high. The default size of a frame is 0 by 0 pixels.
The window border is probably 8 pixels wide. When maximizing, Windows resizes the window so that the client area gets 1280 pixels wide. The whole width of the window is then 8+1280+8 pixels = 1296 pixels. The same happens to the height.
When using extended desktop on multiple monitors, one can sometimes notice that when a window is maximized on one screen, the window's border can be seen on adjacent screens.
Not sure what's happening there. If you're just trying to set your frame to it's maximum size based on available screen real estate, try this alternative:
import java.awt.*;
import javax.swing.*;
public class JFrameExtended
{
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
Rectangle maxBounds = env.getMaximumWindowBounds();
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setBounds(maxBounds);
f.setVisible(true);
System.out.println("Frame size: " + f.getSize());
}
});
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With