本文介绍了多屏 Java 小程序的按钮不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个有 2 个屏幕、一个标题和一个游戏屏幕的程序.如果用户点击播放",他们可以进入下一个屏幕.但是,在下一个屏幕上,如果用户尝试按下继续前进"按钮,则什么也不会发生.我想知道这是为什么,如果有人可以解决它.谢谢,这是我的代码:

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;
    }
}
}

推荐答案

也许你应该仔细看看这两个块...

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

这篇关于多屏 Java 小程序的按钮不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 02:49