java进度条窗口具体如何制作?

TheDisguiser 2020-09-12 16:52:24 java常见问答 9849

可视化窗口是大部分游戏的常态,今天我们就来了解下如何编写进度条窗口,快跟小编一起看看吧。

一、在包右击new->other->WindowBuilder->JFrame->name叫Guithread

package guithread;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.GridLayout;
import javax.swing.JProgressBar;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class GuiThread extends JFrame
{
    private JPanel contentPane;
    /**  * Launch the application.  */
    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                try
                {
                    GuiThread frame = new GuiThread();
                    frame.setVisible(true);
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }
            }
        });
    }
    /**  * Create the frame.  */
    public GuiThread()
    {
        setTitle("线程进度条");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(new GridLayout(4, 1, 0, 0));
        JProgressBar pb1 = new JProgressBar();
        contentPane.add(pb1);
        JProgressBar pb2 = new JProgressBar();
        contentPane.add(pb2);
        JButton btnNewButton = new JButton("启动");
        btnNewButton.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                PdThread.flag = true;
                PdThread pt1 = new PdThread(pb1);
                pt1.start();
                PdThread pt2 = new PdThread(pb2);
                pt2
                    .start();
            }
        });
        contentPane.add(btnNewButton);
        JButton btnNewButton_1 = new JButton("暂停");
        btnNewButton_1.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                PdThread.flag = false;
            }
        });
        contentPane.add(btnNewButton_1);
    }
}

二、新建PdThread类 ,Superclass写java.lang.thread

package guithread;
import javax.sound.midi.Track;
import javax.swing.JProgressBar;
public class PdThread extends Thread
{
    static boolean flag = true; //全局开关 JProgressBar jpb;
    public PdThread(JProgressBar jpb)
    {
        this.jpb = jpb;
    }
    public void run()
        {
            while (jpb.getValue() < jpb.getMaximum())
            {
                try
                {
                    Thread.sleep((long(Math.random() * 600));
                    }
                    catch (InterruptedException e)
                    { // TODO Auto-generated catch block e.printStackTrace();
                    }
                    if (PdThread.flag)
                    {
                        jpb.setValue(jpb.getValue() + 1);
                    }
                    else
                    {
                        Thread.yield(); //暂停 this.stop();//终止当前进程
                    }
                }
            }

java进度条窗口

java进度条窗口

以上就是本篇文章的所有内容,更多java常见问题及解决方法敬请关注奇Q工具网了解详情。

推荐阅读:

java进度条时间显示怎么实现?

java进度条重绘要怎么编写?

java进度条功能的实现原理是什么?实例展示