该程序将执行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) {
}
}
答案 0 :(得分:6)
答案 1 :(得分:1)
它不是if (text == "start")
始终使用equals进行字符串比较。 &安培; equalsIgnoreCase用于案例独立比较
另外,如果其中一个参数是固定字符串,那么它应该用于比较你的情况F,以避免不必要的空值检查。
"start".equalsIgnoreCase(text)
答案 2 :(得分:0)
正如Jigar Joshi所说,或者您可以切换到允许使用创建字符串基础Switcher的Java 7
String name = getName();
switch (name)
{
case "anna": break;
case "sara": break;
default:
}
但这只是在Java 7中,否则你必须使用IF ELSE语句。