[英]Show loading animation during onclick event in java swing onclick
[英]Java Swing Loading Animation
我想通過java swing實現以下加載動畫:
圓圈必須順時針旋轉。
制作它的最佳方法是什么?
非常感謝你。
只需使用ImageIcon和GIF動畫。 請參閱ImageIcon中的setImageObserver 。
可以使用各種在線生成器(如AjaxLoad)來加載圖標。
希望現在還不算太晚。
我設法通過這種方式在我的JPanel中獲得動畫gif:
private JPanel loadingPanel() {
JPanel panel = new JPanel();
BoxLayout layoutMgr = new BoxLayout(panel, BoxLayout.PAGE_AXIS);
panel.setLayout(layoutMgr);
ClassLoader cldr = this.getClass().getClassLoader();
java.net.URL imageURL = cldr.getResource("img/spinner.gif");
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);
return panel;
}
這種方法的一些要點:
1.圖像文件在jar中;
2. ImageIO.read()返回一個BufferedImage,它不會更新ImageObserver;
3.找到捆綁在jar文件中的圖像的另一種方法是詢問Java類加載器,即加載程序的代碼,以獲取文件。 它知道事物的位置。
所以通過這樣做,我能夠在我的JPanel中獲得我的動畫gif,它就像一個魅力。
您可以使用Animated Icon類使用現有圖標創建自己的動畫。
這可以使用常規Java2D
調用使用自定義組件或自定義圖標繪制。 對我來說,它看起來像一個Arc2D
,它有一個用GradientPaint
繪制的相當厚的BasicStroke
。
或者,從Inkscape(或其他圖形程序)導出幀並將其作為圖像加載。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.