java - getPreferredSize() works on JFrame; why doesn't it work on Frame? -
i hope problem confuses me.
i tested getpreferredsize()
on frame
, on frame
. in frame
, getpreferredsize()
returns 0 (both width , height), while in frame
, returns non-zero numbers, working.
i not sure part of coding in nolayout()
method such getpreferredsize()
returns 0, , ispreferredsizeset()
returns false
, implies preferred size not set.
why not set? can help? thanks.
below codings of simple test program.
import java.awt.*; import javax.swing.*; public class test { public test(){ jframe frame = new jframe("absolutelayoutdemo"); frame.setdefaultcloseoperation(jframe.exit_on_close); container p = frame.getcontentpane(); p.setlayout(null); jbutton b1 = new jbutton("one"); p.add(b1); dimension size = b1.getpreferredsize(); system.out.println("size width "+size.width+" height "+size.height); jlabel label2 = new jlabel("text-only label"); p.add(label2); size = label2.getpreferredsize(); system.out.println("label size width "+size.width+" height "+size.height); insets insets = p.getinsets(); frame.setsize(300 + insets.left + insets.right, 125 + insets.top + insets.bottom); frame.setvisible(true); } public static void main(string[] args) { test t = new test(); nolayout(); }enter code here public static void nolayout() { frame f = new frame(); f.setlayout(null); insets = f.getinsets(); system.out.println("its left, "+its.left+" right, "+its.right+" top "+its.top+" bottom "+its.bottom); f.setlocation(200, 100 ); f.setsize(its.left+its.right+300,its.top+its.bottom+200); label l1 = new label("enter integer added : ",label.right); textfield t1 = new textfield("0",10); f.add(l1); f.add(t1); system.out.println("is l1 preferred size set "+l1.ispreferredsizeset()); dimension sz = l1.getpreferredsize(); l1.setbounds(its.left + 10, its.top + 10, sz.width, sz.height); system.out.println("label 1 width "+sz.width+" height " +sz.height); sz = t1.getpreferredsize(); t1.setbounds(its.left + 100, its.top + 10, sz.width, sz.height); system.out.println("text 1 width "+sz.width+" height " +sz.height); f.setvisible(true); } }
the output on command window follows:
c:\javapr>java test size width 55 height 26 label size width 88 height 16 left, 0 right, 0 top 0 bottom 0 l1 preferred size set false label 1 width 0 height 0 text 1 width 0 height 0 enter code here
the result of getpreferredsize()
reliable, once specify layout , pack()
parent window
. suggested here, may want override getpreferredsize()
.
import java.awt.dimension; import java.awt.frame; import java.awt.gridbaglayout; import java.awt.label; import java.awt.panel; import java.awt.textfield; import java.awt.event.windowadapter; import java.awt.event.windowevent; /** @see https://stackoverflow.com/posts/37501391/edit */ public class test { private void display() { frame f = new frame(){ @override public dimension getpreferredsize() { return new dimension(320, 240); } }; f.addwindowlistener(new windowadapter() { @override public void windowclosing(windowevent e) { system.exit(0); } }); f.setlayout(new gridbaglayout()); panel p = new panel(); p.add(new label("test", label.right)); p.add(new textfield("test", 10)); f.add(p); f.pack(); f.setlocationrelativeto(null); f.setvisible(true); } public static void main(string[] args) { new test().display(); } }
Comments
Post a Comment