ActionListeners无法工作

时间:2014-03-23 18:02:20

标签: java swing jframe actionlistener

我在编写程序时遇到问题而且无法正常工作!这让我很生气,因为我知道我正在从平板电脑上输入它。我有这个应用程序,它给了我可以复制的程序。我正在尝试制作一个显示时间,修饰符,源和事件ID的程序。所以我不知道出了什么问题,我需要一些帮助。所以,如果你能在下面写下这段代码有什么问题。还有一件事我是初学者,所以请不要嘲笑我的错误,因为我不知道出了什么问题。无论如何这里是代码:

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

public class Time extends JFrame {
    private JList list;
    private DefaultListModel model;

    public Time() {

        initUI();
    }

    public final void initUI() {

        JPanel panel = new JPanel();
        panel.setLayout(null);

        model = new DefaultListModel();
        list = new JList(model);
        list.setBounds(150, 30, 220, 150);

        JButton okButton = new JButton("Ok");
        okButton.setBounds(30, 35, 80, 25);

        okButton.addActionListener(this);

        panel.add(okButton);
        panel.add(list);
        add(panel);

        setTitle("Time");
        setSize(420, 250);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

    }

    public void actionPerformed(ActionEvent e) {

        Locale locale = Locale.getDefault();
        Date date = new Date(e.getWhen());
        String s = DateFormat.getTimeInstance(DateFormat.SHORT, locale).format(date);

        if(!model.isEmpty()) {
            model.clear();
        }

        if (e.getID() == ActionEvent.ACTION_PERFORMED) {
            model.addElement(" Event Id: ACTION_PERFORMED");

        }

        model.addElement("Time: " + s);

        String source = e.getSource().getClass().getName();

        int mod = e.getModifiers();

        StringBuffer buffer = new StringBuffer("Modifiers: ");

        if ((mod & ActionEvent.ALT_MASK) > 0) {
            buffer.append("Alt ");

        }

        if ((mod & ActionEvent.SHIFT_MASK) > 0) {
            buffer.append("Shift ");

        }

        if ((mod & ActionEvent.META_MASK) > 0) {
            buffer.append("Meta ");

        }

        if ((mod & ActionEvent.CTRL_MASK) > 0) {
            buffer.append("Ctrl ");

        }
        model.addElement(buffer);

    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            public void run() {

                Time ex = new Time();
                ex.setVisible(true);


            }
        });
    }
}

1 个答案:

答案 0 :(得分:1)

为您的类实现ActionListener接口:


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

    public class Time extends JFrame implements ActionListener {
        private JList list;
        private DefaultListModel model;

        public Time() {

            initUI();
        }

        public final void initUI() {

            JPanel panel = new JPanel();
            panel.setLayout(null);

            model = new DefaultListModel();
            list = new JList(model);
            list.setBounds(150, 30, 220, 150);

            JButton okButton = new JButton("Ok");
            okButton.setBounds(30, 35, 80, 25);

            okButton.addActionListener(this);

            panel.add(okButton);
            panel.add(list);
            add(panel);

            setTitle("Time");
            setSize(420, 250);
            setLocationRelativeTo(null);
            setDefaultCloseOperation(EXIT_ON_CLOSE);

        }

        public void actionPerformed(ActionEvent e) {

            Locale locale = Locale.getDefault();
            Date date = new Date(e.getWhen());
            String s = DateFormat.getTimeInstance(DateFormat.SHORT, locale).format(date);

            if(!model.isEmpty()) {
                model.clear();
            }

            if (e.getID() == ActionEvent.ACTION_PERFORMED) {
                model.addElement(" Event Id: ACTION_PERFORMED");

            }

            model.addElement("Time: " + s);

            String source = e.getSource().getClass().getName();

            int mod = e.getModifiers();

            StringBuffer buffer = new StringBuffer("Modifiers: ");

            if ((mod & ActionEvent.ALT_MASK) > 0) {
                buffer.append("Alt ");

            }

            if ((mod & ActionEvent.SHIFT_MASK) > 0) {
                buffer.append("Shift ");

            }

            if ((mod & ActionEvent.META_MASK) > 0) {
                buffer.append("Meta ");

            }

            if ((mod & ActionEvent.CTRL_MASK) > 0) {
                buffer.append("Ctrl ");

            }
            model.addElement(buffer);

        }

        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {

                public void run() {

                    Time ex = new Time();
                    ex.setVisible(true);


                }
            });
        }
    }