2012-11-11 95 views
0

我試圖在擺動中實現播放/暫停按鈕。 我發起這樣的JLabel:更改JLabel的ImageIcon

PlayLabel = new JLabel(new ImageIcon(playPNG)); 
PlayLabel.addMouseListener(new MouseAdapter() { 
      public void mouseClicked(MouseEvent arg0) { 
       PlayLabel.setIcon(new ImageIcon(pausePNG)); 
      } 
     }); 

所以這工作正常。點擊PlayLabel後,imageSource被更改。 但在我暫停方法,這將不起作用:

private void doPause() 
    { 
     System.out.println("PAUSED"); 
     player.pauseCurrentTrack(); 
     PlayLabel.setIcon(new ImageIcon(playPNG)); 
    } 

此JLabel不會把它的ImageIcon回playPNG

+3

什麼問題你沒有顯示:-)考慮發佈一個SSCCE爲更好地幫助越早 – kleopatra

+1

'PlayLabel'也許應該是一個'JButton'的代碼。它有設置備用圖標的方法。 2)請學習常見的[Java命名約定](http://java.sun.com/docs/books/jls/second_edition/html/names.doc.html#73307)(具體用於名稱的情況) ,方法和屬性名稱並一致使用。 3)爲了更快得到更好的幫助,請發佈[SSCCE](http://sscce.org/)。 –

+0

@kleopatra你忘了提及命名嗎? ;) –

回答

2

JToggleButton可能更適合在這種情況下。 setIcon()方法應該按照你的建議工作。作爲參考,這個example更新中的每個按鈕不斷變化List<Icon>。它可能會提供一些見解或形成你的基礎sscce

image

2

下面的例子顯示,你想要做什麼,但文本而不是圖標。該程序切換圖像,文字或任何您顯示的內容。

如果你使用一個JLabel的原因是因爲你想要一個平坦的按鈕,可以添加代碼:

private void makeButtonFlat(final JButton makeMeAFlatButton) { 
     makeMeAFlatButton.setOpaque(false); 
     makeMeAFlatButton.setFocusPainted(false); 
     makeMeAFlatButton.setBorderPainted(false); 
     makeMeAFlatButton.setContentAreaFilled(false); 
     makeMeAFlatButton.setBorder(BorderFactory.createEmptyBorder()); 
    } 

,並從

public void makeLayout() { 
     makeButtonFlat(playButton); 
     add(playButton); 
    } 

那麼它看起來像一個稱之爲JLabel,但作爲一個按鈕,這是更合乎邏輯的。

Running program

代碼片段:

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

import javax.swing.*; 

public class Playful { 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       JFrame frame = new JFrame("Playful"); 
       JPanel playPanel = new PlayPanel(); 
       frame.getContentPane().add(playPanel, BorderLayout.CENTER); 
       frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 
       frame.setMinimumSize(new Dimension(400, 250)); 
       frame.setLocationRelativeTo(null); // Center 
       frame.pack(); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    static class PlayPanel extends JPanel { 

     enum State { 
      PLAY, 
      PAUSE; 

      public State toggle() { 
       switch (this) { 
        case PLAY: 
         return PAUSE; 
        case PAUSE: 
         return PLAY; 
        default: 
         throw new IllegalStateException("Unknown state " + this); 
       } 
      } 
     } 

     private State state; 
     private JButton playButton; 
     private ActionListener playActionListener; 

     PlayPanel() { 
      createComponents(); 
      createHandlers(); 
      registerHandlers(); 
      makeLayout(); 
      initComponent(); 
     } 

     public void createComponents() { 
      playButton = new JButton(); 
     } 

     public void createHandlers() { 
      playActionListener = new AbstractAction() { 
       @Override 
       public void actionPerformed(ActionEvent e) { 
        SwingUtilities.invokeLater(new Runnable() { 
         @Override 
         public void run() { 
          toggleState(); 
         } 
        }); 
       } 
      }; 
     } 

     public void registerHandlers() { 
      playButton.addActionListener(playActionListener); 
     } 

     public void makeLayout() { 
      add(playButton); 
     } 

     public void initComponent() { 
      state = State.PAUSE; 
      updatePlayButton(); 
     } 

     private void toggleState() { 
      state = state.toggle(); 
      updatePlayButton(); 
     } 

     private void updatePlayButton() { 
      playButton.setText(state.name()); 
     } 
    } 
}