java窗口进度条_java进度条窗口具体如何制作?
可視化窗口是大部分游戲的常態,今天我們就來了解下如何編寫進度條窗口,快跟小編一起看看吧。
一、在包右擊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进度条窗口具体如何制作?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【软件设计师教程】程序设计语言基础知识
- 下一篇: 申请软著源程序量一般填多少