简体   繁体   中英

Why doesn't this ActionListener work?

I am just learning to use ActionListener s and making widgets working. Here is my simple program that I want to print whatever i type in a JTextField .

TextViewer1

import javax.swing.JFrame;

public class TextViewer1 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        JFrame frame = new TextFrame1();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

    }

}

TextFrame1

import java.awt.event.ActionListener;
import javax.swing.*;

public class TextFrame1 extends JFrame {
    private static final int FRAME_WIDTH = 300;
    private static final int FRAME_HEIGHT = 200;

    public TextFrame1() {
        createComponents();
        setSize(FRAME_WIDTH, FRAME_HEIGHT);
    }


    private void createComponents() {
        JPanel panel = new JPanel();
        add(panel);

        JTextField textbox = new JTextField(20);
        panel.add(textbox);

        String TYPED = textbox.getText();
        ActionListener sendsText = new TextListener();
        textbox.addActionListener(sendsText);


    }
}

TextListener

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class TextListener implements ActionListener {

    /**
     * @param args
     */

    public void actionPerformed(ActionEvent event) {

        System.out.println(TYPED);

    }

}

So the problem is the variable TYPED in 'TextListener', it wants to create this variable and initialise it.

What is wrong?

So the problem is the variable TYPED in 'Text Listener', it wants to create this variable and initialise it.

This is because the variable is undefined in your TextListener . You create a local variable called TYPED in TextFrame1#createComponents but this is not visible in actionPerformed in TextListener .

In fact you are assigning the variable to the initial text of the of the JTextField which by default is an empty String . If you want to get the current text of the component, you could pass it into the constructor:

class TextListener implements ActionListener {

   private final JTextField textField;
   public TextListener(JTextField textField) {
    this.textField = textField;
   }

   @Override
   public void actionPerformed(ActionEvent event) {

      System.out.println(textField.getText());
   }
}

Create the TextListener like this:

ActionListener sendsText = new TextListener(textbox);

TYPED is not declared as a variable. So either declare it or surround it with "".

edit: Yes, you did declare it, but as a local variable within the createComponents functions inside the TextFrame1 class.

You need to pass the value like:

public class TextListener implements ActionListener {
    private String typed;

    public TextListener(String typed) {
        this.typed = typed;
    }

    /**
     * @param args
     */

    public void actionPerformed(ActionEvent event) {

        System.out.println(typed);

    }

}

Oh, and variables should not be in upper case but in lower case. Upper case is for constants.

edit 2:my bad, that listener constructor should have accepted String typed, not int typed. I fixed the code. As to your other issue, that is because you now need to pass the input result to the listener declaration.

 ActionListener sendsText = new TextListener(TYPED);

Or just do:

 ActionListener sendsText = new TextListener(textbox.getText());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM