Java JFrame Size according to screen resolution
I created java GUI using myEclipse Matisse. when my Screen Resolution is 1024x768 it works fine but when i change resolution my GUI is not working fine. I want my GUI window should be re-sized according to the screen Resolution I am extending JFrame to create the main window.
public class MyClass extends JFrame {
//I am putting some controls here.
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
setBounds(0,0,sc开发者_Go百科reenSize.width, screenSize.height);
setVisible(true);
pack();
}
this is not working, what ever i do, setting size hardcoded or by ToolKit using, the Frame Size Remains same.
You can try using this to maximize the frame:
this.setExtendedState(JFrame.MAXIMIZED_BOTH);
You are calling pack()
which changes the frame size so it just fits the components inside. That's why it is shrinking back I think. Remove the pack()
line and it should work.
Another way to do this is:
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
pack();
setSize(screenSize.width,screenSize.height);
Calling pack()
is vital to a correctly functioning GUI. Call it after all the components have been added, to have it validate the container and set it to it's natural size.
Then call setSize()
& related methods like setBounds()
afterwards.
Try this... Select all components of JFrame, right click, select 'Auto Resizing', check for both Horizontal and Vertical, close .
Calling pack() will usually result in the window being resized to fit the contents' preferred size. Try removing the call to pack() and see what happens.
First, setbounds for frame then remove pack() method.
精彩评论