Java 第二次尝试时,玩家攻击两次
因此,当我第一次去战斗时,它会按照它应该做的那样做,但之后当我再次去战斗时,它会在一次点击中攻击老板两次,然后当我杀死他并攻击他三次,然后它会攻击老板三次代码如下:Java 第二次尝试时,玩家攻击两次,java,multithreading,swing,Java,Multithreading,Swing,因此,当我第一次去战斗时,它会按照它应该做的那样做,但之后当我再次去战斗时,它会在一次点击中攻击老板两次,然后当我杀死他并攻击他三次,然后它会攻击老板三次代码如下: public class temp extends JFrame implements Runnable{ public static JPanel game=new JPanel(); public JPanel town_map=new JPanel(null); public JPanel level1=new JPanel
public class temp extends JFrame implements Runnable{
public static JPanel game=new JPanel();
public JPanel town_map=new JPanel(null);
public JPanel level1=new JPanel(null);
public JLabel character;
public Thread turn;
public JButton attack1=new JButton("reduce health");
public int health,healthleft;
public JLabel healthleftbl=new JLabel();
public boolean a=false;
CardLayout page= new CardLayout();
public static void main(String []args)
{
new temp().setVisible(true);
}
public temp()
{
super("Temporary Debug");
setSize(640,510);
setDefaultCloseOperation(EXIT_ON_CLOSE);
game.setLayout(page);
game.add(town_map,"map");
game.add(level1, "lhealthleft1");
add(game);
town();
}
public void town()
{
page.show(game, "map");
JButton test=new JButton("Go to Battle");
test.setBounds(0, 0, 150, 150);
town_map.add(test);
test.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
level01();
}
});
}
public void level01()
{
page.show(game, "lhealthleft1");
attack1.setBounds(315, 368, 163, 57);
health=800;
healthleft=health;
level1.add(attack1);
healthleftbl.setBounds(150, 0, 100, 100);
level1.add(healthleftbl);
if(a==true)
System.out.println(turn.getState());
turn=new Thread(this);
turn.start();
}
public void run() {
a=true;
attack1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
healthleft=healthleft-100;
}});
do{
healthleftbl.setText(healthleft+"/"+health);
if(healthleft<=0)
{
page.show(game, "map");
break;
}
System.out.println(healthleft);
}while(turn.getState()!=null);
}
}
public类temp扩展JFrame实现Runnable{
公共静态JPanel游戏=新JPanel();
公共JPanel town_map=新JPanel(空);
公共JPanel level1=新JPanel(空);
公共JLabel字符;
公共线程转向;
public JButton attack1=新JButton(“减少健康”);
公共卫生,健康左;
public JLabel healthlefbl=新JLabel();
公共布尔值a=false;
CardLayout页面=新的CardLayout();
公共静态void main(字符串[]args)
{
新温度().setVisible(真);
}
公共临时工()
{
超级(“临时调试”);
设置大小(640510);
setDefaultCloseOperation(关闭时退出);
游戏设置布局(第页);
游戏。添加(城镇地图,“地图”);
游戏添加(1级,“lhealthleft1”);
添加(游戏);
城镇();
}
公共空镇()
{
页面显示(游戏,“地图”);
JButton test=新JButton(“投入战斗”);
测试立根(0,0,150,150);
城镇地图添加(测试);
test.addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件e)
{
level01();
}
});
}
公共空间级别01()
{
页面显示(游戏“lhealthleft1”);
攻击1.挫折(31536816357);
健康=800;
健康左=健康;
级别1.添加(附件1);
healthleftbl.setBounds(150,0,100,100);
级别1.添加(healthleftbl);
如果(a==true)
System.out.println(turn.getState());
转动=新螺纹(此);
turn.start();
}
公开募捐{
a=真;
attack1.addActionListener(新ActionListener()
{
已执行的公共无效操作(操作事件e)
{
healthleft=healthleft-100;
}});
做{
healthlefbl.setText(healthleft+“/”+health);
如果(healthleft您在代码中添加了ActionListeners,并且代码本身会重复,这会被多次调用,这会导致在按下按钮时多次调用操作
public void run() {
a = true;
// **************************************
// this add ActionListener gets called with every creation
// of a new Thread, and then calling .start() on the Thread
attack1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
healthleft = healthleft - 100;
}
});
// **************************************
// this code does not belong in an event-driven program
do {
healthleftbl.setText(healthleft + "/" + health);
if (healthleft <= 0) {
page.show(game, "map");
break;
}
System.out.println(healthleft);
} while (turn.getState() != null);
}
您正在代码中添加ActionListeners,该代码会重复多次调用,这会导致在按下按钮时多次调用操作
public void run() {
a = true;
// **************************************
// this add ActionListener gets called with every creation
// of a new Thread, and then calling .start() on the Thread
attack1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
healthleft = healthleft - 100;
}
});
// **************************************
// this code does not belong in an event-driven program
do {
healthleftbl.setText(healthleft + "/" + health);
if (healthleft <= 0) {
page.show(game, "map");
break;
}
System.out.println(healthleft);
} while (turn.getState() != null);
}
请参阅,了解我不再费心解决的问题。请参阅编辑以回答问题。我制作了一个新程序,显示了事件驱动、状态驱动代码的示例。找不到while true循环。我确实看到了这一点,我非常感谢您所做的一切。我欠您一个感谢!请参阅,了解我不再费心解决的问题ng.查看编辑以回答问题我制作了一个新程序,显示了事件驱动、状态驱动代码的示例。找不到while true循环。我确实看到了这一点,我非常感谢你所做的一切我欠你一个非常感谢!
@SuppressWarnings("serial")
class GamePanel extends JPanel {
private static final int PREF_W = 400;
private static final int PREF_H = 350;
public static final int DAMAGE_AMOUNT = 100;
private Temp2 temp2;
private Player2 player2 = new Player2();
private JLabel statusLabel = new JLabel();
public GamePanel(Temp2 temp2) {
this.temp2 = temp2;
player2.addPropertyChangeListener(Player2.HEALTH, new PlayerListener());
JPanel statusPanel = new JPanel(new FlowLayout(FlowLayout.LEADING, 5, 5));
statusPanel.add(new JLabel("Health"));
statusPanel.add(statusLabel);
displayHealth();
JPanel battlePanel = new JPanel(new GridBagLayout());
battlePanel.add(new JButton(new BattleAction("Battle", KeyEvent.VK_B)));
setLayout(new BorderLayout());
add(statusPanel, BorderLayout.PAGE_START);
add(battlePanel, BorderLayout.CENTER);
}
private void displayHealth() {
String healthText = String.format("%03d/%03d", player2.getHealth(), Player2.MAX_HEALTH);
statusLabel.setText(healthText);
}
public void reset() {
player2.setHealth(Player2.MAX_HEALTH);
temp2.showCard(IntroPanel.class.getName());
}
@Override
public Dimension getPreferredSize() {
if (isPreferredSizeSet()) {
return super.getPreferredSize();
}
return new Dimension(PREF_W, PREF_H);
}
private class BattleAction extends AbstractAction {
public BattleAction(String name, int mnemonic) {
super(name);
putValue(MNEMONIC_KEY, mnemonic);
}
@Override
public void actionPerformed(ActionEvent e) {
int health = player2.getHealth() - DAMAGE_AMOUNT;
player2.setHealth(health);
}
}
private class PlayerListener implements PropertyChangeListener {
@Override
public void propertyChange(PropertyChangeEvent evt) {
int health = (int) evt.getNewValue();
displayHealth();
if (health <= 0) {
reset();
}
}
}
}
@SuppressWarnings("serial")
class IntroPanel extends JPanel {
private Temp2 temp2;
public IntroPanel(Temp2 temp2) {
this.temp2 = temp2;
setLayout(new GridBagLayout());
add(new JButton(new GoToBattleAction("Go To Battle", KeyEvent.VK_G)));
}
private class GoToBattleAction extends AbstractAction {
public GoToBattleAction(String name, int mnemonic) {
super(name);
putValue(MNEMONIC_KEY, mnemonic);
}
@Override
public void actionPerformed(ActionEvent e) {
temp2.showCard(GamePanel.class.getName());
}
}
}
class Player2 {
// make health a "bound property" by using property change support and listeners
public static final String HEALTH = "health";
public static final int MAX_HEALTH = 800;
private SwingPropertyChangeSupport pcSupport = new SwingPropertyChangeSupport(this);
private int health;
public Player2() {
this.health = MAX_HEALTH;
}
public int getHealth() {
return health;
}
// notify all listeners if health changes
public void setHealth(int health) {
int oldValue = this.health;
int newValue = health;
this.health = health;
pcSupport.firePropertyChange(HEALTH, oldValue, newValue);
}
// allow outside code to listen for changes to all bound properties
// should also have the associated remove listener methods too
public void addPropertyChangeListener(PropertyChangeListener listener) {
pcSupport.addPropertyChangeListener(listener);
}
public void addPropertyChangeListener(String name, PropertyChangeListener listener) {
pcSupport.addPropertyChangeListener(name, listener);
}
}