Ich möchte gerne ein kleines Text Adventure, in dem man nur vorerst ja und nein in einem Textfeld eingeben kann, erstellen. Momentan füge ich für jede neue Abfrage einen neuen Actionlistener für ein und dasselbe Textfeld ein. Gibt es da eine elegantere Variante nur einen Actionlistener zu verwenden?
Meine Klasse Oberflaeche:
LG LordSpiegel
Meine Klasse Oberflaeche:
Java:
package textadventure;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextPane;
import javax.swing.JTextField;
public class Oberflaeche extends JFrame {
private String antwortString;
private JPanel contentPane;
private JTextField antwort;
private JTextPane adventureText;
/**
* Create the frame.
*/
public Oberflaeche() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 800, 500);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
adventureText = new JTextPane();
adventureText.setEditable(false);
adventureText.setBounds(12, 13, 758, 255);
contentPane.add(adventureText);
antwort = new JTextField();
antwort.setBounds(12, 281, 758, 22);
contentPane.add(antwort);
antwort.setColumns(10);
adventureText.setText(
"Hallo Abenteurer. Erlebe nun ein kleines Abenteuer,du verwendest dabei nur die Buchstaben: 'y' und 'n'.Wenn du aufgefordert wirst entscheide dich zwichen zwei Auswahlmöglichkeiten. Tippe nun y oder n in das untere Textfeld.");
antwort.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
antwortString = antwort.getText();
if (antwortString.equals("y") || antwortString.equals("n")) {
antwort.setText(null);
beginn();
}
}
});
}
public void beginn() {
adventureText.setText("Möchtest du das Abenteuer beginnen?\ny oder n ?");
antwort.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent f) {
antwortString = antwort.getText();
if (antwortString.equals("y")) {
antwort.setText(null);
beginningGood();
} else if (antwortString.equals("n")) {
antwort.setText(null);
keinAbenteurer();
}
}
});
}
public void beginningGood() {
adventureText.setText("Dann lasst das Abenteuer beginnen!");
}
public void keinAbenteurer() {
adventureQ();
antwort.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent f) {
antwortString = antwort.getText();
if (antwortString.equals("y")) {
antwort.setText(null);
beginningGood();
} else if (antwortString.equals("n")) {
antwort.setText(null);
adventureQ();
antwort.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent g) {
antwortString = antwort.getText();
if (antwortString.equals("y")) {
antwort.setText(null);
beginningGood();
} else if (antwortString.equals("n")) {
antwort.setText(null);
adventureQ();
antwort.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent h) {
antwortString = antwort.getText();
if (antwortString.equals("y")) {
antwort.setText(null);
beginningGood();
} else if (antwortString.equals("n")) {
antwort.setText(null);
adventureText.setText("Du bist hartnäckig... Ich aber auch! Möchtest du beginnen?\ny oder n");
antwort.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent i) {
antwortString = antwort.getText();
if (antwortString.equals("y")) {
antwort.setText(null);
beginningGood();// anderes ende mit verspätung. ist schon abend.
} else if (antwortString.equals("n")) {
antwort.setText(null);
adventureText.setText("... ... ... du bist kein Abenteurer oder? ");
}
}
});
}
}
});
}
}
});
}
}
});
}
public void adventureQ() {
adventureText.setText("Möchtest du das Abenteuer beginnen?\ny oder n ?");
}
}
LG LordSpiegel