根据actionPerformed
Eclipse,似乎所有变量(提交、消息、输入)“无法解析”。根据我的经验(我很少),这意味着我没有定义变量。但是,正如您在代码中看到的那样,我已经定义了它们。Submit 是一个 JButton,msg 是一个字符串,input 是一个 JTextField。
package levels;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.util.*;
public class LevelOne extends JFrame implements ActionListener{
private Object msg;
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);
//these aren't being called to actionPerformed
JButton submit = new JButton("Check sentence");
submit.addActionListener(this);
setVisible(true);
JTextField input = new JTextField("Ich spielen Golf.");
input.setActionCommand("input");
add(input);
input.addActionListener(this);
setVisible(true);
String msg = ("Test successful");
}
//this should be successfully locating and utilizing "submit", "input" and "msg", but it won't
public void actionPerformed(ActionEvent e) {
if (e.getSource() == submit) {
msg = submit.getText();
//!! Display msg only **after** the user has pressed enter.
input.setText(msg);
}
}
}
我知道我的一些进口是不必要的。
PS,我正在为我的德语课制作一个小型文字冒险游戏