java - Why is the graphic not working when the button number 8 is pressed? -
i have code , works until bottom middle button pressed.
import java.awt.*; import javax.swing.jframe; import javax.swing.jpanel; import java.awt.borderlayout; import java.awt.gridlayout; import javax.swing.jbutton; import javax.swing.jlabel; import java.awt.event.actionevent; import java.awt.event.actionlistener; public class memory extends jframe { /** * */ private static final long serialversionuid = 1l; public void paintcomponent(graphics g) { super.paintcomponents(g); g.setcolor(new color(156, 93, 82)); g.fill3drect(21, 3, 7, 12, true); g.setcolor(new color(156, 23, 134)); g.filloval(1, 15, 15, 15); g.filloval(16, 15, 15, 15); g.filloval(31, 15, 15, 15); g.filloval(7, 31, 15, 15); g.filloval(22, 31, 15, 15); g.filloval(16, 47, 15, 15); } public memory() { gridlayout h = new gridlayout(3, 3); final jframe frame = new jframe(); final jpanel pan = new jpanel(h); frame.add(pan); //pan=new jpanel(h); pan.setbackground(new color(130, 224, 190)); setfont(new font("serif", font.bold, 28)); jbutton button1 = new jbutton(); pan.add(button1); final jlabel label1 = new jlabel("hi"); label1.setvisible(false); pan.add(label1); jbutton button2 = new jbutton(); pan.add(button2); final jlabel label2 = new jlabel("hi"); label2.setvisible(false); pan.add(label2); jbutton button3 = new jbutton(); pan.add(button3); final jlabel label3 = new jlabel("hi"); label3.setvisible(false); pan.add(label3); jbutton button4 = new jbutton(); pan.add(button4); final jlabel label4 = new jlabel("hi"); label4.setvisible(false); pan.add(label4); jbutton button5 = new jbutton(); pan.add(button5); final jlabel label5 = new jlabel("hi"); label5.setvisible(false); pan.add(label5); jbutton button6 = new jbutton(); pan.add(button6); final jlabel label6 = new jlabel("hi"); label6.setvisible(false); pan.add(label6); jbutton button7 = new jbutton(); pan.add(button7); final jlabel label7 = new jlabel("hi"); label7.setvisible(false); pan.add(label7); jbutton button8 = new jbutton(); pan.add(button8); final jlabel label8 = new jlabel("hi"); label8.setvisible(false); pan.add(label8); jbutton button9 = new jbutton(); pan.add(button9); final jbutton button10 = new jbutton("exit"); pan.add(button10); setdefaultcloseoperation(exit_on_close); settitle("memory game"); setlayout(new borderlayout()); add(pan, borderlayout.center); add(button10, borderlayout.south); setsize(600, 600); setvisible(true); final jlabel label9 = new jlabel("hi"); label9.setvisible(false); pan.add(label9); button1.addactionlistener(new actionlistener() { public void actionperformed(actionevent arg0) { label1.setvisible(true); } }); button2.addactionlistener(new actionlistener() { public void actionperformed(actionevent arg0) { label2.setvisible(true); } }); button3.addactionlistener(new actionlistener() { public void actionperformed(actionevent arg0) { label3.setvisible(true); } }); button4.addactionlistener(new actionlistener() { public void actionperformed(actionevent arg0) { label4.setvisible(true); } }); button5.addactionlistener(new actionlistener() { public void actionperformed(actionevent arg0) { label5.setvisible(true); } }); button6.addactionlistener(new actionlistener() { public void actionperformed(actionevent arg0) { label6.setvisible(true); } }); button7.addactionlistener(new actionlistener() { public void actionperformed(actionevent arg0) { label7.setvisible(true); } }); //this thought going if pressed. button8.addactionlistener(new actionlistener() { public void actionperformed(actionevent arg0) { label8.setvisible(true); frame.getcontentpane().add(new memory()); setvisible(true); } } ); button9.addactionlistener(new actionlistener() { public void actionperformed(actionevent arg0) { label9.setvisible(true); } } ); button10.addactionlistener(new actionlistener() { public void actionperformed(actionevent arg0) { if (button10.getsize() != null) { system.exit(0); } } }); } public static void main(string args[]) { new memory(); } }
memory
extends jframe
. swing won't let add window window.
you should avoid extending top level containers (this reason) , instead use jpanel
.
this allows add component whatever container like
jframe
not have paintcomponent
method, never called. reason compiles you've called super.paintcomponents
(note s
@ end).
instead, should, extend jpanel
, override it's paintcomponent
method , perform custom painting there
Comments
Post a Comment