Java ActionListener不起作用

时间:2011-10-10 03:47:45

标签: java swing actionlistener

这个游戏应该通过在ActionListener中使用布尔变量来在玩家1和2之间切换命令,但它现在不能正常工作。出于某种原因,我们可以点击播放器1的所有3个按钮。 if语句由于某种原因不起作用。有什么想法在这里发生了什么?谢谢你的帮助。

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.*;
import java.awt.*;


public class Foresight extends JFrame
{

    public static void main(String[] args)
    {

        //implement left panel for player 1 controls
        JPanel left = new JPanel();
        left.setSize(100,300);
        left.setLayout(new GridLayout(4,1));
        JLabel leftlabel = new JLabel("Player 1 Controls");
        leftlabel.setOpaque(true);
        JButton leftyellow = new JButton("8 Yellow");
        leftyellow.addActionListener(new GameLogic(leftyellow, 20));
        JButton leftred = new JButton("4 Red");
        leftred.addActionListener(new GameLogic(leftred, 21));
        JButton leftblue = new JButton("4 Blue");
        leftblue.addActionListener(new GameLogic(leftblue, 22));
        left.add(leftlabel);
        left.add(leftyellow);
        left.add(leftred);
        left.add(leftblue);

        //implement right panel for player 2 controls
        JPanel right = new JPanel();
        right.setSize(100,300);
        right.setLayout(new GridLayout(4,1));
        JLabel rightlabel = new JLabel("Player 2 Controls");
        rightlabel.setOpaque(true);
        JButton rightyellow = new JButton("8 Yellow");
        rightyellow.addActionListener(new GameLogic(rightyellow, 30));
        JButton rightred = new JButton("4 Red");
        rightred.addActionListener(new GameLogic(rightred, 31));
        JButton rightblue = new JButton("4 Blue");
        rightblue.addActionListener(new GameLogic(rightblue, 32));
        left.add(rightlabel);
        left.add(rightyellow);
        left.add(rightred);
        left.add(rightblue);


        //initialize JFrame that contains our game panel
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(700,1100);
        frame.setLayout(new BorderLayout());
        frame.add(left, BorderLayout.WEST);
        frame.add(panel, BorderLayout.SOUTH);
        frame.add(right, BorderLayout.EAST);    
        frame.pack();
        frame.setVisible(true);
    }

}

class GameLogic implements ActionListener
{
    JButton owner;
    //this will help us keep track of which button has been pressed
    int identity;
    //this will keep track of whose turn it is TRUE = PLAYER1 and FALSE = PLAYER2
    boolean turn = true;
    //this will keep track of if a gameplay button has been pressed
    int pressed = -1;
    //these 3 are to keep track of player 1's pieces
    int yellow1 = 8;
    int red1 = 4;
    int blue1 = 4;
    //these 3 are to keep track of player 2's pieces
    int yellow2 = 8;
    int red2 = 4;
    int blue2 = 4;


    public GameLogic(JButton b, int id)
    {
        owner = b;
        identity = id;
    }

    public void actionPerformed(ActionEvent e)
    {

        //if we click PLAYER1 YELLOW, do this
        else if((identity == 20) && (yellow1 > 0) && (turn == true))
        {
        pressed = 1;
        turn = false;
        yellow1--;
        owner.setText(yellow1 + " Yellow"); 
        }
        //if we click PLAYER1 RED, do this
        else if((identity == 21) && (red1 > 0) && (turn == true))
        {
        pressed = 2;
        turn = false;
        red1--; 
        owner.setText(red1 + " Red");
        }
        //if we click PLAYER1 BLUE, do this
        else if((identity == 22) && (blue1 > 0) && (turn == true))
        {
        pressed = 3;
        turn = false;
        blue1--;
        owner.setText(blue1 + " Blue"); 
        }
        //if we click PLAYER2 YELLOW, do this
        else if((identity == 30) && (yellow2 > 0) && (turn == false))
        {
        pressed = 4;
        turn = true;
        yellow2--;
        owner.setText(yellow2 + " Yellow"); 
        }
        //if we click PLAYER2 RED, do this
        else if((identity == 31) && (red2 > 0) && (turn == false))
        {
        pressed = 5;
        turn = true;
        red2--; 
        owner.setText(red2 + " Red");
        }
        //if we click PLAYER2 BLUE, do this
        else if((identity == 32) && (blue2 > 0) && (turn == false))
        {
        pressed = 6;
        turn = true;
        blue2--;
        owner.setText(blue2 + " Blue"); 
        }

    }
}

2 个答案:

答案 0 :(得分:3)

您已经实例化了多个“GameLogic”动作侦听器,每个动作侦听器都有自己的变量“turn”,默认情况下都是“true”。

您应该拥有一个维护应用程序状态的GameLogic类。这不应该是你的actionlistener。

答案 1 :(得分:0)

请查看**行。

//为播放器2控件实现右侧面板

    JPanel right = new JPanel();
    right.setSize(100,300);
    right.setLayout(new GridLayout(4,1));
    JLabel rightlabel = new JLabel("Player 2 Controls");
    rightlabel.setOpaque(true);
    JButton rightyellow = new JButton("8 Yellow");
    rightyellow.addActionListener(new GameLogic(rightyellow, 30));
    JButton rightred = new JButton("4 Red");
    rightred.addActionListener(new GameLogic(rightred, 31));
    JButton rightblue = new JButton("4 Blue");
    rightblue.addActionListener(new GameLogic(rightblue, 32));
    left.add(rightlabel);
    **left**.add(rightyellow);
    **left**.add(rightred);
    **left**.add(rightblue);

我认为您已将右侧面板的按钮添加到左侧面板中。