Java ActionListener错误<identifier>预期和非法启动类型

时间:2017-12-30 03:45:49

标签: java swing actionlistener

我知道这些问题已被提出,我已经在stackoverflow上搜索过了,但我的问题并没有得到解决。将actionlistener设置为按钮时出错,但我总是收到错误:

Talk.java:25: error: <identifier> expected
                button.addActionListener(new ActionListener());
                                        ^

Talk.java:25: error: illegal start of type    
                button.addActionListener(new ActionListener());
                                         ^

代码:

import java.util.Scanner;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.JFrame;
import javax.swing.*;
import java.io.BufferedReader;
class Talk extends javax.swing.JFrame    {
public  void main(String args[]) {
            JFrame frame = new JFrame("LET'S TALK");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setExtendedState(Frame.MAXIMIZED_BOTH);
            JLabel textLabel=new JLabel("What's up.",SwingConstants.CENTER);
            textLabel.setPreferredSize(new Dimension(530,100));
            frame.getContentPane().add(textLabel, BorderLayout.CENTER);
            frame.setLocationRelativeTo(null);
            frame.pack();
            frame.setVisible(true);
            JButton button=new JButton("OK");
            JPanel panel1=new JPanel();
            panel1.add(button);
            frame.add(panel1, BorderLayout.SOUTH);}
            private class ActionListener {
            button.addActionListener(new ActionListener());
                    public void actionPerformed(ActionEvent e){
                    final TextField tf=new TextField();
                    tf.setText("welcome");
                  }
             }
}

2 个答案:

答案 0 :(得分:1)

你做错了。试试这个

 button.addActionListener(new ActionListener(){

    @Override
    public void actionPerformed(ActionEvent e){
      final TextField tf=new TextField();
      tf.setText("welcome");
    }

});

编辑:您的括号放错了地方。这样做:

2nEdit:在将所有组件添加到frame.pack()后,您应该只调用frame.setVisible(true)panel

class Talk extends javax.swing.JFrame    {

public static void main(String args[]) {
    JFrame frame = new JFrame("LET'S TALK");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setExtendedState(Frame.MAXIMIZED_BOTH);
    JLabel textLabel=new JLabel("What's up.",SwingConstants.CENTER);
    textLabel.setPreferredSize(new Dimension(530,100));
    frame.getContentPane().add(textLabel, BorderLayout.CENTER);
    frame.setLocationRelativeTo(null);

    JButton button=new JButton("OK");
    JPanel panel1=new JPanel();
    JTextField textField = new JTextField(8);

    panel1.add(button);
    panel1.add(textField);
    frame.add(panel1, BorderLayout.SOUTH);

    button.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            textField.setText("welcome");
        }
    });

    frame.pack();
    frame.setVisible(true);
  }
}

答案 1 :(得分:-2)

格雷格兹是对的。 您需要创建一个ActionListener的匿名内部类,实现actionPerform(ActionEvent e)方法并在此方法中编写您的操作代码。这是最简单的方法。

或者,您可以创建一个实现ActionListener接口的单独类,并覆盖操作代码的actionPerform(ActionEvent e)方法。然后在这里放置一个类的对象, button.addActionListener(hereObjectOfThatClass)