㈠ java中如何實現進度條效果
代碼如下:import java.awt.Color; import java.awt.Toolkit; import javax.swing.ImageIcon; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JProgressBar; import javax.swing.JWindow; @SuppressWarnings("serial") public class Demo extends JWindow implements Runnable { // 定義載入窗口大小 public static final int LOAD_WIDTH = 455; public static final int LOAD_HEIGHT = 295; // 獲取屏幕窗口大小 public static final int WIDTH = Toolkit.getDefaultToolkit().getScreenSize().width; public static final int HEIGHT = Toolkit.getDefaultToolkit().getScreenSize().height; // 定義進度條組件 public JProgressBar progressbar; // 定義標簽組件 public JLabel label; // 構造函數 public Demo() { // 創建標簽,並在標簽上放置一張圖片 label = new JLabel(new ImageIcon("images/background.jpg")); label.setBounds(0, 0, LOAD_WIDTH, LOAD_HEIGHT - 15); // 創建進度條 progressbar = new JProgressBar(); // 顯示當前進度值信息 progressbar.setStringPainted(true); // 設置進度條邊框不顯示 progressbar.setBorderPainted(false); // 設置進度條的前景色 progressbar.setForeground(new Color(0, 210, 40)); // 設置進度條的背景色 progressbar.setBackground(new Color(188, 190, 194)); progressbar.setBounds(0, LOAD_HEIGHT - 15, LOAD_WIDTH, 15); // 添加組件 this.add(label); this.add(progressbar); // 設置布局為空 this.setLayout(null); // 設置窗口初始位置 this.setLocation((WIDTH - LOAD_WIDTH) / 2, (HEIGHT - LOAD_HEIGHT) / 2); // 設置窗口大小 this.setSize(LOAD_WIDTH, LOAD_HEIGHT); // 設置窗口顯示 this.setVisible(true); } public static void main(String[] args) { Demo t = new Demo(); new Thread(t).start(); } @Override public void run() { for (int i = 0; i < 100; i++) { try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } progressbar.setValue(i); } JOptionPane.showMessageDialog(this, "載入完成"); this.dispose(); } } 效果圖:
㈡ java帶參帶進度條(真實)上傳文件代碼
思路如下:
public class MyClass extends Thread {
public JProgressBar bar = new JProgressBar();//進度條
public OutputStream os;// 通向伺服器的流
public InputStream is;// 你的文件流
public long len;// 你文件的總長度
public void run() {
byte[] buff = new byte[1024];
long passedlen = 0;
try {
int mark = 0;
while (true) {
mark = is.read(buff, 0, 1024);
os.write(buff, 0, mark);
// 更新進度
passedlen += mark;
int prog = (int) ((double) passedlen / (len == 0 ? 1 : len) * 100);
// 改變進度條的value值
bar.setValue(prog);
// 結束判定
if (passedlen == len) {
break;
}
}
os.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
㈢ java讀取文件時如何獲得已讀取的位元組大小
//查看例子即可。
importjava.awt.BorderLayout;
importjava.awt.Cursor;
importjava.awt.Toolkit;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.beans.PropertyChangeEvent;
importjava.beans.PropertyChangeListener;
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileOutputStream;
importjava.io.InputStream;
importjava.io.OutputStream;
importjavax.swing.JButton;
importjavax.swing.JFrame;
importjavax.swing.JProgressBar;
importjavax.swing.SwingWorker;
{
=1L;
privatestaticfinalintMIN=0;
privatestaticfinalintMAX=100;
;
privateJButton;
privateTasktask;
publicBar(Stringtitle)
{
setTitle(title);
setLayout(newBorderLayout());
progressBar=newJProgressBar(MIN,MAX);
progressBar.setValue(MIN);
progressBar.setStringPainted(true);
add(progressBar,BorderLayout.CENTER);
=newJButton("拷貝");
.addActionListener(newActionListener()
{
@Override
publicvoidactionPerformed(ActionEvente)
{
start();
.setEnabled(false);
}
});
add(,BorderLayout.EAST);
}
classTaskextendsSwingWorker<Void,Void>
{
@Override
publicVoiddoInBackground()
{
setProgress(MIN);
Filefile=newFile("a.jpg");
longsize=file.length();
byte[]bts=newbyte[(int)(size/(Math.random()*50+MAX-50))];
intlength=0;
longcLength=0;
try
{
InputStreamstream=newFileInputStream(file);
OutputStreamos=newFileOutputStream(newFile("_aa.jpg"));
while((length=stream.read(bts))>0)
{
os.write(bts,0,length);
cLength+=length;
setProgress((int)(cLength*MAX/size));
Thread.sleep(30);
}
os.flush();
os.close();
stream.close();
}
catch(Exceptione)
{
e.printStackTrace();
}
returnnull;
}
@Override
publicvoiddone()
{
setTitle("拷貝完畢");
Toolkit.getDefaultToolkit().beep();
.setEnabled(true);
setCursor(null);
}
}
publicvoidpropertyChange(PropertyChangeEventevt)
{
if("progress"==evt.getPropertyName())
{
intprogress=(Integer)evt.getNewValue();
progressBar.setValue(progress);
}
}
privatevoidstart()
{
task=newTask();
task.addPropertyChangeListener(this);
task.execute();
setTitle("正在拷貝...");
setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
}
()
{
Barframe=newBar("Bar");
frame.setAlwaysOnTop(true);
frame.setSize(280,60);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
publicstaticvoidmain(String[]args)
{
javax.swing.SwingUtilities.invokeLater(newRunnable()
{
publicvoidrun()
{
createAndShowGUI();
}
});
}
}
㈣ Java中如何實現進度條效果
代碼如下:import java.awt.Color; import java.awt.Toolkit; import javax.swing.ImageIcon; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JProgressBar; import javax.swing.JWindow; @SuppressWarnings("serial") public class Demo extends JWindow implements Runnable { // 定義載入窗口大小 public static final int LOAD_WIDTH = 455; public static final int LOAD_HEIGHT = 295; // 獲取屏幕窗口大小 public static final int WIDTH = Toolkit.getDefaultToolkit().getScreenSize().width; public static final int HEIGHT = Toolkit.getDefaultToolkit().getScreenSize().height; // 定義進度條組件 public JProgressBar progressbar; // 定義標簽組件 public JLabel label; // 構造函數 public Demo() { // 創建標簽,並在標簽上放置一張圖片 label = new JLabel(new ImageIcon("images/background.jpg")); label.setBounds(0, 0, LOAD_WIDTH, LOAD_HEIGHT - 15); // 創建進度條 progressbar = new JProgressBar(); // 顯示當前進度值信息 progressbar.setStringPainted(true); // 設置進度條邊框不顯示 progressbar.setBorderPainted(false); // 設置進度條的前景色 progressbar.setForeground(new Color(0, 210, 40)); // 設置進度條的背景色 progressbar.setBackground(new Color(188, 190, 194)); progressbar.setBounds(0, LOAD_HEIGHT - 15, LOAD_WIDTH, 15); // 添加組件 this.add(label); this.add(progressbar); // 設置布局為空 this.setLayout(null); // 設置窗口初始位置 this.setLocation((WIDTH - LOAD_WIDTH) / 2, (HEIGHT - LOAD_HEIGHT) / 2); // 設置窗口大小 this.setSize(LOAD_WIDTH, LOAD_HEIGHT); // 設置窗口顯示 this.setVisible(true); } public static void main(String[] args) { Demo t = new Demo(); new Thread(t).start(); } @Override public void run() { for (int i = 0; i < 100; i++) { try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } progressbar.setValue(i); } JOptionPane.showMessageDialog(this, "載入完成"); this.dispose(); } } 效果圖:
㈤ java的進度條問題 這個程序如何實現一運行程序進度條就自動走 謝謝
public class NewJFrame extends javax.swing.JFrame implements ActionListener, ChangeListener {
public NewJFrame() {
initComponents();
Timer timer = new Timer(50, this);
timer.setRepeats(true);
timer.start();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jProgressBar1 = new javax.swing.JProgressBar();
jLabel1 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setAlwaysOnTop(true);
getContentPane().setLayout(new java.awt.CardLayout());
jPanel1.setPreferredSize(new java.awt.Dimension(600, 350));
jPanel1.setVerifyInputWhenFocusTarget(false);
jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
jPanel1.add(jProgressBar1, new org.netbeans.lib.awtextra.AbsoluteConstraints(310, 294, 270, 10));
// jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/splashTimer.jpg"))); // NOI18N
jLabel1.setMaximumSize(new java.awt.Dimension(600, 350));
jLabel1.setMinimumSize(new java.awt.Dimension(600, 350));
jLabel1.setPreferredSize(new java.awt.Dimension(600, 350));
jPanel1.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, -1, -1));
getContentPane().add(jPanel1, "card2");
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JLabel jLabel1;
private javax.swing.JPanel jPanel1;
private javax.swing.JProgressBar jProgressBar1;
// End of variables declaration
public void stateChanged(ChangeEvent e1) {
int value = jProgressBar1.getValue();
if (e1.getSource() == jProgressBar1) {
}
}
public void actionPerformed(ActionEvent e) {
int value = jProgressBar1.getValue();
if (value < 100) {
value++;
jProgressBar1.setValue(value);
} else {
jProgressBar1.setValue(0);
}
}
}
終於看到個用netbeans的人了。。