简体   繁体   中英

How to create invisible button on background of animated GIF?

I was using swing to create a program/start menu for my java program. What I want is for my program to play an animated GIF in the background, while an invisible button over a certain section of the gif (which only reveals its presence if you mouse over it). My problems are thus:

A) I am not sure how to get the animated gif to play while to program waits for a button press. B) How do I make a JButton invisible?

Any help would be appreciated, thanks.

-JXP

I never used gif or animated gif in the Swing , but you can use for that this code

在此处输入图片说明在此处输入图片说明

import java.awt.Insets;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

public class MyToggleButton extends JFrame {

    private static final long serialVersionUID = 1L;
    private Icon errorIcon = UIManager.getIcon("OptionPane.errorIcon");
    private Icon infoIcon = UIManager.getIcon("OptionPane.informationIcon");
    private Icon warnIcon = UIManager.getIcon("OptionPane.warningIcon");

    public MyToggleButton() {
        final JButton toggleButton = new JButton();
        toggleButton.setBorderPainted(false);
        toggleButton.setBorder(null);
        toggleButton.setFocusable(false);
        toggleButton.setMargin(new Insets(0, 0, 0, 0));
        toggleButton.setContentAreaFilled(false);
        toggleButton.setIcon((errorIcon));
        toggleButton.setSelectedIcon(infoIcon);
        toggleButton.setRolloverIcon((infoIcon));
        toggleButton.setPressedIcon(warnIcon);
        toggleButton.setDisabledIcon(warnIcon);
        add(toggleButton);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                MyToggleButton t = new MyToggleButton();
            }
        });
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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