在主类中没有调用ActionListener?

时间:2014-10-10 15:52:59

标签: java swing

所以我有2个类,一个主类和一个扩展JButton并实现ActionListener的辅助类。我的主要类扩展了JFrame并实现了ActionListener。单击任何自定义按钮时,将调用辅助节点中的ActionListener并执行其功能(打开文件选择器)。

但是,当我按下与主类关联的任何按钮(不是我的自定义按钮)时,不会调用ActionListener。

以下是主类的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();
    }
}

这是自定义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());
        }
    }
}

sysout仅用于调试目的。

编辑: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();
            }
        }
    }
}

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());
            }
        }
    }
}

2 个答案:

答案 0 :(得分:1)

您需要将ActionListener添加到按钮中才能使其正常工作。要添加使用此代码:

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

public MusicMaker()构造函数中。

MusicButton课程中,您已经在此行中执行此操作:

this.addActionListener(this);

答案 1 :(得分:0)

我刚刚意识到我的问题!我没有用按钮注册ActionListener。我累了......:P谢谢你的尝试。