0

该程序会做text = invoertextbox.getText();invoertextbox.setText("");但它不想打开切换器或默认值。有人建议吗?它也不显示消息框。

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) {
    }
}
4

3 回答 3

6

代替

text == "start"

"start".equals(text)

于 2012-09-20T06:47:57.910 回答
1

它不是开关它的if (text == "start")

始终使用 equals 进行字符串比较。& equalsIgnoreCase 用于大小写无关的比较

此外,如果其中一个参数是固定字符串,则应该使用它来进行比较,就像在您的情况 F 中那样避免不必要的空检查。

"start".equalsIgnoreCase(text)
于 2012-09-20T06:50:40.960 回答
0

正如 Jigar Joshi 所说,或者您可以切换到允许使用创建字符串基础切换器的 Java 7

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

但这仅在 Java 7 中,否则您必须使用 IF ELSE 语句。

于 2012-09-20T06:53:45.077 回答