for loop(i)在循环外使用的变量或可能的解决方案?

时间:2014-04-25 03:19:50

标签: java swing user-interface for-loop awt

我正在尝试为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!");


             }

            }



             }
        }

1 个答案:

答案 0 :(得分:3)

您可以尝试通过尝试使整个类可以访问变量来尝试这样做。

要实现这一点,您可以在类声明后添加声明用户界面项的变量声明。

然后,您可以在另一个类中访问此变量。

示例代码:

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!");
             }

          }
     }
    }