简体   繁体   English

如何在动画GIF背景上创建不可见按钮?

[英]How to create invisible button on background of animated GIF?

I was using swing to create a program/start menu for my java program. 我正在使用swing为Java程序创建程序/开始菜单。 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). 我想要的是让我的程序在后台播放GIF动画,同时在gif的特定部分上显示一个不可见的按钮(只有将鼠标悬停在gif上时,它才能显示它的存在)。 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. A)我不确定如何在程序等待按钮按下时如何播放动画gif。 B) How do I make a JButton invisible? B)如何使JButton不可见?

Any help would be appreciated, thanks. 任何帮助,将不胜感激,谢谢。

-JXP -JXP

I never used gif or animated gif in the Swing , but you can use for that this code 我从未在Swing使用过gifgif animated gif ,但是您可以为此代码使用

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

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

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

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