0

The program wil do the text = invoertextbox.getText(); and invoertextbox.setText(""); but it doesnt want to open the switcher or default. anyone a suggestion? it doesnt show the messagebox either.

import java.awt.event.*;
import javax.swing.JTextField;
import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.awt.Toolkit;
import java.util.Timer;
import java.util.TimerTask;

public class Paneel extends JPanel {

    private static final long serialVersionUID = 1L;
    String text;
    String AccountName = "default";
    String autosavecheck = "";
    int level = 0;
    String iss;
    JLabel monsterlabel, progresslabel, commandslabel, label1;
    JProgressBar monsterbar, progressbar;
    JButton sendknop, clearknop, creditsknop, saveknop, loadknop, restartknop,
            disableautosaveknop;
    JTextField commandstextbox, naamtextbox, invoertextbox, dialoogtextbox;
    JOptionPane resetdialog;
    Toolkit toolkit;
    Timer timer;

    public Paneel() {

        setLayout(null);

        dialoogtextbox = new JTextField(12);
        dialoogtextbox.setFont(new Font("sansserif", Font.BOLD, 12));
        dialoogtextbox.setBounds(12, 12, 838, 207);
        dialoogtextbox.list();
        invoertextbox = new JTextField(12);
        invoertextbox.setBounds(12, 330, 982, 20);
        commandstextbox = new JTextField(12);
        commandstextbox.setBounds(856, 28, 138, 191);
        naamtextbox = new JTextField(12);
        naamtextbox.setBounds(772, 263, 220, 20);
        sendknop = new JButton("Send");
        sendknop.setBounds(12, 260, 75, 23);
        sendknop.addActionListener(new sendknopHandler());

        add(dialoogtextbox);
        add(invoertextbox);
        add(commandstextbox);
        add(naamtextbox);
        add(sendknop);

        class sendknopHandler implements ActionListener {
            public void actionPerformed(ActionEvent e) {
                doen();
            }
        }

    private void doen() {
        text = invoertextbox.getText();
        invoertextbox.setText("");
        // Spellingcheck

        switch (level) {
            case 0:
                if (text == "start") {
                    messagebox("test");
                    level = 1;
                    AccountName = naamtextbox.getText();
                    dialoogtextbox.setText(dialoogtextbox.getText() + "\n"
                            + " you are " + AccountName);
                    dialoogtextbox.setText(dialoogtextbox.getText()
                            + "\n"
                            + " you found you're self in a basement with furniture and a door.");
                    commandstextbox.setText("open door");
                    commandstextbox.setText(commandstextbox.getText() + "\n"
                            + "check table");
                }
                break;
            default:
                dialoogtextbox.setText("niks");
                break;
            // Hall key Gebruiken
        }
        progressbar.setValue(level);
    }

    private void messagebox(String string) {
    }
}
user229044
  • 232,980
  • 40
  • 330
  • 338
Cor Wopereis
  • 9
  • 1
  • 3

3 Answers3

6

replace

text == "start"

with

"start".equals(text)

See

Community
  • 1
  • 1
jmj
  • 237,923
  • 42
  • 401
  • 438
1

Its not the switch its the if (text == "start")

Always use equals for string Comparison. & equalsIgnoreCase for case independent comparision

Also if one of the parameter is fixed string it should be used to compare like in your case F which avoid unnecessary null checks.

"start".equalsIgnoreCase(text)
Community
  • 1
  • 1
Amit Deshpande
  • 19,001
  • 4
  • 46
  • 72
0

As Jigar Joshi say, or you can switch to Java 7 that allow use to create string base Switcher as

String name = getName();
switch (name)
  {
    case "anna": break;
    case "sara": break;
    default:
  }

But this is only in Java 7, else you must use IF ELSE statement.

FIG-GHD742
  • 2,486
  • 1
  • 19
  • 27