如何从Java中的一个特定JButton中取出每个旧的ActionListener?

时间:2014-12-23 03:22:31

标签: java jframe jbutton actionlistener

我正在开发BlackJack游戏,显然,我需要一个按键和支架按钮。我在main方法的hit和stand按钮中添加了actionListener。但是,当单击立场按钮时,我无法弄清楚如何取消旧按钮actionListener。这意味着即使您点击“Stand”,点击按钮仍然有效,反之亦然。

以下是代码:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;



public class Jblackjack {
// Creating static variables

    static JButton card1 = new JButton(" ");
    static JButton card2 = new JButton(" ");
    static JButton card3 = new JButton(" ");
    static JButton card4 = new JButton(" ");
    static JButton compcard1 = new JButton(" ");
    static JButton compcard2 =new JButton(" ");
    static JButton compcard3 = new JButton(" ");
    static JFrame frame = new JFrame("BlackJack");
    static JButton no = new JButton("Cancel");
    static JButton button = new JButton("Let's go!");
    static JButton hit = new JButton("hit");
    static JButton stand = new JButton("Stand");
    static JLabel comptext = new JLabel("The computer has:");
    static JLabel text = new JLabel("We are going to play Blackjack.");
    static JLabel text2 = new JLabel("This Blackjack does not have any splits, doubles, surrenders, or insurance.  Just hits and stands.");
    static JLabel text3 = new JLabel("This is a WIP");
    static JLabel text4 = new JLabel("11's are Jacks.  12's are Queens.  13's are Kings.  1's are Ones for now.");
    static JLabel text5 = new JLabel("Are you ready?");



    static class Cancel implements ActionListener{
        public void actionPerformed (ActionEvent e){
            System.exit(0);
        }
    }

    static class Play implements ActionListener{
        public void actionPerformed (ActionEvent e){
            text.setText("You have:");
            text2.setVisible(false);
            text3.setVisible(false);
            text4.setVisible(false);
            text5.setVisible(false);
            no.setVisible(false);
            button.setVisible(false);
            frame.add(hit);
            frame.add(stand);
            frame.add(card1);
            text.setText("You have:");
            int a = 1 + (int)(Math.random()*4);
            int b = (int)(Math.random()*13);
            if(a==1){
                if((b==11) || (b==12) || (b==13)){
                    b = 10;
                }
                card1.setText(b + "Clubs");
            } else if (a==2){
                if((b==11) || (b==12) || (b==13)){
                    b = 10;
                }
                card1.setText(b + "Spades");
            } else if(a==3){
                if((b==11) || (b==12) || (b==13)){
                    b = 10;
                }
                card1.setText(b + "Hearts");
            } else if(a==4){
                if((b==11) || (b==12) || (b==13)){
                    b = 10;
                }
                card1.setText(b + "Diamonds");
            }
            frame.add(card2);
            int c = 1 + (int)(Math.random()*4);
            int d = (int)(Math.random()*13);
            if(c==1){
                if((d==11) || (d==12) || (d==13)){
                    d = 10;
                }
                card2.setText(d + "Clubs");
            } else if(c==2){
                if((d==11) || (d==12) || (d==13)){
                    d = 10;
                }
                card2.setText(d + "Spades");
            } else if(c==3){
                if((d==11) || (d==12) || (d==13)){
                    d = 10;
                }
                card2.setText(d + "Hearts");
            } else if(c==4){
                if((d==11) || (d==12) || (d==13)){
                    d = 10;
                }
                card2.setText(d + "Diamonds");
            }

            frame.add(comptext);
            comptext.setBounds(10, 350, 200, 50);
            frame.add(compcard1);
            int i = 1 + (int)(Math.random()*4);
            int f = (int)(Math.random()*13);
            if(i==1){
                if((f==11) || (f==12) || (f==13)){
                    f = 10;
                }
                compcard1.setText(f + "Clubs");
            } else if (i==2){
                if((f==11) || (f==12) || (f==13)){
                    f = 10;
                }
                compcard1.setText(f + "Spades");
            } else if(i==3){
                if((f==11) || (f==12) || (f==13)){
                    f = 10;
                }
                compcard1.setText(f + "Hearts");
            } else if(i==4){
                if((f==11) || (f==12) || (f==13)){
                    f = 10;
                }
                compcard1.setText(f + "Diamonds");
            }
            frame.add(compcard2);
            int g = 1 + (int)(Math.random()*4);
            int h = (int)(Math.random()*13);
            if(g==1){
                if((h==11) || (h==12) || (h==13)){
                    h = 10;
                }
                compcard2.setText(h + "Clubs");
            } else if(g==2){
                if((h==11) || (h==12) || (h==13)){
                    h = 10;
                }
                compcard2.setText(h + "Spades");
            } else if(g==3){
                if((h==11) || (h==12) || (h==13)){
                    h = 10;
                }
                compcard2.setText(h + "Hearts");
            } else if(g==4){
                if((h==11) || (h==12) || (h==13)){
                    h = 10;
                }
                compcard2.setText(h + "Diamonds");
            }
            frame.revalidate();
            frame.repaint();
        }
    }

    static class Hit implements ActionListener{
        public void actionPerformed (ActionEvent e){
            hit.removeActionListener(this);
            hit.addActionListener(new Hit2());
            frame.add(card3);
            int a = 1 + (int)(Math.random()*4);
            int b = (int)(Math.random()*13);
            if(a==1){
                if((b==11) || (b==12) || (b==13)){
                    b = 10;
                }
                card3.setText(b + "Clubs");
            } else if (a==2){
                if((b==11) || (b==12) || (b==13)){
                    b = 10;
                }
                card3.setText(b + "Spades");
            } else if(a==3){
                if((b==11) || (b==12) || (b==13)){
                    b = 10;
                }
                card3.setText(b + "Hearts");
            } else if(a==4){
                if((b==11) || (b==12) || (b==13)){
                    b = 10;
                }
                card3.setText(b + "Diamonds");
            }
            frame.revalidate();
            frame.repaint();
        }
    }
    static class Hit2 implements ActionListener{
        public void actionPerformed (ActionEvent e){
            hit.removeActionListener(this);
            hit.addActionListener(new Hit3());
            frame.add(card4);
            int c = 1 + (int)(Math.random()*4);
            int d = (int)(Math.random()*13);
            if(c==1){
                if((d==11) || (d==12) || (d==13)){
                    d = 10;
                }
                card4.setText(d + "Clubs");
            } else if(c==2){
                if((d==11) || (d==12) || (d==13)){
                    d = 10;
                }
                card4.setText(d + "Spades");
            } else if(c==3){
                if((d==11) || (d==12) || (d==13)){
                    d = 10;
                }
                card4.setText(d + "Hearts");
            } else if(c==4){
                if((d==11) || (d==12) || (d==13)){
                    d = 10;
                }
                card4.setText(d + "Diamonds");
            }
            frame.revalidate();
            frame.repaint();
        }
    }

    static class Hit3 implements ActionListener{
        public void actionPerformed (ActionEvent e){

        }
    }
    static class Stand implements ActionListener{
        public void actionPerformed (ActionEvent e){
            frame.add(compcard3);
            int a = 1 + (int)(Math.random()*4);
            int b = (int)(Math.random()*13);
            if(a==1){
                if((b==11) || (b==12) || (b==13)){
                    b = 10;
                }
                compcard3.setText(b + "Clubs");
            } else if (a==2){
                if((b==11) || (b==12) || (b==13)){
                    b = 10;
                }
                compcard3.setText(b + "Spades");
            } else if(a==3){
                if((b==11) || (b==12) || (b==13)){
                    b = 10;
                }
                compcard3.setText(b + "Hearts");
            } else if(a==4){
                if((b==11) || (b==12) || (b==13)){
                    b = 10;
                }
                compcard3.setText(b + "Diamonds");
            }
        }
    }
    static class Stand2 implements ActionListener{
        public void actionPerformed (ActionEvent e){

        }
    }
    static class Stand3 implements ActionListener{
        public void actionPerformed (ActionEvent e){

        }
    }
    public static void main(String[] args){
        //JFrame properties
        frame.setLayout(null);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);

        // Creating "JVariables"
        JLabel background = new JLabel(new ImageIcon("BlackJackBackGround.png"));

        // Adding JVariables and setting bounds
        card1.setBounds(20, 50, 110, 150);
        card2.setBounds(140, 50, 110, 150);
        card3.setBounds(260, 50, 110, 150);
        card4.setBounds(380, 50, 110, 150);
        hit.setBounds(20, 300, 100, 50);
        hit.addActionListener(new Hit());
        stand.setBounds(130, 300, 100, 50);
        stand.addActionListener(new Stand());
        compcard1.setBounds(20, 400, 110, 150);
        compcard2.setBounds(140, 400, 110, 150);
        compcard3.setBounds(260, 400, 110, 150);
        frame.add(background);
        frame.add(text);
        text.setBounds(5, 0, 250, 50);
        frame.add(text2);
        text2.setBounds(5, 25, 550, 50);
        frame.add(text3);
        text3.setBounds(5, 50, 150, 50);
        frame.add(text4);
        text4.setBounds(5, 75, 550, 50);
        frame.add(text5);
        text5.setBounds(5, 100, 150, 50);
        frame.add(no);
        no.setBounds(10, 150, 150, 50);
        no.addActionListener(new Cancel());
        frame.add(button);
        button.setBounds(170, 150, 150, 50);
        button.addActionListener(new Play());
        frame.setSize(1000, 700);

    }
}

0 个答案:

没有答案