Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.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
使用JavaSwing从另一个类调用方法以利用当前类_Java_Swing_User Interface_Drawing - Fatal编程技术网

使用JavaSwing从另一个类调用方法以利用当前类

使用JavaSwing从另一个类调用方法以利用当前类,java,swing,user-interface,drawing,Java,Swing,User Interface,Drawing,我目前正在做一个刽子手游戏。游戏使用一个驱动程序,接收一个单词文本文件,选择一个随机单词并将其发送到我的ImageBuilder类,该类创建一个带有文本框和按钮的gui,用于提交字母和玩游戏 如果用户输入了错误的字母,它将对my DrawArea类进行方法调用,将布尔值更改为true,这将触发绘图显示在面板上 我在想怎么做时遇到了一些问题,教授说创建一个实例,设置大小并将其添加到ImageBuilder类中我的主要gui creator方法的面板中。根据她的想法,我得到了一个空指针异常,因为我猜

我目前正在做一个刽子手游戏。游戏使用一个驱动程序,接收一个单词文本文件,选择一个随机单词并将其发送到我的ImageBuilder类,该类创建一个带有文本框和按钮的gui,用于提交字母和玩游戏

如果用户输入了错误的字母,它将对my DrawArea类进行方法调用,将布尔值更改为true,这将触发绘图显示在面板上

我在想怎么做时遇到了一些问题,教授说创建一个实例,设置大小并将其添加到ImageBuilder类中我的主要gui creator方法的面板中。根据她的想法,我得到了一个空指针异常,因为我猜它找不到实例

我尝试在调用图形的方法中创建实例,但也没有显示任何内容。我的画还不完整,但我只是想在这一点上展示一些东西

在此方面的任何帮助都将不胜感激

这是我的密码:

Game.java:

import java.util.*;
import java.io.*;


public class Game {
    /** The main program will perform the following steps
    * 1. Take in a text file
    * 2. Read through the text file and select a random word
    * 3. Create an instance of ImageBuilder and pass the random word through
    */


    public static void main(String[] args) throws FileNotFoundException {
        ImageBuilder n = new ImageBuilder(grabWord(args[0]));

    }
    /**
     * Opens a file and parses the formatted file
     *  reads on each line of the file and splits the string into an array
     *  grabs the ages of the entries and sums them to print to screen
     * @param fileName the path to input file
     *
     */
    private static String grabWord(String fileName)throws FileNotFoundException {
        int sum = 0;
        ArrayList<String> words = new ArrayList<>();
        Scanner fileScan = new Scanner(new File(fileName));
        while(fileScan.hasNextLine()){
            sum++;
            String line = fileScan.nextLine();
            words.add(line);
        }
        Random rand = new Random();
        int randNum = rand.nextInt(sum);
        System.out.println(randNum);
        String test = words.get(randNum-1);
        System.out.println(test);
        return (test);
    }
}

您正在隐藏您的变量:

public class ImageBuilder extends JPanel {
    //...
    private DrawArea draw;

    public ImageBuilder(String word){
        DrawArea draw = new DrawArea();
您已将
draw
定义为
ImageBuilder
的实例字段,但在构造函数中,您已将
draw
定义为局部变量,只能从构造函数的上下文中访问。这意味着您的实例字段为
null

删除本地声明,例如:

public class ImageBuilder extends JPanel {
    //...
    private DrawArea draw;

    public ImageBuilder(String word){
        draw = new DrawArea();
您使用的
null
布局将导致无止境的问题。我强烈建议您花时间更好地理解布局API并利用它。有关更多详细信息,请参阅

现在图形将不显示。有什么意见吗

正如我所说,
null
layouts会再次困扰你

private void createAndShowGui(){
//...

//这么简单,我的天啊。我能让它工作,现在图形不会显示。有什么输入吗?我取消了变量的阴影,仍然无法让图形显示。这方面的任何帮助都将是致命的。
public class ImageBuilder extends JPanel {
    //...
    private DrawArea draw;

    public ImageBuilder(String word){
        DrawArea draw = new DrawArea();
public class ImageBuilder extends JPanel {
    //...
    private DrawArea draw;

    public ImageBuilder(String word){
        draw = new DrawArea();