java - Why is my JFrame window doing that?(check description) -
i'm programming cookie clicker game remake , when scale jframe window, white appears. disappears hover cursor on button(when refreshes) , need fix that, because same when launch game.
here's screenshot(unscaled | scaled): http://s3.postimg.org/xomifomhf/bandicam_19.png
this whole code of game:
package cookieclicker.tominocz; import java.awt.borderlayout; import java.awt.color; import java.awt.dimension; import java.awt.gridbagconstraints; import java.awt.gridbaglayout; import java.awt.gridlayout; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.io.bufferedreader; import java.io.bufferedwriter; import java.io.file; import java.io.filereader; import java.io.filewriter; import java.io.ioexception; import javax.swing.borderfactory; import javax.swing.icon; import javax.swing.imageicon; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jpanel; public class main { public static int num1; static icon icon1 = new imageicon("cookie.png"); static jbutton b1 = new jbutton(icon1); static jbutton b2 = new jbutton("you got " + num1 + " cookies!"); @suppresswarnings("serial") public static void main(string[] args) { file save = new file(".\\gamesave.cookieclicker"); if (save.exists()) { loadgame(); } jframe f = new jframe("cookie clicker beta v0.1"); b2.setbackground(color.cyan); b2.setpreferredsize(new dimension(10000, 14)); jpanel buttonpanel1 = new jpanel(new gridlayout(1, 1)); buttonpanel1.setbackground(color.dark_gray); b2.setenabled(false); b2.setborder(null); buttonpanel1.add(b2); jpanel buttonpanel2 = new jpanel(new gridlayout(1000, 1)); buttonpanel2.setbackground(color.dark_gray); buttonpanel2.setenabled(false); buttonpanel2.add(new jbutton("grandma")); buttonpanel2.add(new jbutton("")); buttonpanel2.add(new jbutton("")); jpanel east = new jpanel(new gridbaglayout()); jpanel north = new jpanel(new gridbaglayout()); gridbagconstraints gbc = new gridbagconstraints(); gbc.anchor = gridbagconstraints.north; gbc.weighty = 1; north.add(buttonpanel1, gbc); east.add(buttonpanel2, gbc); jpanel center = new jpanel() { @override public dimension getpreferredsize() { return new dimension(200, 200); } }; center.setborder(borderfactory.createlineborder(color.black)); f.add(east, borderlayout.east); f.add(north, borderlayout.north); f.add(center); f.pack(); f.setsize(600, 400); f.setvisible(true); f.setdefaultcloseoperation(jframe.exit_on_close); b1.setbackground(color.lightgray); b1.setborder(null); f.add(b1); b1.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { object source = e.getsource(); if (source instanceof jbutton) { addcookies(); } } }); } public static void addcookies() { savegame(); b2.settext("you got " + ++num1 + " cookies!"); if (num1 == 1) { b2.settext(" got " + 1 + " cookie! "); } else { b2.settext("you got " + num1 + " cookies!"); } system.out.println(num1); } public static void savegame() { try { bufferedwriter writer = new bufferedwriter(new filewriter( ".\\gamesave.cookieclicker")); writer.write(string.valueof(1 + num1)); writer.close(); } catch (ioexception e) { e.printstacktrace(); } } public static void loadgame() { try (bufferedreader br = new bufferedreader(new filereader( ".\\gamesave.cookieclicker"))) { string savedgame; while ((savedgame = br.readline()) != null) { num1 = integer.parseint(savedgame); b2.settext("you got " + num1 + " cookies!"); } } catch (ioexception e) { e.printstacktrace(); } } }
and other thing is, dark grey strip hiding 3 buttons. don't count 4th one, that's 1 showing ammount of cookies have :).
now problem be?
you have 2 components sharing centre
position of frame's borderlayout
, center
, b1
.
b1
, been last component added, getting attention of layout manager , been laid out when frame resized, center
not , remaining @ last size/position set (because called pack
, forced frame layout it's child components, added b1
after it).
borderlayout
can manage single component @ each of it's 5 pre-defined positions
make decision should in centre...
you should have read of should avoid use of set(preferred|maximum|minimum)size methods in java swing? , stop messing preferred size of components, let them make own decisions in combination appropriate layout managers
i'd encourage move content of main
method else (may class's constructor), way, fields won't need static
, solve ton of other issues might have in future
Comments
Post a Comment