Java巨难问题!如何在程序运行时显示运行进度(不用线程)。不够加分! this case, you should use an estimation oryou can simply display the time consumed but not the total estimated time. 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 leonzhao(灯泡) 说的对,只要你能获取当前的进度值就可以实现 谢谢各位的提示。现在的问题是:我尝试做出程序运行前弹出一个小Frame或者JPanel,提示当前操作(并在程序完成前自动释放),但是一旦失去焦点再也无法显示,无法响应。所以即便进度值可以获取,但是当程序运行时(包括获取进度值得过程),如果当前窗体失去焦点,则当前窗体所包含任何控件(包含该窗体)均不被响应,呈现出白面板,十分不友好。这种情况如何处理?用线程可以吗?或是其他? 其实Swing所能提供的进度条功能非常有限,而且非常之烂。偶看了许多用Swing开发的软件,很多时候是这样做的,把进度条分割成10个等份,然后把后台运行的总时间大概计算一下,然后一等份一等份让进度条前进。。楼住,为了这么一点点进度条的问题,去花费很多时间,很不值得。。看看JBuilder,哪里用到进度条了?它要么采用程序开启图片方式(JBuilder一开启),要么采用label显示方式(编译类的时候)。偶建议楼主还可以采用小gif动画,这样效果也好,可以不用考虑其他更多的东西。。偶现在做的东东,就是采用这种方式。 十分感谢rhj_2001(浩) 兄的意见。我不打算采用进度条方式了。不过,请问一下,“它要么采用程序开启图片方式(JBuilder一开启),要么采用label显示方式(编译类的时候)。”这种方式具体指什么,Jlabel所在当前窗体失去焦点,则不被响应,呈现出白面板,同样不友好。你指的在编译时的显示方式如何实现?java调用gif动画如何实现?再次表示感谢 线程肯定是要用到的。。“它要么采用程序开启图片方式(JBuilder一开启)”,是指JBuilder一开启那样的Load画面,假如你的后台程序要占很多时间,不妨做成这种方式,让用户不至于认为你的程序死了。你做的肯定是第二种方式,会早成JLabel失去焦点,变成白板,我觉得,是你的线程没有处理好! 你给我一个EMail吧。我把我自己写的程序发给你好了。 在不知道具体等待时间的情况,你可以用JDK1.4的进度条中的那种来回晃的那种以下是另一方法,不是很好 TestThread testThread = new TestThread();//新生成一个处理事务线程 testThread.start();//启动事务线程 (new ThreadDiag(this, testThread ,"正在加载,请等待......")).start();//启动等待提示框线程package txws; import java.awt.*; import javax.swing.*; public class ThreadDiag extends Thread { public Thread currentThread = null;//实际调用时就是TestThread事务处理线程 private String messages = "";//提示框的提示信息 private JFrame parentFrame = null;//提示框的父窗体 private JDialog clueDiag = null;// “线程正在运行”提示框 private Dimension dimensions = Toolkit.getDefaultToolkit().getScreenSize(); private int width = dimensions.width / 4, height = 60; private int left = 0, top = 0; public ThreadDiag(JFrame parentFrame, Thread currentThread, String messages) { this.parentFrame = parentFrame; this.currentThread = currentThread; this.messages = messages; initDiag();//初始化提示框 } protected void initDiag() { clueDiag = new JDialog(parentFrame,"正在加载,请等待...",true); clueDiag.setCursor(new Cursor(Cursor.WAIT_CURSOR)); JPanel testPanel = new JPanel(); JLabel testLabel = new JLabel(messages); clueDiag.getContentPane().add(testPanel); testPanel.add(testLabel); (new DisposeDiag()).start();//启动关闭提示框线程 } public void run() { //显示提示框 int left = (dimensions.width - width)/2; int top = (dimensions.height - height)/2; clueDiag.setSize(new Dimension(width,height)); clueDiag.setLocation(left, top); clueDiag.show(); }class DisposeDiag extends Thread{ public void run() { try { currentThread.join();//等待事务处理线程结束 } catch(InterruptedException e) { System.out.println("Exception:" + e); } clueDiag.dispose();//关闭提示框 } }} rhj_2001(浩) : mymail[email protected] 3ks 还是帖给你吧。import java.awt.*;import java.awt.event.*;import javax.swing.*;public class DialogTextThread extends Thread { private JDialog dialog; private JLabel promptLabel; private JLabel progressLabel; private JButton cancelBtn; private JFrame parentFrame; private String prompt; private String title; public DialogTextThread(JFrame parentFrame,String prompt,String title) { this.parentFrame = parentFrame; this.prompt = prompt; this.title = title; initDialog(); } public void initDialog() { promptLabel = new JLabel(" 进程 : " + prompt); promptLabel.setPreferredSize(new Dimension(255,22)); progressLabel = new JLabel(" "); progressLabel.setPreferredSize(new Dimension(255,22)); JPanel labelPanel = new JPanel(); labelPanel.setPreferredSize(new Dimension(260,67)); labelPanel.setLayout(new BorderLayout()); labelPanel.add(promptLabel,BorderLayout.CENTER); labelPanel.add(progressLabel,BorderLayout.SOUTH); JLabel tempLabel = new JLabel(); tempLabel.setPreferredSize(new Dimension(90,2)); cancelBtn = new JButton("Cancel"); cancelBtn.setEnabled(false); cancelBtn.addActionListener(new CancelAction()); JPanel buttonPanel = new JPanel(); buttonPanel.setPreferredSize(new Dimension(90,67)); buttonPanel.add(tempLabel); buttonPanel.add(cancelBtn); JPanel panel = new JPanel(); panel.setPreferredSize(new Dimension(370,116)); panel.add(labelPanel); panel.add(buttonPanel); dialog = new DialogExpand(parentFrame,new Dimension(370,116),title,true); dialog.setDefaultCloseOperation(DialogExpand.DO_NOTHING_ON_CLOSE); dialog.getContentPane().add(panel); } public void run() { dialog.setVisible(true); } public void dispose() { dialog.setVisible(false); } public void updateButtonStatus(boolean flag) { cancelBtn.setEnabled(flag); } public void updatePrompt(String prompt) { promptLabel.setText(" 进程 : " + prompt); } public void updateProgress(String progress) { progressLabel.setText(" " + progress); } public class CancelAction implements ActionListener { public void actionPerformed(ActionEvent evt) { dialog.setVisible(false); } } public static void main(String[] args) { final DialogTextThread dialogTextThread = new DialogTextThread(null,"","Create Progress"); Thread thread = new Thread() { public void run() { for(int i = 0;i < 10000;i++) { System.out.println(i); if(i%100 == 0) { dialogTextThread.updatePrompt(String.valueOf(i)); dialogTextThread.updateProgress(String.valueOf(i)); } } //dialogTextThread.dispose(); } }; dialogTextThread.start(); thread.start(); }} dialog = new DialogExpand(parentFrame,new Dimension(370,116),title,true);不好意思,DialogExpand是我自己扩展的,我把JDialog优化了一下,你改成dialog = new JDialog()就可以了。 rhj_2001(浩) : 昨天下午登陆不上,不好意思。 看了你的程序很受启发。思想如下: (1)程序运行两个线程,分别控制数据库搜索(耗时操作)以及等待界面显示 (2)只生成一个线程,显示等待友好界面 目前程序采用MVC模式。不知道以上两种思路采用哪种最好,以及如何具体采用。 线程已经解决:现在JOptionPane如何应用: 实现功能如下: (1)类似JBuilder编译时出现的对话框。即程序运行时自动弹出只有Cancel按钮的对话框(只能取消当前操作),一旦当前操作完成,该对话框自动消失。 (2)如何构造该JOptionPane。是否应用下列方法,showMessageDialog(Component parentComponent, Object message, String title, int messageType) ,最后一个参数如何给值。 思想如下:(1)程序运行两个线程,分别控制数据库搜索(耗时操作)以及等待界面显示(2)只生成一个线程,显示等待友好界面目前程序采用MVC模式。不知道以上两种思路采用哪种最好,以及如何具体采用。应该使用两个线程。3)我给你程序不就是一个对话框么?至于你用Cancel按钮用于结束当前的线程,可以考虑采用杀死线程的方法。4)JOptionPane的用法很简单,你去看看JavaDoc,就可以了。我想,什么问题都靠别人帮你解决,提高不了你独立分析解决问题的能力。 急问工程打成jar包后,找不到工程里用的properties的路径,请问如何解决,除了在环境变量里配置(内部有代码) java模拟超级终端发送指令,并得到响应数据 JAVA监控显卡GPU信息 正则表达式 哪位大侠知道如何用J2SE设计2D赛车游戏 SQL安装无反应,怪了 救助为什么找不到文件 java编译器是由java实现的是什么意思 帮我改改错误好吗 高手帮我写一段简单的Socket客户端代码 100分 JTree中getLeadSelectionPath()返回不正常 #############在JBuilder中。。。。。。############
现在的问题是:我尝试做出程序运行前弹出一个小Frame或者JPanel,提示当前操作(并在程序完成前自动释放),但是一旦失去焦点再也无法显示,无法响应。所以即便进度值可以获取,但是当程序运行时(包括获取进度值得过程),如果当前窗体失去焦点,则当前窗体所包含任何控件(包含该窗体)均不被响应,呈现出白面板,十分不友好。这种情况如何处理?用线程可以吗?或是其他?
不过,请问一下,“它要么采用程序开启图片方式(JBuilder一开启),要么采用label显示方式(编译类的时候)。”这种方式具体指什么,Jlabel所
在当前窗体失去焦点,则不被响应,呈现出白面板,同样不友好。你指的在编译时的显示方式如何实现?
java调用gif动画如何实现?
再次表示感谢
“它要么采用程序开启图片方式(JBuilder一开启)”,是指JBuilder一开启那样的Load画面,假如你的后台程序要占很多时间,不妨做成这种方式,让用户不至于认为你的程序死了。你做的肯定是第二种方式,会早成JLabel失去焦点,变成白板,我觉得,是你的线程没有处理好!
以下是另一方法,不是很好
TestThread testThread = new TestThread();//新生成一个处理事务线程
testThread.start();//启动事务线程
(new ThreadDiag(this, testThread ,"正在加载,请等待......")).start();//启动等待提示框线程package txws; import java.awt.*;
import javax.swing.*;
public class ThreadDiag extends Thread
{
public Thread currentThread = null;//实际调用时就是TestThread事务处理线程
private String messages = "";//提示框的提示信息
private JFrame parentFrame = null;//提示框的父窗体
private JDialog clueDiag = null;// “线程正在运行”提示框
private Dimension dimensions = Toolkit.getDefaultToolkit().getScreenSize();
private int width = dimensions.width / 4, height = 60;
private int left = 0, top = 0;
public ThreadDiag(JFrame parentFrame, Thread currentThread, String messages)
{
this.parentFrame = parentFrame;
this.currentThread = currentThread;
this.messages = messages;
initDiag();//初始化提示框
}
protected void initDiag()
{
clueDiag = new JDialog(parentFrame,"正在加载,请等待...",true);
clueDiag.setCursor(new Cursor(Cursor.WAIT_CURSOR));
JPanel testPanel = new JPanel();
JLabel testLabel = new JLabel(messages);
clueDiag.getContentPane().add(testPanel);
testPanel.add(testLabel);
(new DisposeDiag()).start();//启动关闭提示框线程
}
public void run()
{
//显示提示框
int left = (dimensions.width - width)/2;
int top = (dimensions.height - height)/2;
clueDiag.setSize(new Dimension(width,height));
clueDiag.setLocation(left, top);
clueDiag.show(); }class DisposeDiag extends Thread
{
public void run()
{
try
{
currentThread.join();//等待事务处理线程结束
}
catch(InterruptedException e)
{
System.out.println("Exception:" + e);
}
clueDiag.dispose();//关闭提示框
} }
}
[email protected]
3ks
import java.awt.event.*;
import javax.swing.*;public class DialogTextThread extends Thread { private JDialog dialog;
private JLabel promptLabel;
private JLabel progressLabel;
private JButton cancelBtn; private JFrame parentFrame;
private String prompt;
private String title; public DialogTextThread(JFrame parentFrame,String prompt,String title) {
this.parentFrame = parentFrame;
this.prompt = prompt;
this.title = title;
initDialog();
} public void initDialog() {
promptLabel = new JLabel(" 进程 : " + prompt);
promptLabel.setPreferredSize(new Dimension(255,22));
progressLabel = new JLabel(" ");
progressLabel.setPreferredSize(new Dimension(255,22)); JPanel labelPanel = new JPanel();
labelPanel.setPreferredSize(new Dimension(260,67));
labelPanel.setLayout(new BorderLayout());
labelPanel.add(promptLabel,BorderLayout.CENTER);
labelPanel.add(progressLabel,BorderLayout.SOUTH); JLabel tempLabel = new JLabel();
tempLabel.setPreferredSize(new Dimension(90,2));
cancelBtn = new JButton("Cancel");
cancelBtn.setEnabled(false);
cancelBtn.addActionListener(new CancelAction()); JPanel buttonPanel = new JPanel();
buttonPanel.setPreferredSize(new Dimension(90,67));
buttonPanel.add(tempLabel);
buttonPanel.add(cancelBtn); JPanel panel = new JPanel();
panel.setPreferredSize(new Dimension(370,116));
panel.add(labelPanel);
panel.add(buttonPanel); dialog = new DialogExpand(parentFrame,new Dimension(370,116),title,true);
dialog.setDefaultCloseOperation(DialogExpand.DO_NOTHING_ON_CLOSE);
dialog.getContentPane().add(panel);
} public void run() {
dialog.setVisible(true);
} public void dispose() {
dialog.setVisible(false);
} public void updateButtonStatus(boolean flag) {
cancelBtn.setEnabled(flag);
} public void updatePrompt(String prompt) {
promptLabel.setText(" 进程 : " + prompt);
} public void updateProgress(String progress) {
progressLabel.setText(" " + progress);
} public class CancelAction implements ActionListener {
public void actionPerformed(ActionEvent evt) {
dialog.setVisible(false);
}
} public static void main(String[] args) {
final DialogTextThread dialogTextThread = new DialogTextThread(null,"","Create Progress");
Thread thread = new Thread() {
public void run() {
for(int i = 0;i < 10000;i++) {
System.out.println(i);
if(i%100 == 0) {
dialogTextThread.updatePrompt(String.valueOf(i));
dialogTextThread.updateProgress(String.valueOf(i));
}
}
//dialogTextThread.dispose();
}
}; dialogTextThread.start();
thread.start();
}
}
dialog = new JDialog()就可以了。
昨天下午登陆不上,不好意思。
看了你的程序很受启发。思想如下:
(1)程序运行两个线程,分别控制数据库搜索(耗时操作)以及等待界面显示
(2)只生成一个线程,显示等待友好界面
目前程序采用MVC模式。不知道以上两种思路采用哪种最好,以及如何具体采用。
实现功能如下:
(1)类似JBuilder编译时出现的对话框。即程序运行时自动弹出只有Cancel按钮的对话框(只能取消当前操作),一旦当前操作完成,该对话框自动消失。
(2)如何构造该JOptionPane。是否应用下列方法,
showMessageDialog(Component parentComponent, Object message, String title, int messageType) ,最后一个参数如何给值。
(1)程序运行两个线程,分别控制数据库搜索(耗时操作)以及等待界面显示
(2)只生成一个线程,显示等待友好界面
目前程序采用MVC模式。不知道以上两种思路采用哪种最好,以及如何具体采用。应该使用两个线程。
3)我给你程序不就是一个对话框么?至于你用Cancel按钮用于结束当前的线程,可以考虑采用杀死线程的方法。
4)JOptionPane的用法很简单,你去看看JavaDoc,就可以了。我想,什么问题都靠别人帮你解决,提高不了你独立分析解决问题的能力。