可视化窗口是大部分游戏的常态,今天我们就来了解下如何编写进度条窗口,快跟小编一起看看吧。
一、在包右击new->other->WindowBuilder->JFrame->name叫Guithreadpackage 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.threadpackage 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()
{
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常见问题及解决方法敬请关注奇Q工具网了解详情。
推荐阅读:
今天的文章java窗口进度条_java进度条窗口具体如何制作?分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/27472.html