Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); int width = 500; int height = 100; int x = (screenSize.width - width)>>1; int y = (screenSize.height - height)>>1; this.setBounds(x, y, width, height); } catch(Exception e){ e.printStackTrace(); } }
jProgressBar1.setValue(jProgressBar1.getValue() + 10);
jProgressBar1.setValue(jProgressBar1.getMaximum());
JOptionPane.showMessageDialog(this, "完成!");private JProgressBar jProgressBar1 = new JProgressBar();
Object message, InputStream in)
构造一个对象,以监视输入流的进度。 例如:InputStream in = new BufferedInputStream(
new ProgressMonitorInputStream(
parentComponent,
"Reading " + fileName,
new FileInputStream(fileName)));
这可以创建一个进度监视器,以监视读取输入流的进度
用这个类试试,
事件+监听器的机制进行交互
Definitive Guide to Swing for Java 2, Second Edition
By John Zukowski
ISBN: 1-893115-78-X
Publisher: APress
*/import java.awt.BorderLayout;
import java.awt.Container;import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JProgressBar;
import javax.swing.border.Border;public class ProgressSample {
public static void main(String args[]) {
JFrame f = new JFrame("JProgressBar Sample");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container content = f.getContentPane();
JProgressBar progressBar = new JProgressBar();
progressBar.setValue(25);
progressBar.setStringPainted(true);
Border border = BorderFactory.createTitledBorder("Reading...");
progressBar.setBorder(border);
content.add(progressBar, BorderLayout.NORTH);
f.setSize(300, 100);
f.setVisible(true);
}
}
public class WaitingFrame
extends JWindow
implements WaitingFrameType
{
private static WaitingFrame theObj = null;
private static boolean activity = false;
private JProgressBar progress = null;
private JLabel lblShow = null;
private Frame owner = null;
/**
* 是否激活
* @return Returns the activity.
*/
public static boolean isActivity() {
return activity;
}
/**
*
* @return WaitingFrameType
*/
public static WaitingFrameType getWaitingFrameType(){
return theObj;
} /**
* 设置是否激活
* @param activity The activity to set.
*/
public static void setActivity(boolean activity) {
WaitingFrame.activity = activity;
} /**
* 构造函数
* @param owner
* @param indeterminate 如果进度条是不确定的为true
*/
private WaitingFrame(Frame owner, boolean indeterminate){
super(owner);
try{
this.owner = owner;
this.getContentPane().setLayout(new BorderLayout());
JPanel pnlMain = new JPanel(new BorderLayout());
JPanel pnlTop = new JPanel(new BorderLayout());
ImageIcon img = ResourceManager.getImageIcon("logo.gif");
JLabel label = new JLabel(img);
label.setBorder(new EtchedBorder(EtchedBorder.LOWERED));
pnlTop.add(label, BorderLayout.WEST);
lblShow = new JLabel("", JLabel.CENTER);
pnlTop.add(lblShow, BorderLayout.CENTER);
progress = new JProgressBar();
pnlTop.add(progress, BorderLayout.SOUTH);
progress.setStringPainted(true);
progress.setIndeterminate(indeterminate);
this.getContentPane().add(pnlMain, BorderLayout.CENTER);
pnlMain.add(pnlTop, BorderLayout.NORTH);
pnlMain.setBorder(new EtchedBorder(EtchedBorder.LOWERED));
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int width = 500;
int height = 100;
int x = (screenSize.width - width)>>1;
int y = (screenSize.height - height)>>1;
this.setBounds(x, y, width, height);
}
catch(Exception e){
e.printStackTrace();
}
}
/**
* 显示窗口
* @param owner
* @param showText
* @param indeterminate 如果进度条是不确定的为true
*/
public static void showIt(Frame owner, String showText, boolean indeterminate){
if(theObj == null){
theObj = new WaitingFrame(owner, indeterminate);
}
theObj.progress.setString("");
theObj.lblShow.setText(showText);
setActivity(true);
theObj.setVisible(true);
owner.update(owner.getGraphics());
theObj.update(theObj.getGraphics());
}
/**
* 隐藏窗口
*
*/
public static void hideIt(){
setActivity(false);
theObj.setVisible(false);
theObj.owner.update(theObj.owner.getGraphics());
}
/**
* 更新进度条上的文字
* @param str
*/
public void updateProgress(String str){
updateProgressText(str);
}
/**
* 更新进度条上的文字
* @param str
*/
public static void updateProgressText(String str){
theObj.progress.setString(str);
theObj.update(theObj.getGraphics());
}
/**
* 更新进度条上的文字和刻度
* @param str
* @param value
*/
public static void updateProgressTextAndPos(String str, int value){
theObj.progress.setString(str);
theObj.progress.setValue(value);
theObj.update(theObj.getGraphics());
}
/**
* 初始化进度条
* @param min
* @param max
*/
public static void initProgress(int min, int max){
theObj.progress.setMinimum(min);
theObj.progress.setMaximum(max);
theObj.progress.setValue(min);
}
}
* 等待窗口类型
*/
public interface WaitingFrameType { /**
* 更新进度条上的文字
* @param str
*/
void updateProgress(String str);
}