【问题标题】:calling within ActionListener在 ActionListener 中调用
【发布时间】:2013-05-09 13:33:35
【问题描述】:

目前我正在尝试将我的代码插入我的 GUI让它在我的 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;
    }

}

【问题讨论】:

  • 1) 请学习常见的Java naming conventions(特别是用于名称的大小写)用于类、方法和属性名称,并始终如一地使用它们。 2) Java GUI 可能必须在多个平台、不同的屏幕分辨率和使用不同的 PLAF 上工作。因此,它们不利于组件的精确放置。对于健壮的 GUI,请改为使用布局管理或它们的组合,以及用于空白空间的布局填充和边框来组织组件。 3)一行空白就足够了。

标签: java swing actionlistener jtextfield local-variables


【解决方案1】:

文本 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);

【讨论】:

    【解决方案2】:

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

    你可以像这样使用匿名内部类来添加事件监听器:

    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);
    

    【讨论】:

    • @Sage1216:使用类字段会更好,因为您可能需要通过程序中的多种方法访问文本字段。
    • token "}" 的语法错误,删除这个token...这是前一个方法的结束括号
    • @Hovercraft Full Of Eels:我不确定我将如何使用类字段。我只是 java 的初学者,很快就陷入了困境……我有这个刽子手程序已经完成,但它没有我老师想要的 gui 界面……这对我来说都是全新的。我所知道的关于 gui 的一切都是我昨天学到的......
    • 我认为你打错了,Button.addActionListener 应该是你构造函数中的最后一次调用
    • @Hovercraft Full Of Eels:直到我用 hoaz 解决了这个问题后,你的帖子才出现在我面前“你的构造函数中的最后一次调用”,或者至少我没有看到它。请不要生气,你们俩都非常有帮助......像我这样的初级程序员很幸运有这样一个支持性的编程社区来帮助我们,因为你们两个都帮助了我。 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-01
    • 2013-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多