我创建了一个无限透明的加载jframe,但是当通过调用f.setBackground(new Color(0,0,0,0))时将Background设置为透明时闪烁;如果setBackground被注释,则JFrame显示动画图标正确。
有人可以解决这个问题吗?
import java.awt.Color;
import java.awt.EventQueue;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class InfiniteLoad extends JPanel
{
private JPanel loadingPanel() throws MalformedURLException
{
JPanel panel = new JPanel();
BoxLayout layoutMgr = new BoxLayout(panel, BoxLayout.PAGE_AXIS);
panel.setLayout(layoutMgr);
java.net.URL imageURL = new URL("http://oi59.tinypic.com/106a6vo.jpg");
ImageIcon imageIcon = new ImageIcon(imageURL);
JLabel iconLabel = new JLabel();
iconLabel.setIcon(imageIcon);
imageIcon.setImageObserver(iconLabel);
JLabel label = new JLabel("Loading...");
panel.add(iconLabel);
panel.add(label);
panel.setOpaque(false);
return panel;
}
public static void main(String[] args) throws MalformedURLException
{
EventQueue.invokeLater(new Runnable()
{
@Override
public void run()
{
JFrame f = new JFrame("Window");
f.setUndecorated(true);
//AWTUtilities.setWindowOpacity(f, 0.6f);
//f.setOpacity(0.4f);
InfiniteLoad imagePanel = new InfiniteLoad();
JPanel jp = null;
try
{
jp = imagePanel.loadingPanel();
}
catch (MalformedURLException ex)
{
Logger.getLogger(InfiniteLoad.class.getName()).log(Level.SEVERE, null, ex);
}
f.setContentPane(jp);
f.setBackground(new Color(0, 0, 0, 0)); //<-- THIS CAUSES FLICKERING
f.pack();
f.setVisible(true);
}
});
}
}
修改
Ok问题与OpenJDK Java有关,它显示了
提到的问题java version "1.7.0_55"
OpenJDK Runtime Environment (IcedTea 2.4.7) (7u55-2.4.7-1ubuntu1~0.13.10.1)
OpenJDK 64-Bit Server VM (build 24.51-b03, mixed mode)
用运行时
java version "1.7.0_45"
Java(TM) SE Runtime Environment (build 1.7.0_45-b18)
Java HotSpot(TM) 64-Bit Server VM (build 24.45-b08, mixed mode)
显示正确。 如果任何人都可以展示一种解决方法,使其在错误实施下也能正常工作,那么欢迎您。
答案 0 :(得分:1)
抱歉,我不明白......哪个按钮?我的代码中没有按钮
我总是在未修饰的JButton中测试动画图像(可能是我的坏习惯之一)
找不到(不是最深入的研究)保存为JPEG的良好动画图像有效(所有都是透明的,在我的帖子中没有任何东西可以从代码中看到,然后没有任何闪烁)
动画GIF无法解决问题
码
import com.sun.awt.AWTUtilities;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Insets;
import java.net.MalformedURLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
//http://stackoverflow.com/q/24797774/714968
public class InfiniteLoad extends JPanel {
private JPanel loadingPanel() throws MalformedURLException {
JPanel panel = new JPanel();
BoxLayout layoutMgr = new BoxLayout(panel, BoxLayout.PAGE_AXIS);
panel.setLayout(layoutMgr);
ImageIcon imageIcon = new ImageIcon((getClass().getResource("/Images/smiley_018.gif")));
JButton button = new JButton();
button.setBorderPainted(false);
button.setBorder(null);
button.setFocusable(false);
button.setMargin(new Insets(0, 0, 0, 0));
button.setContentAreaFilled(false);
button.setIcon(imageIcon);
JLabel myLabel = new JLabel(imageIcon);
JLabel label = new JLabel("Loading...", JLabel.CENTER);
panel.add(button);
panel.add(label);
panel.add(myLabel);
panel.setOpaque(false);
return panel;
}
public static void main(String[] args) throws MalformedURLException {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame f = new JFrame("Window");
f.setUndecorated(true);
//AWTUtilities.setWindowOpacity(f, 0.1f);
//f.setOpacity(0.1f);
InfiniteLoad imagePanel = new InfiniteLoad();
JPanel jp = null;
try {
jp = imagePanel.loadingPanel();
} catch (MalformedURLException ex) {
Logger.getLogger(InfiniteLoad.class.getName()).log(Level.SEVERE, null, ex);
}
f.setContentPane(jp);
f.setBackground(new Color(0, 0, 0, 0)); //<-- THIS CAUSES FLICKERING
f.pack();
f.setVisible(true);
}
});
}
}