Featured post
java - Why does setBounds() method not work even after setting setLayout() as null? -
usually setbounds() method positions called object in specified location , in specified size..right? created jframe class , demonstrated working on button setting setlayout(null). created class extends jpanel , put components in it. bounds these components set inside method. object of class included in class extends jframe. object calls setbound() method. result not show components in jpanel extended class. cde fragment given below.
class administrator extends jframe implements actionlistener { public administrator() { settitle("administration"); setsize(500,500); setdefaultcloseoperation(exit_on_close); setlayout(null); jbutton newbutton=new jbutton(" new.. "); newbutton.addactionlistener(this); add(newbutton); newbutton.setbounds(100,100,100,25); setvisible(true); public static void main(string arg[]) { new administrator(); } public void actionperformed(actionevent ae) { string act=(string)ae.getactioncommand(); if(act.equals("new registry..")||act.equals(" new.. ")) { regpanel rgpnl=new regpanel(); dimension sz; sz=rgpnl.getpreferredsize(); rgpnl.setbounds(800,800,sz.width,sz.height); rgpnl.arrangecomponents(); add(rgpnl); repaint(); setvisible(true); } } class regpanel extends jpanel { label namelab; jtextfield name; jbutton do_register; public regpanel() { //container = (); setlayout(null); namelab=new label("name :"); name=new jtextfield(20); add(namelab); add(name); add(do_register); } public void arrangecomponents() { dimension size; size=namelab.getpreferredsize(); namelab.setbounds(20,10,size.width,size.height); name.getpreferredsize(); name.setbounds(150,10,size.width,size.height); do_register.setbounds(10,10,size.width,size.height); repaint(); } }
apart questionable parts of code -- missing curly brackets, null pointers , such errors -- code creates components @ (800,800) while interface 500x500 px in size. put components out-of-view. put components @ smaller offset window origin , show. (i tested this, along modifications code, order in calls made respect setlayout(null)
. call should seemingly occur @ right time, otherwise components wouldn't show me.)
- Get link
- X
- Other Apps
Comments
Post a Comment