Java JFrame 更新不起作用



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

package com.boldchicky.commandsrpg.Main;
import java.io.*;
public class Engine {
public static boolean running;
public static GenerateWorld gw;
public static void main(String[] args) throws IOException{
    gw = new GenerateWorld();
    BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
    running = true;
    while(running){
        System.out.println("Enter a command:");
        String command = input.readLine();
        if(command.equalsIgnoreCase("move up")){
            gw.update(gw.getGraphics());
            GenerateWorld.x += GenerateWorld.dx;
            System.out.println("Moved!");
        } else {
            System.out.println(CommandControl.command(command));
        }
        if(CommandControl.add){
            String command2 = input.readLine();
            System.out.println(CommandControl.add(command2));
            CommandControl.add = false;
        }
        else if(CommandControl.remove){
            String command2 = input.readLine();
            System.out.println(CommandControl.remove(command2));
            CommandControl.remove = false;
        }
    }
    if(!running){
        System.exit(0);
    }
}
}

命令控制.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;
}
}

生成世界.java:

package com.boldchicky.commandsrpg.Main;
import java.awt.*;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.image.*;
import java.io.File;
import java.io.IOException;
public class GenerateWorld extends JFrame{
public static int x = 0, y = 0, dx = 15, dy = 15;
public GenerateWorld() throws IOException{
    JFrame frame = new JFrame("Frame");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(300, 300);
    Image background = null;
    Image player = null;
    String backgroundPath = "C:\Users\Jesse Du\Documents\java eclipse\CommandsRPG\res\grass.png";
    String playerPath = "C:\Users\Jesse Du\Documents\java eclipse\CommandsRPG\res\player.png";
    background = ImageIO.read(new File(backgroundPath));
    player = ImageIO.read(new File(playerPath));
    ImagePanel panel = new ImagePanel(background, player);
    frame.getContentPane().add(panel);
    frame.show();
    frame.setLocationRelativeTo(getRootPane());
    frame.setResizable(false);
    frame.setVisible(true);
}
}

图像面板.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 是创建、设计和显示的。 不要创建本地 JFrame,而是使用 super 构造函数并将方法应用于超级实例。

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

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

最新更新