0

我遇到了一个我一直在处理的错误,一段时间以来,错误是“构造函数 HangmanPanel() 未定义”,但我在 Hangman() 中定义了它..,我认为它可能希望我使用参数,但我不确定是哪些....

刽子手.java

import javax.swing.*;
public class Hangman extends JFrame{
    private static final long serialVersionUID = -6224212014648478637L;

    public static void main(String[] args){
        new Hangman();

    }

    public Hangman() {
        this.setSize(800, 500);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setTitle("Hangman Web App");

        HangmanPanel panel = new HangmanPanel();
        this.add(panel);

        this.setVisible(true);
        }

}

HangmanPanel.java

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

public class HangmanPanel extends JPanel {
    static Boolean FOUND;
    private static final long serialVersionUID = -5793357804828609325L;

    public static String answerKey() {
        //get random array element
        String array[] = new String[10];
        array[0] = "hamlet";
        array[1] = "mysts of avalon";
        array[2] = "the iliad";
        array[3] = "tales from edger allan poe";
        array[4] = "the children of hurin";
        array[5] = "the red b" +
                "+adge of courage";
        array[6] = "of mice and men";
        array[7] =  "utopia"; 
        array[8] =  "chariots of the gods";
        array[9] =  "a brief history of time";

        ArrayList<String> list = new ArrayList<String>(Arrays.asList(array));
        Collections.shuffle(list);
        String s = list.get(0);
        return s;
    }

    public StringBuilder dashReplace(String s) {
        //replace non-white space char with dashes and creates StringBuilder Object
        String tW = s.replaceAll("\\S", "-"); 
        System.out.print(tW + "\n");  
        StringBuilder AnswerKey = new StringBuilder(tW);
        return AnswerKey;
    }
    public static int findAndReplace(String s, JLabel answerKey, String sc,
            char ch) {
        //find position of user input and replace
        int pos = -1;
        FOUND = false;
        while(true){
        pos = s.indexOf(sc, pos+1);
        if(pos < 0){

            break;
        }else{
            FOUND = true;
            StringBuilder builder = new StringBuilder(answerKey.getText());
            builder.setCharAt(pos, ch);
            answerKey.setText(builder.toString());
        }

        }
        JLabel AnswerKey2 = new JLabel(answerKey.toString());
        return pos;
    }




    public HangmanPanel(final String s){
        this.setLayout(null);

        JLabel heading = new JLabel("Welcome to the Hangman App");
        JButton Button = new JButton("Ok");
        //get input

        JLabel tfLable = new JLabel("Please Enter a Letter:");


        final JLabel AnswerKey = new JLabel(dashReplace(answerKey()).toString());

        final JTextField text = new JTextField(10);


        heading.setSize(200, 50);
        tfLable.setSize(150, 50);
        text.setSize(50, 30);
        Button.setSize(60, 20);
        AnswerKey.setSize(200, 100);

        heading.setLocation(300, 10);
        tfLable.setLocation(50, 40);
        text.setLocation(50, 80);
        Button.setLocation(100, 85);
        AnswerKey.setLocation(100,85);

        this.add(heading);
        this.add(tfLable);
        this.add(text);
        this.add(Button);
        this.add(AnswerKey);


        Button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // can't access text
                String sc = text.getText();
                char ch = sc.charAt(0);
                findAndReplace(s, AnswerKey, sc, ch);
            }
        });

    }

}
4

3 回答 3

2

您没有无参数的构造函数HangmanPanel

如果你把它改成

    HangmanPanel panel = new HangmanPanel("some appropriate string");

它应该编译

于 2013-05-09T16:14:23.823 回答
1

您只有在 HangManPanel 中定义的接受字符串的构造函数,没有其他构造函数。

添加public HangmanPanel()到您的班级 HangmanPanet

于 2013-05-09T16:13:26.240 回答
1

错误消息是指类 HangManPanel 没有构造函数 HangManPanel()。给你的问题:鉴于对错误消息的理解,你能看看你的代码,看看为什么编译器会告诉你这个吗?

于 2013-05-09T16:13:27.510 回答