Multiple screen java applet has buttons that don't work

扶醉桌前 提交于 2019-12-23 05:23:09

问题


I'm making a program that has 2 screens, a title and a game screen. If the user clicks "Play", they can proceed to the next screen. However, on the next screen if the user tries to press the button "Keep Going", nothing happens. I want to know why this is and if anyone can fix it. Thanks, and here is my 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;
    }
}
}

回答1:


Maybe you should have a closer look at these two 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 anything funny?

  • With regards to JComponent#add(String, Component) - "This method is obsolete as of 1.1. Please use the method add(Component, Object) instead."
  • Java Plugin support deprecated and Moving to a Plugin-Free Web
  • Why CS teachers should stop teaching Java applets


来源:https://stackoverflow.com/questions/36830099/multiple-screen-java-applet-has-buttons-that-dont-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!