swing - Java setBounds() method (JFrame) -
hi im using setbounds method open window whith size. size pass in argument size of window including bars of frame. how can set dimensions content?
there 2 ways fix this:
- take account border of frame when setting bounds of frame
- override
getpreferredsize()
of content pane ofjframe
, callpack()
on frame.
here demo of 2 techniques:
import java.awt.dimension; import java.awt.insets; import java.awt.rectangle; import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.swingutilities; public class test { protected void initui() { jframe frame = new jframe("insets technique"); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setvisible(true); insets = frame.getinsets(); system.out.println(i); rectangle bounds = new rectangle(50, 100, 400, 500); bounds.width += i.right + i.left; bounds.height += i.bottom + i.top; frame.setbounds(bounds); } protected void initui2() { jframe frame = new jframe("preferred size technique"); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setcontentpane(new jpanel() { @override public dimension getpreferredsize() { return new dimension(400, 500); } }); frame.pack(); frame.setlocation(50, 100); frame.setvisible(true); } public static void main(string[] args) { swingutilities.invokelater(new runnable() { @override public void run() { test test = new test(); test.initui(); test.initui2(); } }); } }
Comments
Post a Comment