Java Swing: Resizing component in JFrame without affecting other components -
i have simple game i'm creating novice project. it's based on online card game called "castlewars". there 2 players, each tower affected cards , opponent play. @ moment have basic framework of collections , classes need make game operate @ simple level, i'm having problems displaying these effects user. have following code, should update, amongst other things, 2 jlabels on jframe gui (constructed in netbeans 7.4) represent player's towers:
private void adjustscreen(){ system.out.println (integer.tostring(jlabel1.getsize().height)); jlabel1.setsize(100, (playerred.gettower().currentheight() * 2)); system.out.println(integer.tostring(playerred.gettower().currentheight() * 2)); system.out.println (integer.tostring(jlabel1.getsize().height)); jlabel2.setsize(100, (playerblue.gettower().currentheight() * 2)); jlabel5.settext(integer.tostring(playerblue.gettower().currentheight())); jlabel6.settext(integer.tostring(playerblue.getgold())); jlabel9.settext(integer.tostring(playerred.gettower().currentheight())); jlabel10.settext(integer.tostring(playerred.getgold())); if (turnblue){ jpanel21.setbackground(inplay); jpanel10.setbackground(outplay); }else{ jpanel10.setbackground(inplay); jpanel21.setbackground(outplay); } }
when run it, following output:
- (initial label height) - 200
- (adjusted tower().height) - 100
- (adjusted label height) - 100
which suggests i'm trying working on level, actual labels visually stay same size.
at start (the initiation of game should have set labels half visible size): after couple of plays - blue's new tower height shown in top left
i've played around enabling , disabling resize property, both on label , on frame, , did once manage resize, shifted other components of frame in unfortunate way. can see, "cards" @ bottom seem resize themselves, although not explicitly instructing them (maybe effect of text length in "cards"?) doing wrong?
after re-reading question realized answer not answer question more of list of suggestions. rather deleting it, i'll leave in case can gain :-)
"what doing wrong?"
welcome world of "why should use layout managers"
there's ocean of problems may arise null layouts , trying set size , location everything. swing made used layout managers.
some layout managers respect preferred sizes , wont.
use correct layout manager , make use of nested
jpanel
different layout managers desired resultmake use of
emptyborder
,vgap
,hgap
empty space.don't set size or location anything. let layout managers take care of you.
go on laying out components withing container learn different layout managers.
edit
if you're using netbeans gui builder take @ this answer how use different layout managers using design tool.
Comments
Post a Comment