简体   繁体   English

更改JLabel的ImageIcon

[英]Rechange ImageIcon of JLabel

I am trying to implement a play/pause button in swing. 我试图在秋千中实现播放/暂停按钮。 I initiate the JLabel like this: 我这样启动JLabel:

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

so this works fine. 所以这很好。 After clicking on PlayLabel the imageSource gets changed. 单击PlayLabel ,imageSource会更改。 But in my pause-Method this won't work: 但是在我的暂停方法中,这将无法工作:

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

this JLabel won't turn its ImageIcon back to playPNG 该JLabel不会将其ImageIcon重新playPNGplayPNG

JToggleButton may be more suitable in this context. JToggleButton在这种情况下可能更合适。 The setIcon() method should work as you propose. setIcon()方法应该按照您的建议工作。 For reference, this example updates each button in a List<JToggleButton> from a continually changing List<Icon> . 作为参考,此示例从不断变化的List<Icon>更新List<JToggleButton>中的每个按钮。 It may offer some insight or form a basis of you sscce . 它可能会提供一些见解或者你形成基础SSCCE

图片

The following example shows you what you want to do, but with text rather than icon. 下面的示例显示了您要执行的操作,但带有文本而不是图标。 The program toggles the image, text or whatever you display. 该程序将切换图像,文本或显示的内容。

If the reason you used a JLabel is because you wanted a flat button, you can add the code: 如果使用JLabel的原因是因为想要一个扁平按钮,则可以添加代码:

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

and call this from the 然后从

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

It will then look like a JLabel, but act as a button, which is more logical. 然后,它将看起来像JLabel,但充当按钮,这更加合乎逻辑。

运行程序

Code snippet: 程式码片段:

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

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

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