如何用JAVA Swing 做出 登陆界面时的 进度条?
最近做JAVASwing项目想做个登陆时的一个进度条有谁知道用JAVASwing怎么才能做出来啊?...
最近做JAVA Swing项目 想做个登陆时的 一个进度条 有谁知道用JAVA Swing怎么才能做出来啊?
展开
展开全部
使用 JProgressBar 对象可以实现进度条。
//设置最大,最小值
getMinimum(); setMinimum();
getMaximum(); setMaximum();
//设置当前值
getValue();setValue();
//可以取得进度百分比(可以用于判断是否完成100%)
jProgressBar.getPercentComplete()
//创建进度条线程
thread = new Barthread(jProgressBar);
//取得线程中的进度条对象
JProgressBar progressBar = ((Barthread)thread).getJProgressBar();
这些方法基本上用这些就可以实现你的需求了,控件在画面设置就不提了和其他控件大同小异。
//设置最大,最小值
getMinimum(); setMinimum();
getMaximum(); setMaximum();
//设置当前值
getValue();setValue();
//可以取得进度百分比(可以用于判断是否完成100%)
jProgressBar.getPercentComplete()
//创建进度条线程
thread = new Barthread(jProgressBar);
//取得线程中的进度条对象
JProgressBar progressBar = ((Barthread)thread).getJProgressBar();
这些方法基本上用这些就可以实现你的需求了,控件在画面设置就不提了和其他控件大同小异。
展开全部
package test;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;
public class MyTest extends JDialog {
private static final long serialVersionUID = 1395849122103612654L;
private JPanel panel_top = null;
private JPanel panel_mid = null;
private JProgressBar progressBar = null;
private JTextField textField;
private Timer timer;
private int count;
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MyTest dialog = new MyTest();
dialog.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
System.out.println("dialog.count*******00*: "
+ dialog.count);
dialog.timer.start();
dialog.setVisible(true);
Random random = new Random();
// Sleep for up to one second.
try {
Thread.sleep(random.nextInt(1000));
} catch (InterruptedException ignore) {
}
System.out.println("dialog.count*******02*: ");
dialog.setProgressBar(99);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public MyTest() {
super();
System.out.println("TestExecutingDlg() Start0: ");
setTitle("処理中。。。");
setBounds(100, 100, 400, 150);
setResizable(false);
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
final JPanel panel = new JPanel();
panel.setLayout(null);
getContentPane().add(panel);
panel.add(getTopPanel());
panel.add(getMidPanel());
System.out.println("TestExecutingDlg() Start01: ");
timer = new Timer(100, new ActionListener() {
public void actionPerformed(final ActionEvent e) {
if (count >= 100 - 2) {
timer.stop();
progressBar.setValue(count);
count = 0;
} else {
count++;
progressBar.setValue(count);
}
System.out.println(" actionPerformed(): count: " + count);
}
});
timer.start();
System.out.println("TestExecutingDlg() Start02: ");
progressBar.setValue(10);
this.repaint();
}
public int getCount() {
return count;
}
protected JPanel getTopPanel() {
if (panel_top == null) {
panel_top = new JPanel();
panel_top.setLayout(null);
panel_top.setBounds(10, 4, 376, 52);
textField = new JTextField();
textField.setBounds(10, 7, 356, 37);
textField.setText("程序运行中,请稍后...... ");
panel_top.add(textField);
}
return panel_top;
}
protected JPanel getMidPanel() {
if (panel_mid == null) {
panel_mid = new JPanel();
panel_mid.setLayout(null);
panel_mid.setBounds(10, 60, 376, 52);
panel_mid.add(getProgressBar());
}
return panel_mid;
}
protected JProgressBar getProgressBar() {
if (progressBar == null) {
progressBar = new JProgressBar(1, 100);
progressBar.setBounds(10, 5, 354, 37);
progressBar.setStringPainted(true);
progressBar.setValue(0);
progressBar.setStringPainted(true);
}
return progressBar;
}
public int setProgressBar(int nValue) {
System.out.println("setProgressBar()nValue: " + nValue + "count: "
+ count);
if (nValue < 0 || nValue > 100)
return -1;
Random random = new Random();
try {
Thread.sleep(random.nextInt(5000));
} catch (InterruptedException ignore) {
}
progressBar.setValue(nValue);
if (nValue == 100) {
timer.start();
count = nValue - 1;
// Sleep for up to one second.
try {
Thread.sleep(random.nextInt(3000));
} catch (InterruptedException ignore) {
}
if (this.count >= 100) {
this.setVisible(false);
this.dispose();
}
}
return 0;
}
}
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;
public class MyTest extends JDialog {
private static final long serialVersionUID = 1395849122103612654L;
private JPanel panel_top = null;
private JPanel panel_mid = null;
private JProgressBar progressBar = null;
private JTextField textField;
private Timer timer;
private int count;
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MyTest dialog = new MyTest();
dialog.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
System.out.println("dialog.count*******00*: "
+ dialog.count);
dialog.timer.start();
dialog.setVisible(true);
Random random = new Random();
// Sleep for up to one second.
try {
Thread.sleep(random.nextInt(1000));
} catch (InterruptedException ignore) {
}
System.out.println("dialog.count*******02*: ");
dialog.setProgressBar(99);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public MyTest() {
super();
System.out.println("TestExecutingDlg() Start0: ");
setTitle("処理中。。。");
setBounds(100, 100, 400, 150);
setResizable(false);
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
final JPanel panel = new JPanel();
panel.setLayout(null);
getContentPane().add(panel);
panel.add(getTopPanel());
panel.add(getMidPanel());
System.out.println("TestExecutingDlg() Start01: ");
timer = new Timer(100, new ActionListener() {
public void actionPerformed(final ActionEvent e) {
if (count >= 100 - 2) {
timer.stop();
progressBar.setValue(count);
count = 0;
} else {
count++;
progressBar.setValue(count);
}
System.out.println(" actionPerformed(): count: " + count);
}
});
timer.start();
System.out.println("TestExecutingDlg() Start02: ");
progressBar.setValue(10);
this.repaint();
}
public int getCount() {
return count;
}
protected JPanel getTopPanel() {
if (panel_top == null) {
panel_top = new JPanel();
panel_top.setLayout(null);
panel_top.setBounds(10, 4, 376, 52);
textField = new JTextField();
textField.setBounds(10, 7, 356, 37);
textField.setText("程序运行中,请稍后...... ");
panel_top.add(textField);
}
return panel_top;
}
protected JPanel getMidPanel() {
if (panel_mid == null) {
panel_mid = new JPanel();
panel_mid.setLayout(null);
panel_mid.setBounds(10, 60, 376, 52);
panel_mid.add(getProgressBar());
}
return panel_mid;
}
protected JProgressBar getProgressBar() {
if (progressBar == null) {
progressBar = new JProgressBar(1, 100);
progressBar.setBounds(10, 5, 354, 37);
progressBar.setStringPainted(true);
progressBar.setValue(0);
progressBar.setStringPainted(true);
}
return progressBar;
}
public int setProgressBar(int nValue) {
System.out.println("setProgressBar()nValue: " + nValue + "count: "
+ count);
if (nValue < 0 || nValue > 100)
return -1;
Random random = new Random();
try {
Thread.sleep(random.nextInt(5000));
} catch (InterruptedException ignore) {
}
progressBar.setValue(nValue);
if (nValue == 100) {
timer.start();
count = nValue - 1;
// Sleep for up to one second.
try {
Thread.sleep(random.nextInt(3000));
} catch (InterruptedException ignore) {
}
if (this.count >= 100) {
this.setVisible(false);
this.dispose();
}
}
return 0;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JProgressBar;
public class cc extends JFrame implements Runnable{
private JProgressBar probar = null;
private int ix=0; //初始值
private int speed=50; //调整速度
private int timer=300; //调整时间 1000==1毫秒
public cc() {
init();
Thread tt=new Thread(this);
tt.start();
this.add(probar);
this.setSize(300, 70);
this.setLocation(400, 300);
this.setVisible(true);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void init() {
probar = new JProgressBar(ix, 100);
probar.setStringPainted(true);
}
public static void main(String[] args) {
new cc();
}
public void run() {
while(true){
if(ix >100) break;
try {
int t =(int)(Math.random()*3+speed);
probar.setValue(ix+=t);
Thread.sleep(timer);
System.out.println("ix =" + ix);
} catch (Exception e) {}
}
JOptionPane.showMessageDialog(this, "处理Ok~~~");
}
}
import javax.swing.JOptionPane;
import javax.swing.JProgressBar;
public class cc extends JFrame implements Runnable{
private JProgressBar probar = null;
private int ix=0; //初始值
private int speed=50; //调整速度
private int timer=300; //调整时间 1000==1毫秒
public cc() {
init();
Thread tt=new Thread(this);
tt.start();
this.add(probar);
this.setSize(300, 70);
this.setLocation(400, 300);
this.setVisible(true);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void init() {
probar = new JProgressBar(ix, 100);
probar.setStringPainted(true);
}
public static void main(String[] args) {
new cc();
}
public void run() {
while(true){
if(ix >100) break;
try {
int t =(int)(Math.random()*3+speed);
probar.setValue(ix+=t);
Thread.sleep(timer);
System.out.println("ix =" + ix);
} catch (Exception e) {}
}
JOptionPane.showMessageDialog(this, "处理Ok~~~");
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
ProgressMonitorDialog
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询