我的目标是比较两个字符串。一个字符串只是来自文本字段 (txt) 的用户的输入,然后,如果它们匹配,则将文本字段更改为第三个字符串 (msg)。
但是,当我为 txt 字符串输入正确的字符并单击按钮时,什么也没有发生。为什么它不更改为“Derk?”,味精字符串?
代码:
package levels;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class LevelOne extends JFrame implements ActionListener{
private JTextField input = new JTextField("Ich spielen Golf.");
private JButton submit = new JButton("Check sentence");
public void one(){
setTitle("Conjugator");
setSize(400,400);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
setLayout(new BorderLayout());
setContentPane(new JLabel(new ImageIcon("images/LevelOneBG.gif")));
setLayout(new FlowLayout());
JTextArea area = new JTextArea("You enter a castle. A Goblin demands you correct his sentences!");
add(area);
setVisible(true);
JButton submit = new JButton("Check sentence");
submit.addActionListener(this);
add(submit);
setVisible(true);
JTextField input = new JTextField("Ich spielen Golf.");
input.setActionCommand("input");
add(input);
input.addActionListener(this);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == submit) {
String txt = (input.getText());
String test = ("test");
String msg = ("Derk?");
if (txt.equals(test)){
//after check
input.setText(msg);
}
}
}
}