主题:[原创]进度条制作,让大家一起分享!
arroncaijing
[专家分:610] 发布于 2005-10-19 10:03:00
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
public class ProgressBarDemo implements ActionListener,ChangeListener
{
JFrame f = null;
JProgressBar progressbar;
JLabel label;
Timer timer;
JButton b;
public ProgressBarDemo()
{
f = new JFrame("progressbar Example");
Container contentPane = f.getContentPane();
label = new JLabel(" ",JLabel.CENTER);
progressbar = new JProgressBar();
progressbar.setOrientation(JProgressBar.HORIZONTAL);
progressbar.setMinimum(0);
progressbar.setMaximum(100);
progressbar.setValue(0);
progressbar.setStringPainted(true);
progressbar.addChangeListener(this);
progressbar.setPreferredSize(new Dimension(200,20));
progressbar.setBorderPainted(true);
progressbar.setBackground(Color.pink);
JPanel panel = new JPanel();
b = new JButton("Start");
b.setForeground(Color.orange);
b.addActionListener(this);
panel.add(b);
timer = new Timer(50,this);
contentPane.add(panel,BorderLayout.NORTH);
contentPane.add(progressbar,BorderLayout.CENTER);
contentPane.add(label,BorderLayout.SOUTH);
f.pack();
f.setVisible(true);
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public static void main(String[] args)
{
new ProgressBarDemo();
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == b)
{
timer.start();
}
if(e.getSource() == timer)
{
int value = progressbar.getValue();
if( value < 100)
{
value++;
progressbar.setValue(value);
}
else
{
timer.stop();
// new bAndW.BAndW();
// Report.ReportComplier@1bf6770
// new Report.ReportComplier();
//System.out.println(new Report.ReportComplier());
Double dValue=new Double(1233.213f);
System.out.println(dValue);
f.dispose();
}
}
}
public void stateChanged(ChangeEvent e1)
{
int value = progressbar.getValue();
if(e1.getSource() == progressbar)
{
label.setText("目前已完成进度:"+Integer.toString(value)+" %");
label.setForeground(Color.blue);
}
}
}
最后更新于:2007-05-22 16:39:00
回复列表 (共26个回复)
11 楼
arroncaijing [专家分:610] 发布于 2007-06-01 16:42:00
上次谁在我其他贴里要进度条的代码的,这里有.
12 楼
majb007 [专家分:0] 发布于 2007-06-01 23:36:00
楼主还不错拉
这个都拿出来分享拉
13 楼
416177937 [专家分:0] 发布于 2007-06-05 19:21:00
我是才来的,我不是很明白,我们回帖对楼主有什么作用啊??
在我看来,认为有必要说几句就回,没必要就不要回,千万不要回一些没用的东西.
这样会影响别人的察看.
14 楼
loveme1985 [专家分:200] 发布于 2007-06-09 10:10:00
知识共享
15 楼
arroncaijing [专家分:610] 发布于 2007-07-20 15:45:00
谢谢大家光顾!~~~~~~~~~~~~~~~~~```
16 楼
yogafrank [专家分:440] 发布于 2007-07-22 23:38:00
不错啊. 前断时间我写传输文件的时候用到过. 不错哦.
17 楼
Finder [专家分:1460] 发布于 2007-07-23 11:22:00
楼主辛苦了。只是想问一下,能不能给出按照JFrame加载的进度给出的进度条呐?就像Eclipse的那种,或者像JDK Demo里面Swing2D的那个样子的。看它的代码一直没有看懂呐。
18 楼
arroncaijing [专家分:610] 发布于 2007-07-24 15:06:00
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Shell;
public class ProgressBarExample {
Display d;
Shell s;
ProgressBarExample() {
d = new Display();
s = new Shell(d);
s.setSize(250, 250);
s.setText("A ProgressBar Example");
final ProgressBar pb = new ProgressBar(s, SWT.HORIZONTAL);
pb.setMinimum(0);
pb.setMaximum(100);
pb.setSelection(50);
pb.setBounds(10, 10, 200, 20);
s.open();
while (!s.isDisposed()) {
if (!d.readAndDispatch())
d.sleep();
}
d.dispose();
}
public static void main() {
new ProgressBarExample();
}
}
19 楼
Finder [专家分:1460] 发布于 2007-07-24 16:21:00
我的意思是,能不能做出不是那种匀速进度的进度条,而是按照JFrame的加载速度更新进度条啊?
20 楼
行者买刀 [专家分:550] 发布于 2007-07-24 21:08:00
还不错,
这里也带上我的一个屏保程序
一起学习
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.DisplayMode;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.geom.Rectangle2D;
import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
*
* @author tewang
*/
public class ScreenTest extends JPanel implements Runnable{
/** Creates a new instance of ScreenTest */
private String time;
private Rectangle2D rd;
private int x,y,a,b;//座标值和方向值
private int rgb=0;//颜色的值
private Color color;//字体的颜色
private int width,height;//屏幕的大小
public ScreenTest(int width,int height) {
this.width=width;
this.height=height;
initWindow();
}
private void initWindow(){
x=(int)(Math.random()*300);
y=(int)(Math.random()*500);
a=1;
b=1;
this.setBackground(Color.BLACK);
this.setOpaque(true);
rd=new Rectangle2D.Double(10,10,101,10);
color=Color.YELLOW;
}
private void doColor(){
rgb=(int)(Math.random()*0xFFFFFF);
color=new Color(rgb);
}
public void paintComponent(Graphics g){
g.setColor(Color.BLACK);
g.fillRect(0,0,width,height);
g.setColor(color);
g.setFont(new Font("楷书",Font.BOLD,15));
FontMetrics fm=g.getFontMetrics();
rd=fm.getStringBounds(time,g);
g.drawString(time,x,(int)(y+rd.getHeight()));
}
private void doTime(){
Calendar cal=Calendar.getInstance();
DateFormat df=DateFormat.getTimeInstance(DateFormat.MEDIUM);
Date date=cal.getTime();
time=df.format(date);
x+=a;
y+=b;
double width1=rd.getWidth();
double height1=rd.getHeight();
rd.setRect(x,y,width1,height1);
if(rd.intersectsLine(width,0,width,height)){
doColor();
a=-1;
} else if(rd.intersectsLine(0,0,0,height)){
doColor();
a=1;
} else if(rd.getY()<=-80){
doColor();
b=1;
} else if(rd.intersectsLine(0,height,width,height)){
doColor();
b=-1;
}
}
public void run(){
while(true){
try{
Thread.sleep(2);
doTime();
repaint();
} catch(InterruptedException ie){
ie.printStackTrace();
}
}
}
public static void main(String arsg[]){
GraphicsDevice gd=GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
DisplayMode dm=gd.getDisplayMode();
int width=dm.getWidth();
int height=dm.getHeight();
System.out.println("width="+width+"\nheight="+height);
ScreenTest st=new ScreenTest(width,height);
final JFrame jf=new JFrame();
jf.getContentPane().add(st,BorderLayout.CENTER);
jf.setUndecorated(true);
gd.setFullScreenWindow(jf);
new Thread(st).start();
jf.addKeyListener(new KeyAdapter(){
public void keyReleased(KeyEvent ke){
if(ke.getKeyCode()==KeyEvent.VK_NUMPAD0)
System.exit(0);
}
});
}
}
我来回复