-2

我已经发布了一个关于如何从文件中读取并从该文件中返回一个随机单词的问题,每个人都一直告诉我使用“List words = new ArrayList();” 我确实尝试过使用它,但它不起作用!

我什么都试过了!我对java很陌生,因为我还是个学生!所以互联网是我最后的选择,教科书没用......

我要做的就是从该文件中返回一个随机字符串,然后将其用作猜测词

在此处输入图像描述

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.ArrayList;
import java.util.Random;
import java.util.*;

public final class Hangman extends JFrame implements ActionListener {
    int i = 0;
    static JPanel panel;
    static JPanel panel2;
    static JPanel panel3;
    static JPanel panel4;

    public Hangman() {
        JButton[] buttons = new JButton[26];

        panel = new JPanel(new GridLayout(0, 9));
        panel2 = new JPanel();
        panel3 = new JPanel();
        panel4 = new JPanel();

        JButton btnRestart = new JButton("Restart");
        btnRestart.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

            }
        });

        JButton btnNewWord = new JButton("Add New Word");
        btnNewWord.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    FileWriter fw = new FileWriter("Words.txt", true);
                    PrintWriter pw = new PrintWriter(fw, true);

                    String word = JOptionPane
                            .showInputDialog("Please enter a word: ");

                    pw.println(word);
                    pw.close();
                } catch (IOException ie) {
                    System.out.println("Error Thrown" + ie.getMessage());
                }
            }
        });

        JButton btnHelp = new JButton("Help");
        btnHelp.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String message = "The word to guess is represented by a row of dashes, giving the number of letters and category of the word. \nIf the guessing player suggests a letter which occurs in the word, the other player writes it in all its correct positions. \nIf the suggested letter does not occur in the word, the other player draws one element of the hangman diagram as a tally mark."
                        + "\n"
                        + "\nThe game is over when:"
                        + "\nThe guessing player completes the word, or guesses the whole word correctly"
                        + "\nThe other player completes the diagram";
                JOptionPane.showMessageDialog(null, message, "Help",
                        JOptionPane.INFORMATION_MESSAGE);
            }
        });

        JButton btnExit = new JButton("Exit");
        btnExit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        JLabel lblWord = new JLabel();

        ImageIcon icon = new ImageIcon(
                "D:\\Varsity College\\Prog212Assign1_10-013803\\images\\Hangman1.jpg");
        JLabel label = new JLabel();
        label.setIcon(icon);
        String b[] = { "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" };
        for (i = 0; i < buttons.length; i++) {
            buttons[i] = new JButton(b[i]);

            panel.add(buttons[i]);
        }

        panel2.add(label);

        panel3.add(btnRestart);
        panel3.add(btnNewWord);
        panel3.add(btnHelp);
        panel3.add(btnExit);

        panel4.add(lblWord);
    }

    public void readWord() {
        try {
            BufferedReader reader = new BufferedReader(new FileReader(
                    "Words.txt"));
            String line = reader.readLine();
            List<String> words = new ArrayList<String>();
            while (line != null) {
                String[] wordsLine = line.split(" ");
                for (String word : wordsLine) {
                    words.add(word);
                }
                line = reader.readLine();
            }
            Random rand = new Random(System.currentTimeMillis());
            String randomWord = words.get(rand.nextInt(words.size()));
        } catch (Exception e) {
        }
    }

    public static void main(String[] args) {
        Hangman frame = new Hangman();
        Box mainPanel = Box.createVerticalBox();
        frame.setContentPane(mainPanel);
        mainPanel.add(panel, BorderLayout.NORTH);
        mainPanel.add(panel2);
        mainPanel.add(panel4);
        mainPanel.add(panel3);
        frame.pack();
        frame.setVisible(true);
    }

    public void actionPerformed(ActionEvent e) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
4

4 回答 4

15

ListJava中调用了2个类。有java.util.Listjava.awt.List。确保您不会意外混合它们。我看到你正在导入java.awt.*,所以 Java 认为你想要 java.awt.List,但你想要java.util.List让它编译的版本。最简单的方法是完全限定List有问题的陈述中的:

java.util.List<String> words = new ArrayList<String>();
于 2012-08-20T14:34:14.647 回答
5

你的问题在于

import java.awt.*;

java.awt包包含一个名为 的类List,就像 一样java.util,但这不是List您想要的。您正在尝试导入它们,这会导致干扰。任何一个:

  • import java.awt.*;为您使用的特定类删除和添加导入,或者
  • 将列表引用限定为java.util.List<String>
  • ArrayList<String>直接用就行
于 2012-08-20T14:35:52.703 回答
2

您应该明确导入 java.util.List

于 2012-08-20T14:37:59.093 回答
1

错误说明了一切:The type List is ambiguous

您调用两个不同的类:

import java.util.ArrayList;

import java.awt.List;

那是由import java.awt.*;

您必须为两个List实例使用相同的类。

例如:

import java.util.ArrayList;
import java.util.List;

或避免使用import java.awt.*;,而是使用

import java.awt.BorderLayout;
import java.awt.GridLayout;

ArrayList<String>直接使用:

ArrayList<String> words = new ArrayList<String>();
于 2012-08-20T14:35:26.230 回答