ActionListener没有检测到按钮?

时间:2015-03-07 03:19:55

标签: javascript actionlistener

代码看起来很长,但我不知道如何让ActionListener使用discard按钮和绘制按钮。 我读了一些教程,但我还是没有真正理解它。我认为你使用getSource()或什么?

谢谢!



package mainpanel;

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

public class Player implements ActionListener{
    String[] cardsInHand=new String[0];
    int numberOfLives;
    String name;
    JButton discard=new JButton("Discard");
    JButton draw=new JButton("Draw");
    public Player(File a) throws FileNotFoundException{
        Scanner scanner=new Scanner(a);
        if(scanner.hasNextLine()){
            name=scanner.nextLine();
            System.out.println(name);
        }
        if(scanner.hasNextLine()){
            numberOfLives=Integer.parseInt(scanner.nextLine());
            System.out.println(numberOfLives);
        }
        while(scanner.hasNextLine()){
            addCard(scanner.nextLine());
        }
        for(int i=0;i<cardsInHand.length;i++){
            System.out.println(cardsInHand[i]);
        }
    }
    public void addCard(String a){
        String[] temp=new String[cardsInHand.length+1];
        for(int i=0;i<cardsInHand.length;i++){
            temp[i]=cardsInHand[i];
        }
        temp[cardsInHand.length]=a;
        cardsInHand=temp;
    }
    public void useCard(String a){
        int index=-1;
        for(int i=0;i<cardsInHand.length;i++){
            if(a.equals(cardsInHand[i])){
                index=i;
                System.out.println("i="+i);
            }
        }
        if(index!=-1 && index!=0){
            for(int i=index;i<=cardsInHand.length;i++){
                cardsInHand[i-1]=cardsInHand[i];
                System.out.println("\n"+cardsInHand[i]);
            }
            String[] temp=cardsInHand;
            cardsInHand=new String[temp.length-1];
            for(int i=0;i<cardsInHand.length;i++){
                cardsInHand[i]=temp[i];
            }
        }else if(index==0){
            String[] temp=cardsInHand;
            cardsInHand=new String[temp.length-1];
            for(int i=0;i<cardsInHand.length;i++){
                cardsInHand[i]=temp[i+1];
            }
        }else{
            System.out.println("No such card");
        }
    }
    public String toString(){
        String cards="";
        for(int i=0;i<cardsInHand.length;i++){
            cards+=cardsInHand[i]+"\n";
        }
        return cards;
    }
    public void update() throws FileNotFoundException{
        PrintStream output=new PrintStream("H:/"+name+".txt");
        output.println(name);
        output.println(numberOfLives);
        for(int i=0;i<cardsInHand.length;i++){
            output.println(cardsInHand[i]);
        }
    }
    public JPanel createStats(){
        JPanel stat=new JPanel(new FlowLayout());
        JComboBox comboBox=new JComboBox(cardsInHand);
        JLabel label1 = new JLabel("Select card:");
        stat.add(label1);
        stat.add(comboBox);
        stat.add(draw);
        stat.add(discard);
        return stat;
    }
    public void actionPerformed(ActionEvent e) {
        if(e.getSource()==discard){
            useCard("Bang");
        }
    }
}
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

来自oracle的指南

要编写动作侦听器,请按照以下步骤操作:

声明一个事件处理程序类,并指定该类实现ActionListener接口或扩展实现ActionListener接口的类。例如:

public class MyClass implements ActionListener { 

将事件处理程序类的实例注册为一个或多个组件上的侦听器。例如:

// this "someComponent" will be the instance of any swing element 
//where you want to add that action listener. ex- JPanel, JButton etc

someComponent.addActionListener(instanceOfMyClass);

包含在侦听器接口中实现方法的代码。例如:

// this method will be called when you perform some action on the element 
//on which you have binded the listener

public void actionPerformed(ActionEvent e) { 
    ...//code that reacts to the action... 
}

了解更多信息 How to write actionListener