简体   繁体   English

Java:Actionlistener标识符预期错误

[英]Java: Actionlistener identifier expected error

I am trying to create a method to get input from a user in a textfield by using an ActionListener. 我正在尝试创建一种方法,以使用ActionListener从文本字段中的用户获取输入。 However, when i compile i keep getting this error: 但是,当我编译时,我不断收到此错误:

Quiz.java:36: error: <identifier> expected
  tfInput.addActionListener(new  a ActionListener() {

Code: 码:

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

public class Quiz implements ActionListener {

private static Label lblInput;         
private static TextField tfInput;  
private static String cityIn;
private static TextField tfOutput;       

public void europe() {
   JFrame frame = new JFrame();      
   frame.setLayout(null);

   lblInput = new Label("Skriv in huvudstaden i : "); // Construct Label
   lblInput.setBounds(40,30,300,40);
   frame.add(lblInput);

   tfInput = new TextField(10);
   tfInput.setBounds(40,70,300,40);
   frame.add(tfInput);     

   tfOutput = new TextField(10); // allocate TextField
   tfOutput.setEditable(false);  // read-only
   tfOutput.setBounds(100,30,300,40);
   frame.add(tfOutput);

   frame.setTitle("Europa"); 
   frame.setSize(375, 150);
   frame.setLocationRelativeTo(null);
   frame.setVisible(true);

}

   tfInput.addActionListener(new  ActionListener() {

      @Override
      public void actionPerformed(ActionEvent e) {        
         String inLand = tfInput.getText(); 

      }       
   });   
 }

I cannot understand what could be causing this. 我不明白是什么原因造成的。

Try it this way maybe: 可以这样尝试:

  frame.setVisible(true);
  tfInput.addActionListener(new  ActionListener() {

       @Override
       public void actionPerformed(ActionEvent e) {        
         String inLand = tfInput.getText(); 
       }       
    });   
  }
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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