Java 文本冒险游戏 swing 组件未正确更新,每个函数运行时枚举被选择两次

标签 java swing enums jtextpane

我对我编写的代码中出现的错误很感兴趣。它在 C# 中工作得很好,但在 Java 中却不起作用。不知道和我第二次设置有没有关系。我已经与将按钮传递到此类的其他类进行了检查,发现没有任何内容会影响此类。另一件需要注意的事情是,所有 Swing 组件在传递给此类时都不为 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

最佳答案

您似乎在代码的逻辑部分中将 ActionListener 添加到按钮,并且这样做意味着每当 processDecision() 方法运行时,按钮都会多次添加 ActionListener调用,最终导致按钮添加了多个监听器,这不是您想要的。监听器应在组件创建时添加一次。

或者,如果您想要交换 ActionListener,请务必在替换为新监听器时删除旧监听器。

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

我发现的一个大问题是您的代码具有非常高的耦合性和低内聚性,导致代码非常脆弱并且难以调试。考虑重构,将模型(程序的逻辑部分)与 View (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# 中使用的不同。变量名应全部以小写字母开头,而类名应以大写字母开头。学习并遵循这一点将使我们更好地理解您的代码,并且将使您更好地理解其他人的代码。

关于Java 文本冒险游戏 swing 组件未正确更新,每个函数运行时枚举被选择两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46512538/

相关文章:

java - 错误: operator does not exist: character varying = bytea

java - 运行 mvn package 时出现 NoClassDefFoundError ArchiverException

java - 仅显示没有图标的 JComboBox 所选项目文本?

java - 如何在java中做一个数字键盘

swift - 在 Swift 中传递并打印枚举中的所有情况

c++ - 如果我在静态类中创建一个枚举,会有什么不同吗?

java - 在 Java 中从 Excel xlsx 转换为 xls

java - 必需的字符串参数 'email' 不存在

java - 如何使 actionListener 方法动态化

c - 标志,枚举 (C)