gpt4 book ai didi

java - 多屏幕java小程序的按钮不起作用

转载 作者:行者123 更新时间:2023-12-01 21:37:11 25 4
gpt4 key购买 nike

我正在制作一个有 2 个屏幕的程序,一个标题和一个游戏屏幕。如果用户单击“播放”,他们可以进入下一个屏幕。但是,在下一个屏幕上,如果用户尝试按“继续”按钮,则不会发生任何情况。我想知道这是为什么以及是否有人可以解决它。谢谢,这是我的代码:

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

最佳答案

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

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");

看到什么有趣的事情了吗?

关于java - 多屏幕java小程序的按钮不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36830099/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com