我正试图为startTextFieldListener找到一种方法来访问for循环中的一些变量。有办法做到这一点吗?如果有一个简单的解决方案,我很抱歉,但我不太确定如何解决这个问题。我看过一些例子,但我不知道如何将其应用到我的程序中。如有任何帮助,我们将不胜感激。
public class TextArea1{
JTextArea text;
JFrame frame;
JTextField textField;
public int k;
public ArrayList aList;
public String correctAnswer;
public static void main (String [] args) {
TextArea1 gui = new TextArea1();
gui.go();
}
private String textLine;
public void go() {
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
textField = new JTextField("");
textField.addActionListener(new startTextFieldListener(aList));
JButton startButton = new JButton ("Start!");
startButton.addActionListener(new startButtonListener(aList));
text = new JTextArea (30, 60);
text.setLineWrap(true);
JScrollPane scroller = new JScrollPane(text);
scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
panel.add(scroller);
frame.getContentPane().add(BorderLayout.CENTER, panel);
frame.getContentPane().add(BorderLayout.WEST, startButton);
frame.getContentPane().add(BorderLayout.SOUTH, textField);
frame.setSize(350, 300);
frame.setVisible(true);
}
class startButtonListener implements ActionListener {
ArrayList aList;
startButtonListener(ArrayList passedInList)
{
aList = passedInList;
}
@Override
public void actionPerformed(ActionEvent event) {
String fileName = "test.txt";
String line;
ArrayList aList = new ArrayList();
try {
try (BufferedReader input = new BufferedReader (new FileReader(fileName))) {
if (!input.ready()) {
throw new IOException();
}
while ((line = input.readLine()) !=null) {
aList.add(line);
}}
} catch (IOException e) {
System.out.println(e);
}
int sz = aList.size();
for (int k = 0; k< sz; k++) {
String correctAnswer = aList.get(k).toString();
text.append(aList.get(k).toString());
text.append("n");
}
}
}
class startTextFieldListener implements ActionListener {
startTextFieldListener(ArrayList passedInList)
{
aList = passedInList;
}
@Override
public void actionPerformed(ActionEvent event) {
text.getText();
if (text.getText().equals(correctAnswer)) {
JOptionPane.showMessageDialog(null, "Hooray!");
}
else if (!text.getText().equals(correctAnswer)) {
JOptionPane.showMessageDialog(null, "Wrong!");
}
}
}
}
您可以尝试通过使整个类都可以访问变量来实现这一点。
要实现这一点,可以在类声明之后,在声明用户界面项的位置添加变量声明。
然后可以在另一个类中访问此变量。
示例代码:
public class TextArea1{
JTextArea text;
JFrame frame;
JTextField textField;
public int sampleIntToBeUsed;
您还可以使用各种get/set方法来实现这一点。
如何创建构造函数:(输入没有返回类型的类的名称)
class startButtonListener implements ActionListener {
ArrayList aList;
startButtonListener(ArrayList passedInList)
{
aList = passedInList;
}
新的第二ActionListener
class startTextFieldListener implements ActionListener {
String correctAnswer;
startTextFieldListener(String answer)
{
correctAnswer = answer;
}
@Override
public void actionPerformed(ActionEvent event) {
if (text.getText().equals(correctAnswer)) {
JOptionPane.showMessageDialog(null, "Hooray!");
}
else {
JOptionPane.showMessageDialog(null, "Wrong!");
}
}
}
}