Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/368.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java和GUI有问题吗_Java - Fatal编程技术网

Java和GUI有问题吗

Java和GUI有问题吗,java,Java,使代码正常工作时遇到问题。我无法在GUI中显示字符数组(myArray)。当它正常工作时,我必须显示数组,如果下一步使用的是字母,我必须重新显示该数组,使使用的字母消失。继续此操作,直到字母数组为空。我将作业说明的内容放在引号中。我目前正在使用Eclipse 开发游戏-基于“Wordmole” 向用户显示字母网格(阅读 从文件中)。让用户输入一个 word使用输入对话框。如果 输入的单词是有效的,然后 检查单词的所有字母是否正确 显示在网格中。否则,请使用消息 对话框以显示错误消息。如果 这个词

使代码正常工作时遇到问题。我无法在GUI中显示字符数组(myArray)。当它正常工作时,我必须显示数组,如果下一步使用的是字母,我必须重新显示该数组,使使用的字母消失。继续此操作,直到字母数组为空。我将作业说明的内容放在引号中。我目前正在使用Eclipse

开发游戏-基于“Wordmole” 向用户显示字母网格(阅读 从文件中)。让用户输入一个 word使用输入对话框。如果 输入的单词是有效的,然后 检查单词的所有字母是否正确 显示在网格中。否则,请使用消息 对话框以显示错误消息。如果 这个词的所有字母都在字母表中 网格,然后奖励玩家 单词的适当点,以及 从网格中删除字母。其他的 使用消息对话框告诉用户 这个词不存在。让球员 继续播放,直到用户退出 通过不输入单词或 单击取消。网格没有 里面有更多的字母

导入java.awt.GridLayout;
//导入java.util.StringTokenizer;
导入javax.swing.JFrame;
导入javax.swing.JOptionPane;
导入javax.swing.JScrollPane;
导入javax.swing.JTextArea;
公共类项目2{
公共静态字符串输入字;
公共静态字符串矩阵=“Words\n\n”;
公共静态字符串total=“total\n\n”;
公共静态字符串新词;
公共静态字符[][]myArray;
公共静态void main(字符串[]args){
myArray=fillArray(“project2a.txt”);
while(true){
整数计数=0;
整数和=0;
inputWord=JOptionPane.showInputDialog(null,“输入一个单词:”);
如果(inputWord==null | | inputWord.length()==0)中断;
newWord=inputWord.toUpperCase();
if(IsValidWord(新词)){
对于(int x=0;x
JTextComponent setText在传递多维字符数组时获取字符串。我不完全确定您试图做什么,但请尝试在显示之前想出一种方法将其转换为字符串。记住String类有一个接受字符数组的构造函数;)

好的,如果你看t,那么你试图用数组初始化一个textArea
import java.awt.GridLayout;
//import java.util.StringTokenizer;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class Project2 {
    public static String inputWord;
    public static String matrix = "Words\n\n";
    public static String total = "Total\n\n";
    public static String newWord;
    public static char[][] myArray; 

public static void main(String[] args){ 

    myArray = fillArray("project2a.txt");


    while(true){
        int count = 0;
        int sum = 0;
        inputWord = JOptionPane.showInputDialog(null,"Enter a word:");
        if(inputWord == null || inputWord.length() == 0) break;
        newWord = inputWord.toUpperCase();

        if(IsValidWord(newWord)){
            for(int x = 0; x < newWord.length(); x++){

                for(int i = 0; i < 10; i ++){
                    for(int j = 0; j < 10; j++){
                        if(newWord.charAt(x) == myArray[i][j]) count++;
                    }
                }
            }

            if(count == newWord.length()){
                for(int c = 0; c < newWord.length(); c++)
                    sum += scoreValue(newWord.charAt(c)); 

                for(int x = 0; x < newWord.length(); x++){

                    for(int i = 0; i < 10; i ++){
                        for(int j = 0; j < 10; j++){
                            if(newWord.charAt(x) == myArray[i][j]) myArray[i][j] = (' ');
                        }
                    }   
                }
                matrix = matrix + newWord + "\n";
                total = total + sum + "\n";
            }
            else
                JOptionPane.showMessageDialog(null,"The word is not there!!!");




            } // IF

        else
            JOptionPane.showMessageDialog(null,"ERROR: The word is invalid!!!");

        } //while
    createAndShowGUI();
}

public static char[][] fillArray(String myFile){
    TextFileInput tfi = new TextFileInput(myFile);
    String line = tfi.readLine();
    System.out.println(line);
    int rows, cols;
    rows = Integer.parseInt(line);
    cols = Integer.parseInt(line);

    char[][] rtn = new char[rows][cols];

    for(int i=0; i < rows; i++) {
        line = tfi.readLine();
        System.out.println(line);
        for(int j= 0; j < cols; j++) {
            rtn[i][j] = line.charAt(j);

        } // For
    } // For
    return rtn;
} // FillArray

public static boolean IsValidWord(String myWord){
    for(int i = 0; i < myWord.length(); i++)
        if(!Character.isLetter(myWord.charAt(i))) return false;
    return true;
} // IsValidWord

public static int scoreValue(char letters){
    if(Character.isLetter(letters)){
        if(letters == 'A' || letters == 'E' || letters == 'I' || letters == 'O' || letters == 'U') 
            return 0;
        else if(letters == 'K' || letters == 'V' || letters == 'F' || letters == 'W') return 5;
        else if(letters == 'X' || letters == 'Q') return 10;
        } // If statement
    return 1;

    } // scoreValue

public static void createAndShowGUI() {
    //Create and set up the window.
    JFrame frame = new JFrame("Word Game");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize( 100,100);//width, height);
    frame.setLocation(200,200);//x, y);
    frame.setLayout(new GridLayout(1,2));

    JTextArea textArea = new JTextArea(5, 20);
    JTextArea textArea1 = new JTextArea(5, 20);
    textArea.setEditable(false);
    textArea1.setEditable(false);
    JScrollPane scrollPane = new JScrollPane(textArea);
    JScrollPane scrollPane2 = new JScrollPane(textArea1);
    frame.getContentPane().add(scrollPane);
    frame.getContentPane().add(scrollPane2);

    textArea.setText(matrix);
    textArea1.setText(total);

    //Display the window.
    frame.pack();
    frame.setVisible(true);


}// createAndShowGUI

public static void createAndShowGUI2() {
    //Create and set up the window.
    JFrame frame = new JFrame("Letters");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize( 100,100);//width, height);
    frame.setLocation(200,200);//x, y);
    frame.setLayout(new GridLayout(1,2));

    JTextArea textArea = new JTextArea(5, 20);
    textArea.setEditable(false);
    JScrollPane scrollPane = new JScrollPane(textArea);
    frame.getContentPane().add(scrollPane);

    textArea.setText(myArray);


    //Display the window.
    frame.pack();
    frame.setVisible(true);


}// createAndShowGUI
}