Creating a nice “LOADING…” animation [duplicate]

天大地大妈咪最大 提交于 2019-11-26 22:51:11

问题


Possible Duplicate:
Pretty alternative to JProgressBar?

I have a process which takes several seconds to load, and I want to create an animation in Java Swing until it finishes loading.

I'd like to avoid using a typical ProgressBar and use a nice modern infinite progress like this one

I'm aware of similar questions but this is strictly Java Swing related. Is there any library or example for this?


回答1:


I'd recommend using the glasspane for this type of activity. The steps involved are:

  • Create a JComponent with BorderLayout. Add a JLabel to the CENTER which includes the animated .gif icon of your choice.
  • (Optional) Override the paint(Graphics) method to make your GUI appear greyed out (or whited out). To achieve this you need to draw a filled rectangle the size of the component, filling the rectangle with a semi-transparent Color.
  • Add the component as the glasspane of your application's root frame.



回答2:


Just use a ImageIcon for this task, it automatically animates gifs. The code below produced this screenshot (the ajax-loader.gif was downloaded from http://www.ajaxload.info/):

Code:

public static void main(String[] args) throws Exception {
    JFrame frame = new JFrame("Test");

    ImageIcon loading = new ImageIcon("ajax-loader.gif");
    frame.add(new JLabel("loading... ", loading, JLabel.CENTER));

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(400, 300);
    frame.setVisible(true);
}



回答3:


Sure it's possible. You can use the tool AjaxLoad to generate an animated image, which can be used in any image/html container.



来源:https://stackoverflow.com/questions/7634402/creating-a-nice-loading-animation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!