2014-10-09 74 views
-4

我沒有得到代碼的問題。請幫忙。爲什麼我的actionperformed方法不起作用?每當我點擊一個按鈕,它什麼都不做

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? 
+4

那是最短的例子,你可以拿出演示該問題? – 2014-10-09 20:33:00

+0

嗯它已經有一段時間了,因爲我已經完成了gui編程,但是你沒有忘記添加ActionListener到你的按鈕? – Gas 2014-10-09 21:21:05

回答

1

ActionListeners需要註冊到要調用的組件。更換

button2.addMouseListener(this); 

button2.addActionListener(this); 
相關問題