简体   繁体   English

在主类中未调用ActionListener?

[英]ActionListener not called in main class?

So I have 2 classes, a main class and a secondary one that extends JButton and implements ActionListener. 因此,我有2个类,一个是主类,另一个是扩展JButton并实现ActionListener的辅助类。 My primary class extends JFrame and implements ActionListener. 我的主类扩展了JFrame并实现了ActionListener。 When any of my custom buttons are clicked, ActionListener in the secondary is called and it performs its function (to open a file picker). 单击我的任何自定义按钮时,将调用辅助按钮中的ActionListener并执行其功能(以打开文件选择器)。

However, when I press any buttons associated with the main class (buttons that are not my custom ones), ActionListener is not called. 但是,当我按下与主类关联的任何按钮(不是我的自定义按钮)时,不会调用ActionListener。

Here is the ActionListener code from the main class: 这是来自主类的ActionListener代码:

@Override
public void actionPerformed(ActionEvent e) {
    System.out.println("ActionEvent MM");
    String ac = e.getActionCommand();
    if(ac.equalsIgnoreCase("play")){
        pl.unPause();
    }
    if(ac.equalsIgnoreCase("stop")){
        pl.pause();
    }
}

And here is the ActionListener code from the custom JButton class: 这是来自定制JButton类的ActionListener代码:

@Override
public void actionPerformed(ActionEvent e) {
    System.out.println("ActionEvent MB");
    int code = fc.showOpenDialog(this);
    if (code == JFileChooser.APPROVE_OPTION) {
        file = fc.getSelectedFile().getName();
        if (file.substring(file.length() - 3).equalsIgnoreCase("mp3")
                || file.substring(file.length() - 3)
                        .equalsIgnoreCase("wav")) {
            super.setText(file);
            musicfile = new File(fc.getSelectedFile().getPath());
        }
    }
}

The sysout is only for debugging purposes. sysout仅用于调试目的。

EDIT: Entire code of MusicMaker (main) class: 编辑:MusicMaker(主)类的完整代码:

public class MusicMaker extends JFrame implements ActionListener{
    public static Random gen = new Random();
    public static Scanner kbr = new Scanner(System.in);

    private JButton play = new JButton(new ImageIcon("play.png"));
    private JButton stop = new JButton(new ImageIcon("stop.png"));
    private JLabel BPML = new JLabel("BPM: ");
    private SpinnerModel BPMsm = new SpinnerNumberModel(150, // initial value
            1, // minimum
            300, // max
            1); // step
    final private JSpinner BPMs = new JSpinner(BPMsm);
    private ArrayList<MusicButton> mbtn = new ArrayList<MusicButton>();
    public final CopyOnWriteArrayList<ArrayList<JCheckBox>> chbxsal = new CopyOnWriteArrayList<ArrayList<JCheckBox>>();
    private final Object lock = new Object();
    private Player pl = new Player();

    public MusicMaker() {
        super("Music Maker Beta v0.1");
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        for (int i = 0; i < 12; i++) {
            mbtn.add(new MusicButton("choose" + i));
        }
        for (int i = 0; i < 16; i++) {
            chbxsal.add(new ArrayList<JCheckBox>());
            for (int e = 0; e < 12; e++) {
                chbxsal.get(i).add(new JCheckBox());
            }
        }
        this.getContentPane().setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();

        gbc.fill = GridBagConstraints.BOTH;
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridwidth = 1;
        this.add(play, gbc);
        gbc.gridx = 1;
        gbc.gridwidth = 1;
        this.add(stop, gbc);
        gbc.gridx = 4;
        gbc.gridwidth = 3;
        this.add(BPMs, gbc);
        gbc.gridx = 2;
        gbc.gridwidth = 3;
        this.add(BPML, gbc);
        gbc.gridwidth = 2;
        for (int i = 0; i < 12; i++) {
            gbc.gridx = 0;
            gbc.gridy = i + 1;
            this.add(mbtn.get(i), gbc);
        }
        gbc.gridwidth = 1;
        for (int i = 0; i < 16; i++) {
            for (int e = 0; e < 12; e++) {
                gbc.gridx = i + 2;
                gbc.gridy = e + 1;
                this.add(chbxsal.get(i).get(e), gbc);
            }
        }
        this.pack();
        Thread thread = new Thread(pl);
        thread.setName("Music player thread");
        thread.setDaemon(true);
        thread.start();
    }

    public static void main(String[] args) {
        MusicMaker mm = new MusicMaker();
        mm.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("ActionEvent MM");
        String ac = e.getActionCommand();
        if(ac.equalsIgnoreCase("play")){
            pl.unPause();
        }
        if(ac.equalsIgnoreCase("stop")){
            pl.pause();
        }
    }

    private class Player implements Runnable {
        private volatile boolean isPaused = true;

        @Override
        public void run() {
            while (true) {
                try {
                    while (isPaused) {
                        synchronized (lock) {
                            lock.wait();
                        }
                    }
                    System.out.println("test");
                } catch (Exception e) {
                    // handle exceptions
                }
            }

        }

        public void pause() {
            isPaused = true;
        }

        public void unPause() {
            isPaused = false;
            synchronized (lock) {
                lock.notifyAll();
            }
        }
    }
}

Entire code of MusicButton (custom button) class: MusicButton(自定义按钮)类的完整代码:

public class MusicButton extends JButton implements ActionListener {
    public static Random gen = new Random();
    public static Scanner kbr = new Scanner(System.in);
    // Create a file chooser
    final JFileChooser fc = new JFileChooser();
    public String file = "";
    public File musicfile;

    public MusicButton(String s) {
        super(s);
        super.addActionListener(this);
        super.setText("Choose");
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("ActionEvent MB");
        int code = fc.showOpenDialog(this);
        if (code == JFileChooser.APPROVE_OPTION) {
            file = fc.getSelectedFile().getName();
            if (file.substring(file.length() - 3).equalsIgnoreCase("mp3")
                    || file.substring(file.length() - 3)
                            .equalsIgnoreCase("wav")) {
                super.setText(file);
                musicfile = new File(fc.getSelectedFile().getPath());
            }
        }
    }
}

You need to add your ActionListener s to your buttons to make them work. 您需要将ActionListener添加到按钮上,以使其起作用。 To add use this code: 要添加,请使用以下代码:

play.addActionListener(this);
stop.addActionListener(this);

in your public MusicMaker() constructor. 在您的public MusicMaker()构造函数中。

In your MusicButton class you do this already in this line: 在您的MusicButton类中,您已经在以下行中执行了此操作:

this.addActionListener(this);

I just realized my problem! 我才意识到我的问题! I didn't register the ActionListener with the buttons. 我没有使用按钮注册ActionListener。 I am tired... :P Thanks for trying anyways. 我很累...:P仍然感谢您的尝试。

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

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