Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/381.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 JFrame更新不工作_Java_Swing_Jframe - Fatal编程技术网

Java JFrame更新不工作

Java JFrame更新不工作,java,swing,jframe,Java,Swing,Jframe,我的Java项目需要一些真正的帮助。它是一个控制台控制的JFrame,您在控制台中键入的命令应该对JFrame窗口作出反应,只是它没有。这是一个大问题。以下是我的代码(Engine.java): CommandControl.java: package com.boldchicky.commandsrpg.Main; import java.util.ArrayList; import java.io.*; public class CommandControl { static Array

我的Java项目需要一些真正的帮助。它是一个控制台控制的JFrame,您在控制台中键入的命令应该对JFrame窗口作出反应,只是它没有。这是一个大问题。以下是我的代码(Engine.java):

CommandControl.java:

package com.boldchicky.commandsrpg.Main;

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

public class CommandControl {
static ArrayList inv = new ArrayList();
public static boolean add = false;
public static boolean remove = false;
public static boolean move = false;
public static String commandReact = "";

public static String command(String command){
    BufferedReader input = new BufferedReader(new        InputStreamReader(System.in));
    if(command.equalsIgnoreCase("inventory")){
        if(inv.size() == 0){
            commandReact = "Empty!";
        }  else {
            commandReact = "";
            for(int i = 0; i<inv.size(); i++){
                commandReact += (String) inv.get(i) + "\n";
            }
        }
    }

    else if(command.equalsIgnoreCase("add")){
        add = true;
        commandReact = "What do you want to add?";
    }
    else if(command.equalsIgnoreCase("remove")){
        remove = true;
        commandReact = "Waht do you want to remove from your inventory?";
    }
    else{
        commandReact = "Please try again!";
    }
    return commandReact;
}

public static String add(String command){
    if(add){
        inv.add(command);
        commandReact = "Added!";
    }
    return commandReact;
}

public static String remove(String command){
    if(remove){
        if(inv.indexOf(command) != -1){
            inv.remove(inv.indexOf(command));
            commandReact = "Removed!";
        } else {
            commandReact = "Sorry! There is no " + command;
        }
    }
    return commandReact;
}
}
ImagePanel.java:

package com.boldchicky.commandsrpg.Main;

import java.awt.*;

import javax.swing.JPanel;

public class ImagePanel extends JPanel{
private Image backgroundImage;
private Image player;

public ImagePanel(Image background, Image player){
    super();
    this.backgroundImage = background;
    this.player = player;
}

public void paintComponent(Graphics g){
    super.paintComponent(g);
    g.drawImage(this.backgroundImage, 0, 0, null);
    g.drawImage(this.player, GenerateWorld.x, GenerateWorld.y, null);
}
}

谢谢D

您的
gw
对象,尽管是JFrame的实例,但并不是您正在显示的JFrame,因为在
GenerateWorld
构造函数中,会创建、设计并显示构造函数本身的本地JFrame。使用
super
构造函数并将方法应用于super实例,而不是创建本地JFrame

public GenerateWorld() throws IOException {
    super("Frame");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    // ... etc

此外,在这种情况下,您应该使用
repaint()
,而不是
update(Graphics)
,并且应该在修改后而不是之前调用它。

这可能是一个愚蠢的问题,但是我应该在哪里初始化帧变量呢?GenerateWorld是“frame”变量,它是通过调用
super来实例化的(“帧”)
package com.boldchicky.commandsrpg.Main;

import java.awt.*;

import javax.swing.JPanel;

public class ImagePanel extends JPanel{
private Image backgroundImage;
private Image player;

public ImagePanel(Image background, Image player){
    super();
    this.backgroundImage = background;
    this.player = player;
}

public void paintComponent(Graphics g){
    super.paintComponent(g);
    g.drawImage(this.backgroundImage, 0, 0, null);
    g.drawImage(this.player, GenerateWorld.x, GenerateWorld.y, null);
}
}
public GenerateWorld() throws IOException {
    super("Frame");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    // ... etc