swing - Java JButton sizing issue -
people. started working on simple games advance in knowledge of swing. needed use jbuttons 1 of projects, did. here's problem...
when opened window, instead of seeing normal screen loaded images, whole screen last button added frame. tried using setsize()
function button still super large , filled screen.
code:
import javax.swing.jpanel; import javax.swing.borderfactory; import java.awt.color; import java.awt.dimension; import java.awt.graphics; import javax.swing.swingutilities; import javax.swing.jframe; import java.awt.image; import java.io.*; import java.net.url; import javax.imageio.*; import java.awt.image.*; import java.awt.font; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class engine { public static int chips = 5000; public static string c = chips+""; public static void main(string[] args) { swingutilities.invokelater(new runnable() { public void run() { gui(); } }); } private static void gui() { jframe f = new jframe("pinnacle baccarat"); f.setdefaultcloseoperation(jframe.exit_on_close); f.setpreferredsize(new dimension(768, 512)); f.add(new p()); engine e2 = new engine(); jbutton bet1 = new jbutton("bet 1 chip"); jbutton bet2 = new jbutton("bet 10 chips"); jbutton bet3 = new jbutton("bet 100 chips"); jbutton bet4 = new jbutton("bet 1k chips"); jbutton bet5 = new jbutton("bet 10k chips"); jbutton bet6 = new jbutton("bet 100k chips"); if(e2.chips < 1){bet1.setenabled(false);} if(e2.chips > 0){bet1.setenabled(true);} if(e2.chips < 10){bet2.setenabled(false);} if(e2.chips > 9){bet2.setenabled(true);} if(e2.chips < 100){bet3.setenabled(false);} if(e2.chips > 99){bet3.setenabled(true);} if(e2.chips < 1000){bet4.setenabled(false);} if(e2.chips > 999){bet4.setenabled(true);} if(e2.chips < 10000){bet5.setenabled(false);} if(e2.chips > 9999){bet5.setenabled(true);} if(e2.chips < 100000){bet6.setenabled(false);} if(e2.chips > 99999){bet6.setenabled(true);} bet1.setpreferredsize(new dimension(96, 32)); bet2.setpreferredsize(new dimension(96, 32)); bet3.setpreferredsize(new dimension(96, 32)); bet4.setpreferredsize(new dimension(96, 32)); bet5.setpreferredsize(new dimension(96, 32)); bet6.setpreferredsize(new dimension(96, 32)); f.add(bet1); f.add(bet2); f.add(bet3); f.add(bet4); f.add(bet5); f.add(bet6); f.pack(); f.setvisible(true); } } class p extends jpanel { public p() { } public void paintcomponent(graphics g) { engine eng = new engine(); super.paintcomponent(g); bufferedimage chip = null; bufferedimage table = null; try { table = imageio.read(new file("bg.png")); } catch (ioexception e) { } try { chip = imageio.read(new file("chip.png")); } catch (ioexception e) { } g.drawimage(table, 0, 0, null); g.drawimage(chip, 4, 446, null); g.setcolor(new color(255, 0, 0)); g.setfont(new font("default", font.bold, 20)); g.drawstring(eng.c, 36, 467); } }
can help? edit: need know how place buttons in places. edit 2: after using f.setlayout(null)
, buttons work paint method , pictures broken: don't show. can help?
its because have not specified layout manager add this:
f.setlayout(new flowlayout());
also learn how set positioning of stuff read layout managers or use setlayout(null)
panel , use setbounds()
method set x , y position of element.
Comments
Post a Comment