Java 如何在switch语句中执行操作?

Java 如何在switch语句中执行操作?,java,swing,user-interface,awt,Java,Swing,User Interface,Awt,我正在尝试使用基于命令提示符的GUI(即键入命令并单击enter按钮)制作一个简单的程序。每当用户单击enter按钮时,他们在JTextArea中键入的所有信息都会被处理,然后根据他们的输入给出输出(例如键入/退出以关闭程序) 每当用户键入in/exit时,就会出现一个提示,询问用户是否确实要退出。然而,一旦弹出,如果用户输入了一些无法识别的内容,它就会被我输入的无效输入消息所取代。关于为什么会发生这种情况,我唯一的理由是ActionListener类仍然将/exit存储在inputHolder

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

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

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语句中执行另一个操作,但我不确定如何执行,或者这是否是最好的解决方案。我欢迎任何回答和反馈,只是请不要有毒

当你这样做的时候

output.setText("Are you sure you want to terminate this session?");
inputHolder = commandLine.getText();
用户尚未更新
命令行
并按下按钮,因此没有任何更改。您需要“等待”用户再次响应

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

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;
        } 
    }  
}

如果您想在一个流程中执行它,那么您应该考虑使用<代码> joptPANGE< /Code >来提示用户而不是

问题是,您的ActoPetri中的代码完全执行,用户无法进入“是”或“否”,并单击“输入”按钮。内部开关在inputHolder具有值“/exit”的情况下执行,该值导致默认情况

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

下面的代码试图接近您的原始想法

    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;
                }  

        }
    }

inputHolder
不会再次处理输入,直到用户再次按下
inputButton
(确认请求)。因此,您需要将
/exit
请求作为一个过程进行处理,用户输入的响应作为第二个过程进行处理,可能需要使用状态值来维护上下文。我如何使用JOptionPane进行处理?我对这个很陌生