Wie kann ich in einer Switch -Anweisung eine Action -Performance erstellen?
Posted: 31 Aug 2025, 13:01
Ich versuche, ein einfaches Programm mit einer GUI zu erstellen, die mit dem Befehl prompt basiert (d. H. Einen Befehl eingeben und auf eine Eingabetaste klicken). Wenn der Benutzer auf die Schaltfläche Eingabetaste klickt, werden alle Informationen, die er in einer jTextArea eingegeben hat, verarbeitet. Anschließend wird eine Ausgabe auf der Grundlage ihrer Eingabe angegeben (z. B. eingeben /beenden, um das Programm zu schließen). Sobald das auftaucht, wird es jedoch durch die ungültige Eingangsnachricht ersetzt, die ich eingefügt habe, wenn der Benutzer etwas, das nicht erkannt wurde, eingesetzt wurde. Meine einzige Gründe, warum dies geschieht, ist, dass die ActionListener -Klasse immer noch in der Inputholder -Zeichenfolge gespeichert /beendet ist. Dies ist die Zeichenfolge, mit der ich die Eingänge des Benutzers speichere und sie verarbeiten. < /P>
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();
}
}
< /code>
Derzeit mein einziger Denkprozess, wie man dies lösen soll>
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();
}
}
< /code>
Derzeit mein einziger Denkprozess, wie man dies lösen soll>