首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在switch语句中创建actionPerformed?

问如何在switch语句中创建actionPerformed?
EN

Stack Overflow用户
提问于 2019-10-17 10:58:21
回答 2查看 47关注 0票数 0

我正在尝试用基于命令提示符的GUI创建一个简单的程序(即输入命令并单击enter按钮)。每当用户单击enter按钮时,都会处理他们在JTextArea中键入的所有信息,然后根据他们的输入(例如键入/exit关闭程序)给出一个输出。

每当用户在/exit中键入时,都会出现一个提示,询问用户是否确定要退出。但是,一旦弹出,如果用户输入了未被识别的内容,它就会被我输入的无效输入消息所取代。发生这种情况的唯一原因是,ActionListener类仍然将/exit存储在inputHolder字符串中,这是我用来存储用户输入并对其进行处理的字符串。

代码语言:javascript
复制
package msai;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MainFrame extends JFrame implements ActionListener {
    JTextArea output;
    JButton inputButton;
    JTextArea commandLine;
    String inputHolder;
    String invalid = "Invalid input!";

    public MainFrame() {
        super("MSAI");
        setLookAndFeel();
        setSize(650, 350);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        FlowLayout flo = new FlowLayout();
        setLayout(flo);

        output = new JTextArea("Hi! I'm MSAI. Type any command to get started, or type /help for a list of commands and syntax!", 8, 40);
        inputButton = new JButton("ENTER");
        inputButton.addActionListener(this);
        commandLine = new JTextArea(8, 40);
        commandLine.setLineWrap(true);
        commandLine.setWrapStyleWord(true);
        JScrollPane scroll = new JScrollPane(commandLine, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

        add(output);
        add(inputButton);
        add(scroll);

        setVisible(true);

    }

    public void actionPerformed(ActionEvent event) {

        inputHolder = commandLine.getText();
        switch(inputHolder) {
            "/exit":
                output.setText("Are you sure you want to terminate this session?");
                inputHolder = commandLine.getText();
                switch(inputHolder.toUpperCase()) {
                    case "YES":
                        System.exit(0);
                        break;
                    case "NO":
                        output.setText("Your session has not been terminated.");
                        break;
                    default:
                        output.setText(invalid);
                        break;
            }
        } 
    }  
    private void setLookAndFeel() {
        try {
            UIManager.setLookAndFeel(
            "javax.swing.plaf.nimbus.NimbusLookAndFeel"
            );
        } catch (Exception exc) {
            //ignore error
        }
    }


    public static void main(String[] args) {
        MainFrame frame = new MainFrame();
    }
}

目前,我唯一想到的解决方法就是在/exit switch语句中放入另一个actionPerformed,但我不确定如何做到这一点,或者这是否是最好的解决方案。我欢迎任何答案和反馈,只是请不要有毒!

EN

回答 2

Stack Overflow用户

发布于 2019-10-17 11:16:20

当你这样做的时候

代码语言:javascript
复制
output.setText("Are you sure you want to terminate this session?");
inputHolder = commandLine.getText();

用户尚未更新commandLine并按下按钮,因此没有任何变化。您需要“等待”,直到用户再次响应。

您需要将请求的“状态”带到下一次调用ActionPerformed时,例如...

代码语言:javascript
复制
private boolean wantsToExit = false
public void actionPerformed(ActionEvent event) {

    inputHolder = commandLine.getText();

    if (wantsToExit) {
        if (wantsToExit && inputHolder.equals("YES")) {
            // Better to close the frame and let the system deal with it
        } else if (wantsToExit && inputHolder.equals("NO")) {
            output.setText("Your session has not been terminated.");
        }
        wantsToExit = false;
    } else {
        switch(inputHolder) {
            "/exit":
                wantsToExit = true;
                output.setText("Are you sure you want to terminate this session?");
                break;
        } 
    }  
}

如果您希望在单个流中执行此操作,则应考虑使用JOptionPane来提示用户

票数 0
EN

Stack Overflow用户

发布于 2019-10-17 11:45:20

问题是actionPeformed中的代码是完全执行的,用户无法输入是或否,然后单击"ENTER“按钮。内部开关使用值为"/exit“的inputHolder执行,这导致了默认情况。

您需要一种方法来区分这些操作。您可以使用标志来指示用户是否已发出退出命令。如果标志为true,则执行内部开关。否则,从该方法返回。

这里的代码试图接近你最初的想法。

代码语言:javascript
复制
    boolean exitCommand = false;  //has the user issued exit command?


    public void actionPerformed(ActionEvent event) {
        inputHolder = commandLine.getText();
        if (exitCommand){
            switch (inputHolder.toUpperCase()) {
                    case "YES":
                        System.exit(0);
                        break;
                    case "NO":
                        exitCommand = false; //forget exit command.
                        output.setText("Your session has not been terminated.");
                        break;
                    default:
                        output.setText(invalid);
                        break;
                }
        }


        switch (inputHolder) {
            case "/exit": 
                exitCommand = true;
                commandLine.setText("");
                output.setText("Are you sure you want to terminate this session?");
                inputHolder = commandLine.getText();
                if (!exitCommand) {
                    exitCommand = true;
                    return;
                }  

        }
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58424244

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档