1.java 如何取得当前时间精确到秒,毫秒 2.Java 如何在一个大循环里如何显示进度条(100分)

  • 主题发起人 主题发起人 goddy
  • 开始时间 开始时间
G

goddy

Unregistered / Unconfirmed
GUEST, unregistred user!
Java 如何在一个大循环里如何显示进度条
for(int i=0;i<0XFFFF;i++)
{
do something;
// 显示进度条并刷新页面,我现在是页面刷新不了好像死机似的
}
 
1.
我用 System.currentTimeMillis() 获取当前时间,需要说得是,这个值的精确度不够
经常会有多有少,但是我也没有什么其它的办法。
2.
最好用进程实现,我实现过的,不会很难的。
 
To DragonPC_???:谢谢你啦,第一个问题你是对的,第二个问题能说明白一点吗??
 
public class TestFrame extends JFrame implements Runnable{
...
...
void btnTestActionPerformed(ActionEvent e) {
start() ;
for (int i=0;
i<100;
i++) {
try {
Thread.sleep(50);
} catch (Exception ex) {}
setBarValue(i) ;
}
}
int nValue = 0 ;
JProgressBar bar = new JProgressBar() ;
synchronized void setBarValue(int nValue) {
this.nValue = nValue ;
}
synchronized int getBarValue() {
return nValue ;
}
Thread timer = null ;
public void start () {
if (timer == null) {
timer = new Thread(this) ;
timer.start() ;
}
}
public void stop() {
timer = null ;
}
public void run() {
while((timer != null) &amp;&amp;
(getBarValue() < 100)) {
try {
Thread.currentThread().sleep(10);
} catch (Exception ee) {}
bar.setValue(getBarValue());
bar.paintImmediately(0,0,bar.getWidth(), bar.getHeight());
}
}
}
一个JBuilder测试代码,关键需要调用 paintImmediately 方法, :-)
 
我試試看等一下給你加分
 
DragonPC_??? 兄:
我的代碼是這樣寫的怎一點反應都沒有
還有我把 timer = new Thread(this) ;
改成 timer = new Thread() ;
要不我編譯不能通過 能幫我看看嗎
謝謝
package myapplit;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import java.beans.*;
import com.borland.jbcl.layout.*;
import com.borland.dbswing.*;
import java.lang.Object.*;
import java.lang.Thread.*;
public class Applet1 extends JApplet {
boolean isStandalone = false;
public XYLayout xYLayout1;
public JPanel jPanel1;
public XYLayout xYLayout2;
public Button button1;
public MenuBar menuBar1;
JProgressBar JB = new JProgressBar();
JLabel jLabel2 = new JLabel();
/**Get a parameter value*/
public String getParameter(String key, String def) {
return isStandalone ? System.getProperty(key, def) :
(getParameter(key) != null ? getParameter(key) : def);
}
/**Construct the applet*/
public Applet1() {
}
/**Initialize the applet*/
public void init() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
/**Component initialization*/
private void jbInit() throws Exception {
xYLayout1 = (XYLayout) Beans.instantiate(getClass().getClassLoader(), XYLayout.class.getName());
jPanel1 = (JPanel) Beans.instantiate(getClass().getClassLoader(), JPanel.class.getName());
xYLayout2 = (XYLayout) Beans.instantiate(getClass().getClassLoader(), XYLayout.class.getName());
button1 = (Button) Beans.instantiate(getClass().getClassLoader(), Button.class.getName());
menuBar1 = (MenuBar) Beans.instantiate(getClass().getClassLoader(), MenuBar.class.getName());
this.setForeground(Color.white);
this.getContentPane().setBackground(Color.lightGray);
this.setSize(new Dimension(400,300));
this.getContentPane().setLayout(xYLayout1);
jPanel1.setLayout(xYLayout2);
button1.setLabel("button1");
button1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
button1_actionPerformed(e);
}
});
jLabel2.setText("jLabel2");
this.getContentPane().add(jPanel1, new XYConstraints(0, 0, 383, 249));
jPanel1.add(JB, new XYConstraints(21, 97, 249, -1));
jPanel1.add(button1, new XYConstraints(174, 182, 76, 27));
jPanel1.add(jLabel2, new XYConstraints(34, 122, -1, -1));
}
/**Get Applet information*/
public String getAppletInfo() {
return "Applet Information";
}
/**Get parameter info*/
public String[][] getParameterInfo() {
return null;
}
/**Main method*/
public static void main(String[] args) {
Applet1 applet = new Applet1();
applet.isStandalone = true;
JFrame frame = new JFrame();
//EXIT_ON_CLOSE == 3
frame.setDefaultCloseOperation(3);
frame.setTitle("Applet Frame");
frame.getContentPane().add(applet, BorderLayout.CENTER);
applet.init();
applet.start();
frame.setSize(400,320);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation((d.width - frame.getSize().width) / 2, (d.height - frame.getSize().height) / 2);
frame.setVisible(true);
}
//static initializer for setting look &amp;
feel
static {
try {
//UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
//UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
}
catch(Exception e) {
}
}


void jComboBox1_itemStateChanged(ItemEvent e) {
}
void button1_actionPerformed(ActionEvent e) {
start() ;
for (int i=0;
i<100;
i++) {
try {
// Thread.sleep(50);
} catch (Exception ex) {}
setBarValue(i) ;
}
}
int nValue = 0 ;
synchronized void setBarValue(int nValue) {
this.nValue = nValue ;
}
synchronized int getBarValue() {
return nValue ;
}
Thread timer = null ;
public void start () {
if (timer == null) {
timer = new Thread() ;
timer.start() ;
}
}
public void stop() {
timer = null ;
}
public void run() {
while((timer != null) &amp;&amp;
(getBarValue() < 100)) {
try {
Thread.currentThread().sleep(10);
} catch (Exception ee) {}
JB.setValue(getBarValue());
jLabel2.setText(Integer.toString(getBarValue()));
jLabel2.paintImmediately(0,0,jLabel2.getWidth(), jLabel2.getHeight());
JB.paintImmediately(0,0,JB.getWidth(), JB.getHeight());
}
}

}
 
可以拉 ,我忘了加implements Runnable
 
后退
顶部