Java 文字冒险游戏摇摆组件未正确更新,并且每个函数都选择了两次枚举 运行

Java text-adventure game swing components not updating correctly and enum gets selected twice per function run

我对我编写的代码中出现的错误很感兴趣。它在 C# 中完美运行,但不知何故在 Java 中不起作用。不知道是不是和我第二次设置有关系。我已经用我的其他 class 将按钮传递给此 class 进行了检查,但我发现没有任何东西会影响此 class。另一件需要注意的事情是,所有 Swing 组件在传递到此 class.

时都不是 null

我的主要问题是在设置 时间的文本后所有按钮都没有更新。

public class StoryManager extends Text{
    // this is the body of the story
    private JTextPane textPane;
    // the choices that the user can make
    private JButton btnChoice1;
    // this would determine which ending the user will get
    private short ending = 0;

/**
 * checkpoint for each part of the story
 * */
public enum Part{
    START, A_JOKE, B_BASE
}
// the part that the user is currently in
Part userPart;

/**
 * This runs the story.
 * */
public void PlayStory() {
    printText();
    processDecision();
}
/**
 * This attaches the buttons and the textPane to their corresponding variables in the class.
 * */
public StoryManager(WindowHandler windowHandler) {
    // pane
    textPane = windowHandler.getTextPane();
    // buttons
    btnChoice1 = windowHandler.getBtnChoice1();
    // set the user to start the story from the beginning
    userPart = Part.START;
}

@Override
public void printText() {
    switch(userPart) {
    case START:
        System.out.println("Start event started");
        textPane.setText("some text"
                + "[1] option 1\n");
        break;
    case A_JOKE:
        System.out.println("A_JOKE event started");
        textPane.setText("another text 1"
                + "[1] option 1\n");
        break;
    case B_BASE:
        System.out.println("B_BASE event started");
        textPane.setText("another text 2"
                + "[1] option 1\n");

        break;
    default:
    }
    textPane.setCaretPosition(0);
    textPane.repaint();
}
/**
 * Enables all the buttons.
 * */
private void EnableAllButtons() {
    if(!btnChoice1.isEnabled()) {
        btnChoice1.setEnabled(true);
        btnChoice1.setOpaque(true);
        btnChoice1.setContentAreaFilled(true);
        btnChoice1.setBorderPainted(true);
    }
    btnChoice1.repaint();
    //System.out.println("Button1: "+btnChoice1.isEnabled());
}
/**
 * Temporarily disables the other buttons and runs the PlayStory() again to continue the story.
 * 
 * @param button The button that is clicked.
 * */
private void SetDecisionText(JButton button) {
    button.setText("Next");
    btnChoice1.revalidate();
    PlayStory();
}
/**
 * Sets the action of the button per part.
 * */
@Override
public void processDecision() {
    switch(userPart) {
    case START:
        btnChoice1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                ending -= 10;
                userPart = Part.A_JOKE;
                SetDecisionText(btnChoice1);
            }
        });
        break;
    case A_JOKE:
        btnChoice1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                btnChoice1.setText("Choice 1");
                //btnChoice1.revalidate(); <- tried this but it isn't working
                GoToBase();
            }
        });
        break;
    case B_BASE:
        System.out.println("Updated with B_BASE");
        btnChoice1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                ending -= 10;
                userPart = Part.B_ARGUE;
                SetDecisionText(btnChoice1);
            }
        });
    }

}
private void GoToBase() {
    EnableAllButtons();
    switch(userPart) {
    case A_JOKE:
        userPart = Part.B_BASE;
        System.out.println("GotoBase: " + userPart);
        break;
    default:
        System.out.println("Error");
    }
    PlayStory();
}

}

我将在这里分享我的程序的输出。我选择了第一个按钮两次然后发生了这种情况。当我再次按下它时,userPart 变为 null 或其他东西。

// pressed play
Start event started

// click 1st button
A_JOKE event started

// click 1st button again
GotoBase: B_BASE
B_BASE event started
Updated with B_BASE
A_JOKE event started

// click 1st button for the third time
GotoBase: B_BASE
B_BASE event started
Updated with B_BASE
Error
A_JOKE event started

您似乎在代码的逻辑部分将 ActionListeners 添加到按钮,而您这样做意味着按钮将多次添加 ActionListeners,无论何时调用 processDecision() 方法,结果最终在添加了多个侦听器的按钮中,这不是您想要的。监听器应该在创建组件时添加一次。

或者,如果您打算交换 ActionListener,请务必在替换为新的侦听器时移除旧的侦听器。

您的代码中可能存在其他逻辑问题,但在您能够为我们提供有效的 Minimal, Complete, and Verifiable Example Program 之前,这是我可以建议的限制。这是您将代码压缩为仍然可以编译和运行的最小位的地方,没有外部依赖性(例如需要 link 到数据库或图像),没有与您的问题无关的额外代码,但是仍然证明你的问题。

我看到的一个大问题是你的代码耦合度很高,内聚度很低,导致代码非常脆弱,很难调试。考虑重构,将模型(程序的逻辑部分)与视图(GUI 部分)分离,并将数据与代码分离,例如摆脱硬编码的显示文本,并将其放入数据存储库中,无论是文本文件、数据库还是任何最有效的文件。

删除侦听器的示例:

case A_JOKE:
    for (ActionListener l : btnChoice1.getActionListeners()) {
        btnChoice1.removeActionListener(l);
    }
    btnChoice1.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent arg0) {
            btnChoice1.setText("Choice 1");
            // btnChoice1.revalidate(); <- tried this but it isn't
            // working
            GoToBase();
        }
    });
    break;

顺便说一句,您需要学习和使用 Java naming conventions,这与用于 C# 的不同。变量名称应全部以小写字母开头,而 class 名称应以大写字母开头。了解这一点并遵循这一点将使我们能够更好地理解您的代码,并使您能够更好地理解其他人的代码。