如何在java中创建加载栏

时间:2016-02-17 15:03:48

标签: java swing progress-bar components loading

我想知道当我点击按钮时如何为进度条设置循环。 我已经尝试了一个循环,但这是浪费时间。我附上了我想要的图片。There is button in the North of the form and there is Progress Bar in the south

1 个答案:

答案 0 :(得分:0)

enter image description here

这是一个简单的进度条示例,当您将鼠标悬停在按钮上时(基于您的图片),当鼠标悬停在按钮上时,您可以在线程中执行背景内容: -

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JProgressBar;

public class GUI extends JFrame 
{
boolean isMouseOver = false;
int count = 0;

public GUI()
{
    this.setTitle("Progress Bar");
    this.setBounds(100, 100, 407, 119);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.getContentPane().setLayout(null);

    JProgressBar progressBar = new JProgressBar();
    progressBar.setBounds(10, 45, 371, 22);
    this.getContentPane().add(progressBar);

    JButton btnMousOver = new JButton("Mouse Over");
    btnMousOver.addMouseListener(new MouseAdapter()
    {
        public void mouseEntered(MouseEvent evt)
        {
            isMouseOver = true;
            btnMousOver.setEnabled(false);
            Thread go = new Thread()
            {
                public void run()
                {
                    while (isMouseOver && count < 101)
                    {
                        count = count + 2;
                        progressBar.setValue(count);
                        // do some stuffs here
                        try
                        {
                            Thread.sleep(100);
                        }
                        catch (InterruptedException e)
                        {
                            e.printStackTrace();
                        }
                    }
                }
            };
            go.start();

        }

        public void mouseExited(MouseEvent evt)
        {
            isMouseOver = false;
            btnMousOver.setEnabled(true);
        }

        public void mousePressed(MouseEvent evt)
        {
            JOptionPane.showMessageDialog(null, "Mouse CLicked");
        }

        public void mouseReleased(MouseEvent evt)
        {
            JOptionPane.showMessageDialog(null, "Mouse released");
        }
    });
    btnMousOver.setBounds(142, 11, 108, 23);

    this.getContentPane().add(btnMousOver);
    this.getContentPane().add(progressBar);
    this.setLocationRelativeTo(null);
    this.setVisible(true);

}

public static void main(String[] args)
{
    new GUI();
}
}