java的进度条问题 这个程序如何实现一运行程序进度条就自动走 谢谢

java的进度条问题 这个程序如何实现一运行程序进度条就自动走 谢谢,第1张

public class NewJFrame extends javaxswingJFrame implements ActionListener, ChangeListener {
public NewJFrame() {
initComponents();
Timer timer = new Timer(50, this);
timersetRepeats(true);
timerstart();
}
/ 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 javaxswingJPanel();
jProgressBar1 = new javaxswingJProgressBar();
jLabel1 = new javaxswingJLabel();
setDefaultCloseOperation(javaxswingWindowConstantsEXIT_ON_CLOSE);
setAlwaysOnTop(true);
getContentPane()setLayout(new javaawtCardLayout());
jPanel1setPreferredSize(new javaawtDimension(600, 350));
jPanel1setVerifyInputWhenFocusTarget(false);
jPanel1setLayout(new orgnetbeanslibawtextraAbsoluteLayout());
jPanel1add(jProgressBar1, new orgnetbeanslibawtextraAbsoluteConstraints(310, 294, 270, 10));
// jLabel1setIcon(new javaxswingImageIcon(getClass()getResource("/splashTimerjpg"))); // NOI18N
jLabel1setMaximumSize(new javaawtDimension(600, 350));
jLabel1setMinimumSize(new javaawtDimension(600, 350));
jLabel1setPreferredSize(new javaawtDimension(600, 350));
jPanel1add(jLabel1, new orgnetbeanslibawtextraAbsoluteConstraints(0, 0, -1, -1));
getContentPane()add(jPanel1, "card2");
pack();
}// </editor-fold>
/
@param args the command line arguments
/
public static void main(String args[]) {
javaawtEventQueueinvokeLater(new Runnable() {
public void run() {
new NewJFrame()setVisible(true);
}
});
}
// Variables declaration - do not modify
private javaxswingJLabel jLabel1;
private javaxswingJPanel jPanel1;
private javaxswingJProgressBar jProgressBar1;
// End of variables declaration
public void stateChanged(ChangeEvent e1) {
int value = jProgressBar1getValue();
if (e1getSource() == jProgressBar1) {
}
}
public void actionPerformed(ActionEvent e) {
int value = jProgressBar1getValue();
if (value < 100) {
value++;
jProgressBar1setValue(value);
} else {
jProgressBar1setValue(0);
}
}
}
终于看到个用netbeans的人了。。

在读取大型文件或者其它大批量数据输入 *** 作时 希望能够通过一个进度条显示当前的进度 现在在Java中非常容易实现 仅仅需要几行代码即可 Java的swing包提供了ProgressMonitorInputStream类 该类提供了自动地d出进度窗口和事件处理机制 使用这个类也非常方便 只需要把任何一个InputStream作为参数 构造一个新的ProgressMonitorInputStream类 其它不需要任何额外的代码 即可实现进度窗口的自动生成 ProgressMonitorInputStream类可以和其它InputStream一样使用 ProgressMonitorInputStream类继承层次 [pre]java lang Object|+ java io InputStream|+ java io FilterInputStream|+ javax swing ProgressMonitorInputStream[/pre] 构造方法 ProgressMonitorInputStream(Component parentComponent Object message InputStream in)parentComponent 触发被监视 *** 作的组件message (如果d出进度显示窗口) 显示在进度显示窗口中的指示信息in 需要监视的输入流 *** 作方法 除了在InputStream和FilterInputStream中继承的方法外 还增加了如下方法 ProgressMonitor getProgressMonitor()//得到当前对象使用的ProgressMonitor对象 int read()int read(byte[] b)int read(byte[] b int off int len)void reset()long skip(long n)//上面几个方法都是覆盖了FilterInputStream中的方法 因为需要更新进度指示 void close()//因为需要关闭进度监视对象和窗口 所以覆盖了FilterInputStream父类中的close方法 示例代码: import java awt FlowLayout;import java awt event ActionEvent;import java awt event ActionListener;import java io FileInputStream;import java io InputStream;import javax swing JButton;import javax swing JFrame;import javax swing ProgressMonitorInputStream;public class ProgressMonitorTest{public static void main(String[] args){// 创建一个包含 Click me 的窗口final JFrame f =new JFrame( ProgressMonitor Sample );f getContentPane() setLayout(new FlowLayout());JButton b = new JButton( Click me );f getContentPane() add(b);f pack();// 设置按钮的动作事件b addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){// 这儿使用了新的线程处理按钮的动作事件 因为我们需要//主窗口的线程响应用户 这样你可以多次点击该按钮 //会启动多个读取文件的线程 主窗口也保持响应 new Thread(){public void run(){try {// 打开文件输出流 把InputStream包装在ProgressMonitorInputStream中 //在当前目录中需要放置一个大文件 建议超过 MInputStream in = new FileInputStream( bigfile dat );ProgressMonitorInputStream pm =new ProgressMonitorInputStream(f Reading a big file in);// 读取文件 如果总耗时超过 秒 将会自动d出一个进度监视窗口 //  显示已读取的百分比 int c;while((c=pm read()) != ){// 处理代码}pm close();}catch(Exception ex){ex printStackTrace();}}} start();}});// 设置缺省的窗口关闭行为 并显示窗口 f setDefaultCloseOperation(JFrame EXIT_ON_CLOSE);f setVisible(true);}} lishixinzhi/Article/program/Java/hx/201311/25629

你这个做法阻塞了UI线程,看下下面链接里的例子你就明白了。
>try{
insert(信息表);//插入信息
}catch{
1 insert(错误信息表);//放入错误信息的表
2 listadd(插入信息);
}

试试这个代码。

import javaioFile;
import javatextDecimalFormat;
import javatextSimpleDateFormat;
import javautilDate;
public class FilesAndDirectories {

public FilesAndDirectories() {

}
public void runTest() {
// list files and folders
String dir = "D:\\temp";
File file = new File(dir);
displayInfo(0, file);
}
private void displayInfo(int depth, File file) {
// Name, Date, Size, Attr
boolean executable = filecanExecute();
boolean readable = filecanRead();
boolean writable = filecanWrite();
boolean hidden = fileisHidden();
boolean directory = fileisDirectory();
long lastModified = filelastModified();
long length = filelength();
String name = filegetName();
// create ASCII file structure
StringBuilder buf = new StringBuilder();
for (int i = 0; i < depth; ++i) {
bufappend("|");
}
if (directory) {
bufappend("+ ");
}
if (nameisEmpty()) {
bufappend("");
} else {
bufappend(name);
}
// add modification date
bufappend("\t\t");
Date date = new Date(lastModified);
bufappend(new SimpleDateFormat()format(date));
bufappend("\t\t");
// add file size in kilobytes
long kb = length / 1024;
DecimalFormat format = new DecimalFormat();
formatsetGroupingUsed(true);
bufappend(formatformat(kb));
bufappend(" KB");
// add read, write, execute attribute flags
bufappend("\t\t");
if (hidden)
bufappend("");
if (readable)
bufappend("R");
if (writable)
bufappend("W");
if (executable)
bufappend("X");
// print everything to the command line
Systemoutprintln(buftoString());
File[] children = filelistFiles();
if (children != null) {
for (File child : children) {
displayInfo(depth + 1, child);
}
}
}
public static void main(String[] args) {
new FilesAndDirectories()runTest();
}
}

代码如下:import javaawtColor; import javaawtToolkit; import javaxswingImageIcon; import javaxswingJLabel; import javaxswingJOptionPane; import javaxswingJProgressBar; import javaxswingJWindow; @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 = ToolkitgetDefaultToolkit()getScreenSize()width; public static final int HEIGHT = ToolkitgetDefaultToolkit()getScreenSize()height; // 定义进度条组件 public JProgressBar progressbar; // 定义标签组件 public JLabel label; // 构造函数 public Demo() { // 创建标签,并在标签上放置一张 label = new JLabel(new ImageIcon("images/backgroundjpg")); labelsetBounds(0, 0, LOAD_WIDTH, LOAD_HEIGHT - 15); // 创建进度条 progressbar = new JProgressBar(); // 显示当前进度值信息 progressbarsetStringPainted(true); // 设置进度条边框不显示 progressbarsetBorderPainted(false); // 设置进度条的前景色 progressbarsetForeground(new Color(0, 210, 40)); // 设置进度条的背景色 progressbarsetBackground(new Color(188, 190, 194)); progressbarsetBounds(0, LOAD_HEIGHT - 15, LOAD_WIDTH, 15); // 添加组件 thisadd(label); thisadd(progressbar); // 设置布局为空 thissetLayout(null); // 设置窗口初始位置 thissetLocation((WIDTH - LOAD_WIDTH) / 2, (HEIGHT - LOAD_HEIGHT) / 2); // 设置窗口大小 thissetSize(LOAD_WIDTH, LOAD_HEIGHT); // 设置窗口显示 thissetVisible(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 { Threadsleep(100); } catch (InterruptedException e) { eprintStackTrace(); } progressbarsetValue(i); } JOptionPaneshowMessageDialog(this, "加载完成"); thisdispose(); } } 效果图:


欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/yw/13401608.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-07-29
下一篇 2023-07-29

发表评论

登录后才能评论

评论列表(0条)

保存