有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

ActionListener中的java调用

我正在尝试将我的代码插入到我的GUI中,现在我找不到一种方法来调用ActionListener中的文本,而不中断我的代码。我知道findAndReplace()中有一些东西我需要处理才能使它在我的GUI中工作。。。但现在我只是想解释一下ActionListener

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;

import javax.swing.*;



public class HangmanPanel extends JPanel {
    static Boolean FOUND;
    private static final long serialVersionUID = -5793357804828609325L;

    public static String answerKey() {
        //get random array element
        String array[] = new String[10];
        array[0] = "hamlet";
        array[1] = "mysts of avalon";
        array[2] = "the iliad";
        array[3] = "tales from edger allan poe";
        array[4] = "the children of hurin";
        array[5] = "the red badge of courage";
        array[6] = "of mice and men";
        array[7] =  "utopia"; 
        array[8] =  "chariots of the gods";
        array[9] =  "a brief history of time";

        ArrayList<String> list = new ArrayList<String>(Arrays.asList(array));
        Collections.shuffle(list);
        String s = list.get(0);
        return s;
    }

    public StringBuilder dashReplace(String s) {
        //replace non-white space char with dashes and creates StringBuilder Object
        String tW = s.replaceAll("\\S", "-"); 
        System.out.print(tW + "\n");  
        StringBuilder AnswerKey = new StringBuilder(tW);
        return AnswerKey;
    }





    public HangmanPanel(){
        this.setLayout(null);

        JLabel heading = new JLabel("Welcome to the Hangman App");
        JButton Button = new JButton("Ok");
        //get input
        Button.addActionListener(new input()); 

        JLabel tfLable = new JLabel("Please Enter a Letter:");


        JLabel AnswerKey = new JLabel(dashReplace(answerKey()).toString());

        JTextField text = new JTextField(10);


        heading.setSize(200, 50);
        tfLable.setSize(150, 50);
        text.setSize(50, 30);
        Button.setSize(60, 20);
        AnswerKey.setSize(200, 100);

        heading.setLocation(300, 10);
        tfLable.setLocation(50, 40);
        text.setLocation(50, 80);
        Button.setLocation(100, 85);
        AnswerKey.setLocation(100,85);

        this.add(heading);
        this.add(tfLable);
        this.add(text);
        this.add(Button);
        this.add(AnswerKey);
    }
    public class input implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent e) {
            // can't access text
            sc = text.getText();
            char ch = sc.charAt(0);
            findAndReplace(s, AnswerKey, input, ch);
        }


    } 
    public static int findAndReplace(String s, StringBuilder AnswerKey, String sc,
            char ch) {
        //find position of user input and replace
        int pos = -1;
        FOUND = false;
        while(true){
        pos = s.indexOf(sc, pos+1);
        if(pos < 0){

            break;
        }else{
            FOUND = true;
            AnswerKey.setCharAt(pos, ch);
        }

        }
        System.out.println(AnswerKey);
        return pos;
    }

}

共 (2) 个答案

  1. # 1 楼答案

    首先不要使用小写的类名和大写的变量名,你的代码真的很难读

    您可以使用匿名内部类添加事件侦听器,如下所示:

    Button.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            // can't access text
            sc = text.getText();
            char ch = sc.charAt(0);
            findAndReplace(s, AnswerKey, input, ch);
        }
    });
    

    text变量标记为final,以便在匿名类中访问它:

    final JTextField text = new JTextField(10);
    
  2. # 2 楼答案

    文本JTextField变量在构造函数中声明,并且对类的其他方法不可见,因为它没有类“作用域”。您需要通过在类中声明该变量,而不是在构造函数中声明该变量,使其成为类字段,以便使其在整个类中可见

    例如:

    public class HangmanPanel extends JPanel {
       private static final long serialVersionUID = -5793357804828609325L;
       private Boolean found; // this should not be static nor capitalized
       private JTextField text; // declare your class field
    
       // .... etc...
    
       public HangmanPanel() {
          this.setLayout(null);  // *** don't do this. Use layout managers.
    
          // ... etc
    
          // JTextField text = new JTextField(10);
          text = new JTextField(10);