java - Background Images With JFrame -
this 1 section of yahtzee game i'm working with. trying set background yahtzee.png file in project folder. commented out attempt so, because it's not working out me. there better way set up?
exframe(int numplayers) { frame = new jframe(); frame.setsize(450+150*numplayers,700); frame.settitle("yahtzee!"); frame.setdefaultcloseoperation(jframe.exit_on_close); this.numplayers = numplayers; this.numgridrows = 20; this.buttonwidth = 140; this.numcreatebutlabcalls = 0; this.component = new dicecomponent(buttonwidth*2); this.cbuttons = new jbutton[numgridrows]; this.cbuttonstext = new string[numgridrows]; this.clabels = new jlabel[numplayers][numgridrows]; this.statuslabel = new jlabel("<html>new game has been started!<br>please select dice wish hold or click on scoring button</html>"); this.score = new yahtzeescore[numplayers]; //populate score array for(int k = 0; k < numplayers; k++) { score[k] = new yahtzeescore(cbuttons,clabels, statuslabel, component.getdiearray(), cbuttonstext, numgridrows, k); } statuslabel.setpreferredsize(new dimension(buttonwidth*2, 100)); centerpanel = new jpanel(new gridlayout(numgridrows,numplayers+1)); //columns based on numplayers component.rolldice(true); popcenterpanel(); for(int k = 0; k < numplayers; k++) score[k].reset(); addlisteners(); frame.setlayout(null); frame.add(component); frame.add(statuslabel); frame.add(centerpanel); // frame.add(new jlabel(new imageicon("/yahtzeeagain/yahtzee.png"))); insets insets = frame.getinsets(); dimension size = statuslabel.getpreferredsize(); statuslabel.setbounds(100+ insets.left,insets.top,size.width,size.height); size = component.getpreferredsize(); component.setbounds(insets.left, 150 + insets.top, size.width, size.height); size = centerpanel.getpreferredsize(); centerpanel.setbounds(290 + insets.left, 140 + insets.top, size.width, size.height); centerpanel.setbackground(color.gray); frame.add(this); frame.setvisible(true); }
best way:
add background jlabel frame's contentpane (as doing), setlayout(null) on it, , add other components background jlabel
Comments
Post a Comment