swing - Multiple screen java applet has buttons that don't work -
i'm making program has 2 screens, title , game screen. if user clicks "play", can proceed next screen. however, on next screen if user tries press button "keep going", nothing happens. want know why , if can fix it. thanks, , here code:
import java.applet.*; import java.awt.*; import javax.swing.*; import java.awt.event.*; public class zombiedice extends applet implements actionlistener { panel p_card; panel card1, card2; cardlayout cdlayout = new cardlayout (); int shotguns = 0; int brains = 0; int p1b = 0; int p2b = 0; jlabel pic1; jlabel pic2; jlabel pic3; public void init () { p_card = new panel (); p_card.setlayout (cdlayout); screen1 (); screen2 (); resize (600, 500); setlayout (new borderlayout ()); add ("center", p_card); } public void screen1 () { card1 = new panel (); jlabel logo = new jlabel (createimageicon ("logo.jpg")); jbutton play = new jbutton (createimageicon ("play.jpg")); play.setactioncommand ("2"); play.addactionlistener (this); jbutton quit = new jbutton (createimageicon ("quit.jpg")); quit.setactioncommand ("quit"); quit.addactionlistener (this); card1.add (logo); card1.add (play); card1.add (quit); setbackground (color.red); p_card.add ("1", card1); } public void screen2 () { card2 = new panel (); jlabel player = new jlabel ("player 1 "); player.setfont (new font ("vinque", font.bold, 20)); jlabel shotgun = new jlabel ("shotguns: " + shotguns + " "); shotgun.setfont (new font ("vinque", font.bold, 14)); jlabel brain = new jlabel ("braaainss: " + brains); brain.setfont (new font ("vinque", font.bold, 14)); jlabel p1brains = new jlabel ("player 1 ate: " + p1b + " braaiinnss "); p1brains.setfont (new font ("vinque", font.bold, 16)); jlabel p2brains = new jlabel ("player 2 ate: " + p2b + " braaiinnss"); p2brains.setfont (new font ("vinque", font.bold, 16)); pic1 = new jlabel (createimageicon ("zombie.jpg")); pic2 = new jlabel (createimageicon ("zombie.jpg")); pic3 = new jlabel (createimageicon ("zombie.jpg")); jbutton keepgoing = new jbutton (createimageicon ("keepgoing.jpg")); keepgoing.addactionlistener (this); keepgoing.setactioncommand ("kg"); jbutton stopscore = new jbutton (createimageicon ("stopandscore.jpg")); keepgoing.addactionlistener (this); keepgoing.setactioncommand ("ss"); jbutton nextplayer = new jbutton (createimageicon ("nextplayer.jpg")); nextplayer.addactionlistener (this); nextplayer.setactioncommand ("np"); card2.add (player); card2.add (shotgun); card2.add (brain); card2.add (p1brains); card2.add (p2brains); card2.add (pic1); card2.add (pic2); card2.add (pic3); card2.add (keepgoing); card2.add (stopscore); card2.add (nextplayer); p_card.add ("2", card2); } public void actionperformed (actionevent e) { if (e.getactioncommand ().equals ("2")) cdlayout.show (p_card, "2"); if (e.getactioncommand ().equals ("kg")) { int n = (int) ((math.random () * 9) + 1); { if (n == 1) pic1.seticon (createimageicon ("greenbrain.jpg")); else if (n == 2) pic1.seticon (createimageicon ("greenfootprints.jpg")); else if (n == 3) pic1.seticon (createimageicon ("greenshotgun.jpg")); else if (n == 4) pic1.seticon (createimageicon ("redbrain.jpg")); else if (n == 5) pic1.seticon (createimageicon ("redfootprints.jpg")); else if (n == 6) pic1.seticon (createimageicon ("redshotgun.jpg")); else if (n == 7) pic1.seticon (createimageicon ("yellowbrain.jpg")); else if (n == 8) pic1.seticon (createimageicon ("yellowfootprints.jpg")); else pic1.seticon (createimageicon ("yellowshotgun.jpg")); } int n2 = (int) ((math.random () * 9) + 1); { if (n2 == 1) pic2.seticon (createimageicon ("greenbrain.jpg")); else if (n2 == 2) pic2.seticon (createimageicon ("greenfootprints.jpg")); else if (n2 == 3) pic2.seticon (createimageicon ("greenshotgun.jpg")); else if (n2 == 4) pic2.seticon (createimageicon ("redbrain.jpg")); else if (n2 == 5) pic2.seticon (createimageicon ("redfootprints.jpg")); else if (n2 == 6) pic2.seticon (createimageicon ("redshotgun.jpg")); else if (n2 == 7) pic2.seticon (createimageicon ("yellowbrain.jpg")); else if (n2 == 8) pic2.seticon (createimageicon ("yellowfootprints.jpg")); else pic2.seticon (createimageicon ("yellowshotgun.jpg")); } int n3 = (int) ((math.random () * 9) + 1); if (n3 == 1) pic3.seticon (createimageicon ("greenbrain.jpg")); else if (n3 == 2) pic3.seticon (createimageicon ("greenfootprints.jpg")); else if (n3 == 3) pic3.seticon (createimageicon ("greenshotgun.jpg")); else if (n3 == 4) pic3.seticon (createimageicon ("redbrain.jpg")); else if (n3 == 5) pic3.seticon (createimageicon ("redfootprints.jpg")); else if (n3 == 6) pic3.seticon (createimageicon ("redshotgun.jpg")); else if (n3 == 7) pic3.seticon (createimageicon ("yellowbrain.jpg")); else if (n3 == 8) pic3.seticon (createimageicon ("yellowfootprints.jpg")); else pic3.seticon (createimageicon ("yellowshotgun.jpg")); } } protected static imageicon createimageicon (string path) { java.net.url imgurl = zombiedice.class.getresource (path); if (imgurl != null) { return new imageicon (imgurl); } else { system.err.println ("couldn't find file: " + path); return null; } } }
maybe should have closer @ these 2 blocks...
jbutton keepgoing = new jbutton("kg", createimageicon("keepgoing.jpg")); keepgoing.addactionlistener(this); keepgoing.setactioncommand("kg"); jbutton stopscore = new jbutton("ss", createimageicon("stopandscore.jpg")); keepgoing.addactionlistener(this); keepgoing.setactioncommand("ss");
see funny?
- with regards
jcomponent#add(string, component)
- "this method obsolete of 1.1. please use method add(component, object) instead." - java plugin support deprecated , moving plugin-free web
- why cs teachers should stop teaching java applets
Comments
Post a Comment