Hangman actionListener图形

时间:2016-05-19 15:51:43

标签: java swing actionlistener

我目前正在使用Java制作一个自定义Hangman游戏,并且已进入JPanel上显示按钮的阶段。我没有按钮工作,因为letterA actionListener中的if语句将无法访问。此外,单击时,这些字母不会更新显示被绞死人员的Hanger类的图形。有人可以帮忙看看为什么letterA if语句将无法访问并修复它。我已经尝试使用多个System.out.println()语句来查看程序是否曾在letterA actionListener中输入if if语句而它没有。请记住,我没有得到HangmanWord中的字母来点击时改变颜色。任何建议或帮助都会很棒。 This is an example of clicking the letter A button and not having any drawing appear (the head is already supposed to be there because I set the count to a number other than 0.

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

public class HangmanGUI extends JFrame{

JFrame Hangman = new JFrame("Hangman");
JPanel panel = new JPanel();

private ArrayList<String> Words = new ArrayList<String>();
private String[] AlphabetArray;
private ArrayList<String> Alphabet = new ArrayList<String>();
private String HangmanWord;
private final int PanelHeight = 400;
private final int PanelWidth = 900;
private final int BUTTON = 20;
//private JButton play; /** this is the button you push to play the  */ 
//private JButton reset; /** this is the button you push to reset the game */ 
//private int totalWins; /** this is the total number of game wins */
//private int totalGames; /** this is the total number of games */
private JButton letterA; /** the letter button for the specified letter */
private JButton letterB; /** the letter button */
private JButton letterC; /** the letter button */
private JButton letterD; /** the letter button */
private JButton letterE; /** the letter button */
private JButton letterF; /** the letter button */
private JButton letterG; /** the letter button */
private JButton letterH; /** the letter button */
private JButton letterI; /** the letter button */
private JButton letterJ; /** the letter button */
private JButton letterK; /** the letter button */
private JButton letterL; /** the letter button */
private JButton letterM; /** the letter button */
private JButton letterN; /** the letter button */
private JButton letterO; /** the letter button */
private JButton letterP; /** the letter button */
private JButton letterQ; /** the letter button */
private JButton letterR; /** the letter button */
private JButton letterS; /** the letter button */
private JButton letterT; /** the letter button */
private JButton letterU; /** the letter button */
private JButton letterV; /** the letter button */
private JButton letterW; /** the letter button */
private JButton letterX; /** the letter button */
private JButton letterY; /** the letter button */
private JButton letterZ; /** the letter button */
private Hanger hang;
private int count = 0;


public HangmanGUI(){
    panel.setBorder(BorderFactory.createTitledBorder("LETTERS"));
    panel.setBounds(600, 200, 450, 250);
    assignLetters();
    add(panel);
    addWords();
    getWord();

    hang = new Hanger(getCount());
    hang.setBounds(0,0, 250, 200);
    this.add(hang, BorderLayout.CENTER);


}

public int getCount(){
    return count;
}

public void action(ActionEvent a, JButton x){
    if (!HangmanWord.contains(x.getText()) || !HangmanWord.contains(x.getText().toLowerCase())){
        count++;
        System.out.println(x.getText());
        hang.setCount(count);
        hang = new Hanger(getCount());
        hang.setBounds(0,0,250,200);
        Hangman.remove(hang);
        Hangman.add(hang);
        Hangman.setVisible(true);
        Hangman.invalidate();
        Hangman.validate();
        Hangman.repaint();
    }
    x.setVisible(false);
}


public void assignLetters(){
    count = 5;

    letterA = new JButton();
    letterA.setText("A");
    letterA.setOpaque(true);
    panel.add(letterA);
    letterA.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            count ++;
            if ((HangmanWord.contains(letterA.getText()))==true || (HangmanWord.contains(letterA.getText().toLowerCase()))==true){
                count --;
            }
            hang.setCount(getCount());
            hang = new Hanger(getCount());
            hang.setBounds(0,0, 250, 200);

            Hangman.remove(hang);
            Hangman.add(hang);
            letterA.setVisible(false);

        }
    });


    letterB = new JButton();
    letterB.setText("B");
    panel.add(letterB);
    //letterB.setBounds(480, 0, BUTTON, BUTTON);
    letterB.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            action(e, letterB);
            if ((HangmanWord.indexOf("B")==-1) || (HangmanWord.indexOf("b")==-1)){
                letterB.setVisible(false);
                count ++;
                System.out.println("b");
                hang.setCount(getCount());
                hang.setBounds(0,0, 250, 200);
                Hangman.add(hang, BorderLayout.CENTER);
            }
            letterA.setVisible(false);
            hang = new Hanger(getCount());
            hang.setCount(getCount());
            hang.setBounds(0,0, 250, 200);;
            Hangman.add(hang, BorderLayout.CENTER);
            Hangman.setVisible(true);
            Hangman.invalidate();
            Hangman.validate();
            Hangman.repaint();
            letterB.setForeground(Color.RED);
        }

    });

    letterC = new JButton();
    letterC.setText("C");
    panel.add(letterC);
    //letterC.setBounds(510, 0, BUTTON, BUTTON);
    letterC.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            letterC.setVisible(false);
            if (!HangmanWord.contains(letterC.getText())){
                count ++;
                hang.setCount(getCount());
                hang = new Hanger(getCount());
                hang.setBounds(0,0, 250, 200);;
                Hangman.add(hang, BorderLayout.CENTER);
                Hangman.setVisible(true);
            }

        }
    });

    letterD = new JButton();
    letterD.setText("D");
    panel.add(letterD);
    //letterD.setBounds(540, 0, BUTTON, BUTTON);
    letterD.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            letterD.setVisible(false);
            if (!HangmanWord.contains(letterD.getText())){
                count ++;
                hang.setCount(getCount());
                hang = new Hanger(getCount());
            }

        }
    });

    letterE = new JButton();
    letterE.setText("E");
    panel.add(letterE);
    //letterE.setBounds(570, 0, BUTTON, BUTTON);
    letterE.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            letterE.setVisible(false);
            if (!HangmanWord.contains(letterE.getText())){
                count ++;
            }
        }
    });

    letterF = new JButton();
    letterF.setText("F");
    panel.add(letterF);
    //letterF.setBounds(600, 0, BUTTON, BUTTON);
    letterF.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            letterF.setVisible(false);
            if (!HangmanWord.contains(letterF.getText())){
                count ++;
            }
        }
    });

    letterG = new JButton();
    letterG.setText("G");
    panel.add(letterG);
    //letterG.setBounds(630, 0, BUTTON, BUTTON);
    letterG.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            letterG.setVisible(false);
            if (!HangmanWord.contains(letterG.getText())){
                count ++;
            }
        }
    });

    letterH = new JButton();
    letterH.setText("H");
    panel.add(letterH);
    //letterH.setBounds(660, 0, BUTTON, BUTTON);
    letterH.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            letterH.setVisible(false);
            if (!HangmanWord.contains(letterH.getText())){
                count ++;
            }
        }
    });

    letterI = new JButton();
    letterI.setText("I");
    panel.add(letterI);
    //letterI.setBounds(690, 0, BUTTON, BUTTON);
    letterI.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            letterI.setVisible(false);
            if (!HangmanWord.contains(letterI.getText())){
                count ++;
            }
        }
    });

    letterJ = new JButton();
    letterJ.setText("J");
    panel.add(letterJ);
    //letterJ.setBounds(720, 0, BUTTON, BUTTON);
    letterJ.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            letterJ.setVisible(false);
            if (!HangmanWord.contains(letterJ.getText())){
                count ++;
            }
        }
    });

    letterK = new JButton();
    letterK.setText("K");
    panel.add(letterK);
    //letterK.setBounds(750, 0, BUTTON, BUTTON);
    letterK.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            letterK.setVisible(false);
            if (!HangmanWord.contains(letterK.getText())){
                count ++;
            }
        }
    });

    letterL = new JButton();
    letterL.setText("L");
    panel.add(letterL);
    //letterL.setBounds(780, 0, BUTTON, BUTTON);
    letterL.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            letterL.setVisible(false);
            if (!HangmanWord.contains(letterL.getText())){
                count ++;
            }
        }
    });

    letterM = new JButton();
    letterM.setText("M");
    panel.add(letterM);
    //letterM.setBounds(810, 0, BUTTON, BUTTON);
    letterM.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            letterM.setVisible(false);
            if (!HangmanWord.contains(letterM.getText())){
                count ++;
            }
        }
    });

    letterN = new JButton();
    letterN.setText("N");
    panel.add(letterN);
    //letterN.setBounds(450, 30, BUTTON, BUTTON);
    letterN.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            letterN.setVisible(false);
            if (!HangmanWord.contains(letterN.getText())){
                count ++;
            }
        }
    });

    letterO = new JButton();
    letterO.setText("O");
    panel.add(letterO);
    //letterO.setBounds(480, 30, BUTTON, BUTTON);
    letterO.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            letterO.setVisible(false);
            if (!HangmanWord.contains(letterO.getText())){
                count ++;
            }
        }
    });

    letterP = new JButton();
    letterP.setText("P");
    panel.add(letterP);
    //letterP.setBounds(510, 30, BUTTON, BUTTON);
    letterP.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            letterP.setVisible(false);
            if (!HangmanWord.contains(letterP.getText())){
                count ++;
            }
        }
    });

    letterQ = new JButton();
    letterQ.setText("Q");
    panel.add(letterQ);
    //letterQ.setBounds(540, 30, BUTTON, BUTTON);
    letterQ.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            letterQ.setVisible(false);
            if (!HangmanWord.contains(letterQ.getText())){
                count ++;
            }
        }
    });

    letterR = new JButton();
    letterR.setText("R");
    panel.add(letterR);
    //letterR.setBounds(570, 30, BUTTON, BUTTON);
    letterR.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            letterR.setVisible(false);
            if (!HangmanWord.contains(letterR.getText())){
                count ++;
            }
        }
    });

    letterS = new JButton();
    letterS.setText("S");
    panel.add(letterS);
    //letterS.setBounds(600, 30, BUTTON, BUTTON);
    letterS.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            letterS.setVisible(false);
            if (!HangmanWord.contains(letterS.getText())){
                count ++;
            }
        }
    });

    letterT = new JButton();
    letterT.setText("T");
    panel.add(letterT);
    //letterT.setBounds(630, 30, BUTTON, BUTTON);
    letterT.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            letterT.setVisible(false);
            if (!HangmanWord.contains(letterT.getText())){
                count ++;
            }
        }
    });

    letterU = new JButton();
    letterU.setText("U");
    panel.add(letterU);
    //letterU.setBounds(660, 30, BUTTON, BUTTON);
    letterU.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            letterU.setVisible(false);
            if (!HangmanWord.contains(letterU.getText())){
                count ++;
            }
        }
    });

    letterV = new JButton();
    letterV.setText("V");
    panel.add(letterV);
    //letterV.setBounds(690, 30, BUTTON, BUTTON);
    letterV.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            letterV.setVisible(false);
            if (!HangmanWord.contains(letterV.getText())){
                count ++;
            }
        }
    });

    letterW = new JButton();
    letterW.setText("W");
    panel.add(letterW);
    //letterW.setBounds(720, 30, BUTTON, BUTTON);
    letterW.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            letterW.setVisible(false);
            if (!HangmanWord.contains(letterW.getText())){
                count ++;
            }
        }
    });

    letterX = new JButton();
    letterX.setText("X");
    panel.add(letterX);
    //letterX.setBounds(750, 30, BUTTON, BUTTON);
    letterX.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            letterX.setVisible(false);
            if (!HangmanWord.contains(letterX.getText())){
                count ++;
            }
        }
    });

    letterY = new JButton();
    letterY.setText("Y");
    panel.add(letterY);
    //letterY.setBounds(780, 30, BUTTON, BUTTON);
    letterY.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            letterY.setVisible(false);
            if (!HangmanWord.contains(letterY.getText())){
                count ++;
            }
        }
    });

    letterZ = new JButton();
    letterZ.setText("Z");
    panel.add(letterZ);
    //letterZ.setBounds(810, 30, BUTTON, BUTTON);
    letterZ.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            letterZ.setVisible(false);
            if (!HangmanWord.contains(letterZ.getText())){
                count ++;
            }
        }
    });

}

public void alphabet(){
    String [] AlphabetArray;
    AlphabetArray = new String[] {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"};
    Alphabet.addAll(Arrays.asList(AlphabetArray));
}

public void addWords(){
    Words.add("Quip");


}


public String getWord(){
    int wordsIndex = (int) (Math.random() * (Words.size()));
    HangmanWord =  Words.get(wordsIndex);
    return HangmanWord;
}


public void setWord(String a){
    HangmanWord = a;
}


}


import javax.swing.*;
import java.awt.*;
public class GUI extends JPanel
{
public static void main(String[] args) 
{
    HangmanGUI theGUI = new HangmanGUI();
    theGUI.setTitle("HANGMAN");
    theGUI.setSize(1440,900);
    theGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);



    ColorPanel panel = new ColorPanel(Color.blue);
    Container pane = theGUI.getContentPane();
    pane.add(panel);


    String a = panel.getWord();

    theGUI.setWord(a);

    theGUI.setVisible(true); 
}

}

    import javax.swing.*;
import java.awt.*;

public class ColorPanel extends JPanel 
{

private String word; 

public ColorPanel (Color backColor)
{
    setBackground(backColor); //takes parameter from GUIWindow class and sets background color
}

public void paintComponent(Graphics g)
{

    super.paintComponent(g); //call this line first
    //choose a color before you draw the shape 
    g.setColor(Color.blue);
    g.drawRect(0,  150,  100,  200); //the numbers give the coordinates and size




    Font f = new Font("Comic Sans MS", Font.BOLD, 30);
    g.setFont(f);
    g.setColor(Color.cyan);
    g.drawString("HANGMAN", 200, 50);


    g.setColor(Color.black);
    g.fillRect(0,75,240,30); //this should always be there


    //number wrong should be a counter
    HangmanGUI wor = new HangmanGUI();
    word = wor.getWord();

    int counter=0;
    int max=0;



        //find the length of the longest word in the saying
        for(int x=0; x<word.length(); x++){
                if (!word.substring(x,x+1).equals(" ")){
                    counter++;

                    if (counter>max){
                        max=counter;
                    }
                }
                else{

                    counter=0; 

            }

            }


    int x1cord = 25;
    int y1cord=500;
    int space=500/(max); //TRY TO PUT MAX HERE

    for (int i=0; i<word.length(); i++){    

        if (word.substring(i, i+1).equals(" ")){

            x1cord=25;//+100 if same line
            y1cord+=space;

        }
        else{   
            g.setColor(Color.black);
            g.drawLine(x1cord,  y1cord, x1cord+space/2,  y1cord);

            f = new Font("Comic Sans MS", Font.BOLD, space/3);
            g.setFont(f);
            g.setColor(Color.cyan);
            g.drawString(word.substring(i,i+1), x1cord+space/4, y1cord-5);

            x1cord+=space;
        }
        }

    }

public String getWord(){
    return word;
}

}

    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.BasicStroke;  
    import java.awt.Rectangle;
    import java.awt.Graphics2D;           
    import java.awt.geom.Ellipse2D;       
  import java.awt.geom.Rectangle2D;     
  import java.awt.geom.Arc2D;           
  import java.awt.geom.Line2D;          
  import javax.swing.JPanel;

public class Hanger extends JPanel{
private JPanel hangedPerson;
private int count;

public Hanger(int getCount){
    count = getCount;
}

public void setCount(int getCount){
    count = getCount;
}

public void paintComponent(Graphics g){
    super.paintComponents(g);
    Graphics2D hangedPerson = (Graphics2D) g;
    hangedPerson.setBackground(Color.BLUE);
    hangedPerson.setPaint(Color.BLUE);
    hangedPerson.drawRect(0,  150,  100,  200);


        if (count>0){
            g.setColor(Color.black);
            g.drawLine(175,  75,  175,  150); //if one wrong    
        }

        if (count>1){
            g.setColor(Color.black); //if two wrong
            g.fillOval(150,150,50,50); 
        }

        if (count>2){
            g.setColor(Color.black);
            g.drawLine(175,  350,  175,  200);//if  three wrong
        }

        if (count>3){
            g.setColor(Color.black);
            g.drawLine(90,  200,  175,  250);//if four wrong
        }

        if (count>4){
            g.setColor(Color.black);
            g.drawLine(175,  250,  260,  200);//if five wrong
        }

        if (count>5){
            g.setColor(Color.black);
            g.drawLine(175,  350,  250,  400);// if six wrong
        }

        if (count>6){
            g.setColor(Color.black);
            g.drawLine(175,  350,  100,  400); //if seven wrong
        }



}


}

0 个答案:

没有答案