import java.awt.Dialog;
import java.awt.Frame;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
public class Test7 implements ActionListener
{
private static final String DEFAULT_STATUS = "Please Waiting";
private JDialog dialog;
private JProgressBar progressBar;
private JLabel lbStatus;
private JButton btnCancel;
private Window parent;
private Thread thread; //处理业务的线程
private String statusInfo;
private String resultInfo;
private String cancelInfo;
public static void show(Window parent, Thread thread)
{
new Test7(parent, thread, DEFAULT_STATUS, null, null);
}
public static void show(Window parent, Thread thread, String statusInfo)
{
new Test7(parent, thread, statusInfo, null, null);
}
public static void show(Window parent, Thread thread, String statusInfo,
String resultInfo, String cancelInfo)
{
new Test7(parent, thread, statusInfo, resultInfo, cancelInfo);
}
private Test7(Window parent, Thread thread, String statusInfo,
String resultInfo, String cancelInfo)
{
this.parent = parent;
this.thread = thread;
this.statusInfo = statusInfo;
this.resultInfo = resultInfo;
this.cancelInfo = cancelInfo;
initUI();
startThread();
dialog.setVisible(true);
}
private void initUI()
{
if(parent instanceof Dialog)
{
dialog = new JDialog((Dialog)parent, true);
}
else if(parent instanceof Frame)
{
dialog = new JDialog((Frame)parent, true);
}
else
{
dialog = new JDialog((Frame)null, true);
}
final JPanel mainPane = new JPanel(null);
progressBar = new JProgressBar();
lbStatus = new JLabel("" + statusInfo);
btnCancel = new JButton("Cancel");
progressBar.setIndeterminate(true);
btnCancel.addActionListener(this);
mainPane.add(progressBar);
mainPane.add(lbStatus);
//mainPane.add(btnCancel);
dialog.getContentPane().add(mainPane);
dialog.setUndecorated(true); // 除去title
dialog.setResizable(true);
dialog.setSize(390, 100);
dialog.setLocationRelativeTo(parent); //设置此窗口相对于指定组件的位置
dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE); // 不允许关闭
mainPane.addComponentListener(new ComponentAdapter()
{
public void componentResized(ComponentEvent e)
{
layout(mainPane.getWidth(), mainPane.getHeight());
}
});
}
private void startThread()
{
new Thread()
{
public void run()
{
try
{
thread.start(); // 处理耗时任务
// 等待事务处理线程结束
thread.join();
}
catch(InterruptedException e)
{
e.printStackTrace();
}
finally
{
// 关闭进度提示框
dialog.dispose();
if(resultInfo != null && !resultInfo.trim().equals(""))
{
String title = "Info";
JOptionPane.showMessageDialog(parent, resultInfo, title,
JOptionPane.INFORMATION_MESSAGE);
}
}
}
}.start();
}
private void layout(int width, int height)
{
progressBar.setBounds(20, 20, 350, 15);
lbStatus.setBounds(20, 50, 350, 25);
btnCancel.setBounds(width - 85, height - 31, 75, 21);
}
@SuppressWarnings("deprecation")
public void actionPerformed(ActionEvent e)
{
resultInfo = cancelInfo;
thread.stop();
}
public static void main(String[] args) throws Exception
{
Thread thread = new Thread()
{
public void run()
{
int index = 0;
while(index < 5)
{
try
{
sleep(1000);
System.out.println(++index);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
}
};
Test7.show((Frame)null, thread, "Status", "Result", "Cancel");
}
}
分享到:
相关推荐
SWING进度条
主要介绍了Java Swing JProgressBar进度条的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
NULL 博文链接:https://yshjava.iteye.com/blog/1326539
NULL 博文链接:https://hw1287789687.iteye.com/blog/2003105
java swing 实现的带启动界面的简易截图软件,启动界面有进度条,可保存可粘贴到qq微信,简单实用
实现两个不同颜色的圆形进度条
不确定某个程序运行多久,用进度条来监测,如果进度条监测到运行结果结束了,就退出进度条,否则以一种不确定的状态等待。
基于java swing 开发的文件管理器: 采用苹果 ios 主题风格,支持环形进度条显示cpu利用率 GUI文件管理系统 1、实现文件夹创建、删除、进入。 2、实现当前文件夹下的内容罗列。 3、实现文件拷贝和文件夹拷贝...
Java代码实现圆圈旋转效果,比如在线看电影缓存时的旋转圆圈,网页刷新时的旋转圆圈,等待时
这里是我最近学习swing编程中用到的一些demo JTable CURD 事件绑定 进度条 线程处理等等demo,总之作为新手的你,看这个demo你可以学到很多。这里还采用了2-3中布局方式实现布局,由于项目其他保密性,这里只是一个...
基于JavaSwing组件实现的拼图小游戏源码+项目说明.zip基于JavaSwing组件实现的拼图小游戏源码+项目说明.zip基于JavaSwing组件实现的拼图小游戏源码+项目说明.zip 基于JavaSwing组件的拼图游戏 主要运用GUI设计,使用...
Swing还提供了许多其他的GUI组件和功能,如文本框、标签、列表、菜单、工具栏、进度条、表格等。开发人员可以使用这些组件和功能来构建复杂的GUI应用程序。此外,Swing还提供了许多皮肤和主题,可以用于美化应用程序...
既美观又简单的进度条,一个部分封装的漂亮进度条 兼容FF的进度条组件
《Java Swing图形界面开发与案例详解》全书共20章,其中第1~2章主要介绍有关Swing的基础知识,包括Swing的基本概述、如何使用IDE开发Swing程序;第3~15章结合众多实例和问题介绍最常用、最有用的组件及其应用方法...
主要介绍了Java Swing组件实现进度监视功能,结合完整实例形式详细分析了Java基于Swing组件实现进度条显示功能的具体操作技巧与相关注意事项,需要的朋友可以参考下
Java 图形界面开发简介 .............. ................................ ................................ ..... 5 1. Swing1. Swing1. Swing 1. Swing1. Swing1. Swing1. Swing简介 ..............................
采用java swing开发的splash Screen效果
跟我学Java Swing之游戏设计 一 谁知道通天的巴比伦塔耗费了多少沙石?又有谁知道罗马的建成经历了多少个日夜?我 们惟一知道的是,没有一块块砖石的垒砌,就没有蜿蜒万里的长城;没有巨石和黏土的堆 集,就没有亘古...