java - Window Size is smaller than it should be -
alright i've got jframe screen on it. i've set size 800 800. window created smaller that. it's not problem taskbar because it's not fullsize.
package sharph; import java.awt.dimension; import javax.swing.jframe; public class main extends jframe { public static string title = "game 1"; public static dimension screensize = new dimension(800,800); public static void main(string[] args) { jframe frame = new jframe(); frame.settitle(title); frame.setsize(screensize); frame.setlocationrelativeto(null); frame.setresizable(true); frame.setdefaultcloseoperation(exit_on_close); screen screen = new screen(); screen.setsize(screensize); frame.add(screen); frame.setvisible(true); } }
in screen class paint method draws box around border should be:
//draw border g.setcolor(color.red); g.drawrect(1, 1, 799, 799);
when run it, window smaller box , bottom , right sides cut off.
note second picture manually re-sized show border difference.
i realize have drawn box 1 pixel smaller on each side, difference more 2 pixels.
this happens because content needs squeezed size of frame minus borders.
checkout this question , this question more detailed explanation
the layout manager overriding size property set on screen
component. in either case, should overriding getpreferredsize
method of screen
class
also, shouldn't relying on magic numbers or assumptions actual size of component, should, instead, using getwidth
, getheight
instead (i know, it's demonstration purposes)
Comments
Post a Comment