为什么我的actionperformed方法不起作用?每当我点击一个按钮,它什么都不做

时间:2014-10-09 20:30:28

标签: java eclipse button user-interface actionlistener

我没有解决代码问题。请帮忙。

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

import javazoom.jl.decoder.JavaLayerException;

import com.hubberspot.example.PausablePlayer;


import java.awt.event.*;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

    @SuppressWarnings("serial")
    public class GUI extends JFrame implements ActionListener, MouseListener{

        JButton button1, button2, button3, button4, button5;
        String song;
        PausablePlayer p;
        JPanel gridPanel, buttonPanel;

        public GUI(int rows, int columns) throws FileNotFoundException, JavaLayerException
        {

            super();
            setTitle("Santorini");
            setSize(1000, 700);
            setLocation(200, 100);
            Container content = getContentPane();
            content.setBackground(new Color(1,1,1));
            content.setLayout(new BorderLayout());


            InitPanels(rows, columns);
            InitGridTiles();
            CreateButtons();
            setVisible(true);

            WindowDestroyer wd = new WindowDestroyer();
            addWindowListener(wd);
        }

        public void InitPanels(int row, int column)
        {
            //Create Panel for the tiles grid
            gridPanel = new JPanel(new GridLayout(5,5 ));
            gridPanel.setBackground(new Color(1,1,1));
            add(gridPanel, BorderLayout.CENTER);

            //Create panel for the buttons vertical bar
            buttonPanel = new JPanel(new GridLayout(2,2));
            buttonPanel.setBackground(Color.PINK);
            add(buttonPanel, BorderLayout.NORTH);
        }

        public void InitGridTiles() throws FileNotFoundException, JavaLayerException
        {
            song = JOptionPane.showInputDialog(null,
                    "Please enter location of song: ");
            song();
            for(int i = 0; i < 6; i++)
            {
                if(i==1)
                {
                    /*JLabel label = new JLabel("Pause");
                    label.setHorizontalAlignment(JLabel.CENTER);
                    gridPanel.add(label);*/

                    //ImageIcon icon = new ImageIcon("pausetry.png");
                    button1 = new JButton("Pause");
                    button1.addMouseListener(this);
                    gridPanel.add(button1);
                }
                if(i==2)
                {
                    /*JLabel label2 = new JLabel("Play");
                    label2.setHorizontalAlignment(JLabel.CENTER);
                    gridPanel.add(label2);*/

                    //ImageIcon icon1 = new ImageIcon("play.png");
                    button2 = new JButton("Play");
                    button2.addMouseListener(this);
                    gridPanel.add(button2);
                }
                if(i==3)
                {
                    /*JLabel label3 = new JLabel("Stop");
                    label3.setHorizontalAlignment(JLabel.CENTER);
                    gridPanel.add(label3);*/

                    //ImageIcon icon3 = new ImageIcon("stop.png");
                    button3 = new JButton("Stop");
                    button3.addMouseListener(this);
                    gridPanel.add(button3);
                }
                if(i==4)
                {
                    button4 = new JButton("Mute");
                    button4.addMouseListener(this);
                    gridPanel.add(button4);
                }
                if(i==5)
                {
                    button5 = new JButton("Repeat");
                    button5.addMouseListener(this);
                    gridPanel.add(button5);
                }



            }

        }

        public void CreateButtons()
        {
            JLabel label = new JLabel(song);
            label.setHorizontalAlignment(JLabel.CENTER);
            buttonPanel.add(label);
        }

        public void mouseClicked(MouseEvent e) {
            // TODO Auto-generated method stub
            /*if (e.getSource().equals("Play"))
            {
                try {
                    p.play();
                } catch (JavaLayerException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }
            else 
            {
                if(e.getSource().equals("Pause"))
                {
                    p.pause();
                }
                else 
                {
                    if(e.getSource().equals("Stop"))
                    {
                        p.stop();
                    }
                }
            }*/
        }

        public void mouseEntered(MouseEvent arg0) {
            // TODO Auto-generated method stub

        }

        public void mouseExited(MouseEvent arg0) {
            // TODO Auto-generated method stub

        }

        public void mousePressed(MouseEvent arg0) {
            // TODO Auto-generated method stub

        }

        public void mouseReleased(MouseEvent arg0) {
            // TODO Auto-generated method stub

        }

        public void song() throws FileNotFoundException, JavaLayerException
        {
            //C:\\Users\\Dina Mohamed\\Desktop\\Gone gone gone.mp3
            FileInputStream in = new FileInputStream(song);
            PausablePlayer p = new PausablePlayer(in);
            p.play();
        }

        public void actionPerformed(ActionEvent e) 
        {   
            if(e.getSource() == button2)
            {
                try {
                    p.play();
                } catch (JavaLayerException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }
            else 
            {
                if(e.getSource() == button1)
                {
                    p.pause();
                }
                else 
                {
                    if(e.getActionCommand().equals(button3))
                    {
                        p.stop();
                    }
                }
            }
        }
    }
    Why?

1 个答案:

答案 0 :(得分:1)

ActionListeners需要在要调用的组件中注册。取代

button2.addMouseListener(this);

button2.addActionListener(this);