swing207短任务

文件复制

package my;

import java.io.File;

public class Test
{

	public static void main(String[] args)
	{
		// Win10下,不能在根目录下直接创建文件
		File srcFile = new File("/Users/ding/Desktop/11.zip");
		File dstFile = new File("/Users/ding/Desktop/2.zip");
		try
		{
			MyFileUtils.copyFile(srcFile, dstFile);
		} catch (Exception e)
		{
			e.printStackTrace();
		}

		System.out.println("完成");
	}

}
package my;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

public class MyFileUtils
{
	public static void copyFile(File srcFile, File dstFile) throws Exception
	{
		InputStream inputStream = null;
		OutputStream outputStream = null;
		try {
			inputStream = new FileInputStream(srcFile);
			outputStream = new FileOutputStream(dstFile);
			byte[] buf = new byte[8192]; // 每次最多读取8KB
			while(true)
			{
				int n = inputStream.read(buf); // 从源文件读取数据
				if(n <= 0) break; // 已经读完
				outputStream.write(buf,  0,  n); // 写入到目标文件
			}
		}
		finally {
			// 出异常时,确保所有的文件句柄被关闭
			try { inputStream.close();}catch(Exception e) {}
			try { outputStream.close();} catch(Exception e) {}
		}
	}
}

短任务

在这里插入图片描述

package my;

import java.awt.Container;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class Swing2
{
	private static void createGUI()
	{
		// JFrame指一个窗口,构造方法的参数为窗口标题
		// 语法:因为MyFrame是JFrame的子类,所以可以这么写
		JFrame frame = new MyFrame("Swing Demo");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		
		// 设置窗口的其他参数,如窗口大小
		frame.setSize(500, 300);
		
		// 显示窗口
		frame.setVisible(true);
		
		
	}
	
	public static void main(String[] args)
	{
		// 此段代码间接地调用了 createGUI(),具体原理在 Swing高级篇 里讲解
		// 初学者先照抄此代码框架即可
		javax.swing.SwingUtilities.invokeLater(new Runnable() {
			public void run()
			{
				createGUI();
			}
		});

	}
}
package my;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;

public class MyFrame extends JFrame
{
	JButton okButton = new JButton("开始");
	WaitDialog waitDialog = null;
	
	public MyFrame(String title)
	{
		super(title);

		// Content Pane
		JPanel root = new JPanel();
		this.setContentPane(root);
		root.setLayout(new FlowLayout());

		root.add(okButton);
		
		okButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e)
			{				
				onMouseClicked();				
			}			
		});
	}

	// 按钮点击事件处理
	private void onMouseClicked()
	{			
		// 创建任务
		// 注意:在Win10下不允许直接在根目录下创建文件,所以建一个子目录来测试
		File srcFile = new File("/Users/ding/Desktop/11.zip");
		File dstFile = new File("/Users/ding/Desktop/22.zip");
		ShortTask th = new ShortTask();
		th.execute(srcFile, dstFile);	

		// 提示等待对话框
		waitDialog = new WaitDialog(this);
		waitDialog.exec();
	}
	
	private class ShortTask extends Thread
	{
		private File srcFile, dstFile;
			
		// 传入参数,并启动线程
		public void execute(File srcFile, File dstFile)
		{
			this.srcFile = srcFile;
			this.dstFile = dstFile;
			start(); // 启动线程
		}
		
		@Override
		public void run()
		{
			// 处理任务
			try {
				doInBackground();
			}catch(Exception e)
			{
				e.printStackTrace();
			}
			
			// 歇息 片刻
			try { sleep(100); }catch(Exception e) {}
			
			// 显示结果 ( 更新 UI )
			SwingUtilities.invokeLater( ()->{
				done();
			});
		}
		
		// 处理任务
		protected void doInBackground() throws Exception
		{
			MyFileUtils.copyFile(srcFile, dstFile);
		}
		
		// 任务完成后,更新界面显示
		protected void done()
		{
			// 关闭对话框
			if(waitDialog!=null)
			{
				waitDialog.setVisible(false);
				waitDialog = null;
			}
		}
	}
	
	
}
package my;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

public class MyFileUtils
{
	public static void copyFile(File srcFile, File dstFile) throws Exception
	{
		InputStream inputStream = null;
		OutputStream outputStream = null;
		try {
			inputStream = new FileInputStream(srcFile);
			outputStream = new FileOutputStream(dstFile);
			byte[] buf = new byte[8192]; // 每次最多读取8KB
			while(true)
			{
				int n = inputStream.read(buf); // 从源文件读取数据
				if(n <= 0) break; // 已经读完
				outputStream.write(buf,  0,  n); // 写入到目标文件
			}
		}
		finally {
			// 出异常时,确保所有的文件句柄被关闭
			try { inputStream.close();}catch(Exception e) {}
			try { outputStream.close();} catch(Exception e) {}
		}
	}
}

package my;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Rectangle;

import javax.swing.BorderFactory;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.EtchedBorder;

/* 提示等待对话框
 * 
 * 无标题栏 setUndecorated(true)
 * 
 * 对话框里没有任何关闭按钮,所以用户不能手工关闭,只能等待操作结束
 * 
 */
public class WaitDialog extends JDialog
{
	public JLabel display = new JLabel("请等待...");
	
	public WaitDialog(JFrame owner)
	{
		super(owner, "", true);
		
		// 去掉标题栏
		this.setUndecorated(true);
		
		// 布局
		JPanel root = new JPanel();
		this.setContentPane(root);
		root.setLayout(new BorderLayout());
		
		// 背景
		root.setOpaque(true);
		root.setBackground(new Color(0xF4F4F4));
		root.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
		
		// 文字提示
		display.setHorizontalAlignment(SwingConstants.CENTER);
		root.add(display, BorderLayout.CENTER);
		
		// 设置大小
		this.setSize(200, 80);
	}

	public void exec()
	{
		// 相对owner居中显示
		Rectangle frmRect = this.getOwner().getBounds();
		int width = this.getWidth();
		int height = this.getHeight();
		int x = frmRect.x + (frmRect.width - width) / 2;
		int y = frmRect.y + (frmRect.height - height) / 2;
		this.setBounds(x, y, width, height);

		// 显示窗口 ( 阻塞 ,直接对话框窗口被关闭)
		this.setVisible(true);
	}
}

Af封装

在这里插入图片描述

package my;

import java.awt.Container;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class Swing2
{
	private static void createGUI()
	{
		// JFrame指一个窗口,构造方法的参数为窗口标题
		// 语法:因为MyFrame是JFrame的子类,所以可以这么写
		JFrame frame = new MyFrame("Swing Demo");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		
		// 设置窗口的其他参数,如窗口大小
		frame.setSize(500, 300);
		
		// 显示窗口
		frame.setVisible(true);
		
		
	}
	
	public static void main(String[] args)
	{
		// 此段代码间接地调用了 createGUI(),具体原理在 Swing高级篇 里讲解
		// 初学者先照抄此代码框架即可
		javax.swing.SwingUtilities.invokeLater(new Runnable() {
			public void run()
			{
				createGUI();
			}
		});

	}
}

package my;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;

import af.swing.thread.AfShortTask;


public class MyFrame extends JFrame
{
	JButton okButton = new JButton("开始");
	WaitDialog waitDialog = null;
	
	public MyFrame(String title)
	{
		super(title);

		// Content Pane
		JPanel root = new JPanel();
		this.setContentPane(root);
		root.setLayout(new FlowLayout());

		root.add(okButton);
		
		okButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e)
			{				
				onMouseClicked();				
			}			
		});
	}

	// 按钮点击事件处理
	private void onMouseClicked()
	{			
		// 创建任务
		// 注意:在Win10下不允许直接在根目录下创建文件,所以建一个子目录来测试
		File srcFile = new File("/Users/ding/Desktop/11.zip");
		File dstFile = new File("/Users/ding/Desktop/33.zip");
		CopyTask th = new CopyTask();
		th.execute(srcFile, dstFile);	

		// 提示等待对话框
		waitDialog = new WaitDialog(this);
		waitDialog.exec();
	}
	
	private class CopyTask extends AfShortTask
	{
		@Override
		protected void doInBackground() throws Exception
		{
			// this.args 是一个数组,保存了execute()传进来的参数
			File srcFile = (File)this.args[0];
			File dstFile = (File)this.args[1];
			MyFileUtils.copyFile(srcFile, dstFile);
		}

		@Override
		protected void done()
		{
			// 关闭对话框
			if(waitDialog!=null)
			{
				waitDialog.setVisible(false);
				waitDialog = null;
			}
		}
		
	}
	
	
}
package my;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

public class MyFileUtils
{
	public static void copyFile(File srcFile, File dstFile) throws Exception
	{
		InputStream inputStream = null;
		OutputStream outputStream = null;
		try {
			inputStream = new FileInputStream(srcFile);
			outputStream = new FileOutputStream(dstFile);
			byte[] buf = new byte[8192]; // 每次最多读取8KB
			while(true)
			{
				int n = inputStream.read(buf); // 从源文件读取数据
				if(n <= 0) break; // 已经读完
				outputStream.write(buf,  0,  n); // 写入到目标文件
			}
		}
		finally {
			// 出异常时,确保所有的文件句柄被关闭
			try { inputStream.close();}catch(Exception e) {}
			try { outputStream.close();} catch(Exception e) {}
		}
	}
}

package my;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Rectangle;

import javax.swing.BorderFactory;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.EtchedBorder;

/* 提示等待对话框
 * 
 * 无标题栏 setUndecorated(true)
 * 
 * 对话框里没有任何关闭按钮,所以用户不能手工关闭,只能等待操作结束
 * 
 */
public class WaitDialog extends JDialog
{
	public JLabel display = new JLabel("请等待...");
	
	public WaitDialog(JFrame owner)
	{
		super(owner, "", true);
		
		// 去掉标题栏
		this.setUndecorated(true);
		
		// 布局
		JPanel root = new JPanel();
		this.setContentPane(root);
		root.setLayout(new BorderLayout());
		
		// 背景
		root.setOpaque(true);
		root.setBackground(new Color(0xF4F4F4));
		root.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
		
		// 文字提示
		display.setHorizontalAlignment(SwingConstants.CENTER);
		root.add(display, BorderLayout.CENTER);
		
		// 设置大小
		this.setSize(200, 80);
	}

	public void exec()
	{
		// 相对owner居中显示
		Rectangle frmRect = this.getOwner().getBounds();
		int width = this.getWidth();
		int height = this.getHeight();
		int x = frmRect.x + (frmRect.width - width) / 2;
		int y = frmRect.y + (frmRect.height - height) / 2;
		this.setBounds(x, y, width, height);

		// 显示窗口 ( 阻塞 ,直接对话框窗口被关闭)
		this.setVisible(true);
	}
}

package af.swing.thread;

import javax.swing.SwingUtilities;


public abstract class AfShortTask extends Thread
{
	// 参数列表
	public Object[] args;
	// 任务出错信息
	public Exception err;
	
	// 传入参数,并启动线程
	// 参考本节课网盘内的文档:《省略号参数语法》
	public void execute(Object...args)
	{
		this.args = args; // 记录参数到this.args
		start(); // 启动线程
	}

	@Override
	public void run()
	{
		// 处理任务
		try
		{
			doInBackground();
		} catch (Exception e)
		{
			this.err = e;
			System.out.println("** AfShortTask: " + e.getMessage());
		}

		// 歇息 片刻
		try
		{
			sleep(100);
		} catch (Exception e)
		{
		}

		// 显示结果 ( 更新 UI )
		SwingUtilities.invokeLater(() -> {
			done( );
		});
	}

	// 处理任务
	protected abstract void doInBackground() throws Exception;

	// 任务完成后 
	// 如果this.err != null则说明doInBackground()里出错了
	protected abstract void done();

}

SwingWorker封装

package my;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;


public class MyFrame extends JFrame
{
	JButton okButton = new JButton("开始");
	WaitDialog waitDialog = null;
	
	public MyFrame(String title)
	{
		super(title);

		// Content Pane
		JPanel root = new JPanel();
		this.setContentPane(root);
		root.setLayout(new FlowLayout());

		root.add(okButton);
		
		okButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e)
			{				
				onMouseClicked();				
			}			
		});
	}

	// 按钮点击事件处理
	private void onMouseClicked()
	{			
		// 创建任务
		// 注意:在Win10下不允许直接在根目录下创建文件,所以建一个子目录来测试
		File srcFile = new File("c:/example/a1.rar");
		File dstFile = new File("c:/example/a2.rar");		
		CopyTask th = new CopyTask(srcFile, dstFile);
		th.execute();	

		// 提示等待对话框
		waitDialog = new WaitDialog(this);
		waitDialog.exec();
	}
	
	private class CopyTask extends SwingWorker
	{
		File srcFile, dstFile;
		
		public CopyTask(File srcFile, File dstFile)
		{
			this.srcFile = srcFile;
			this.dstFile = dstFile;
		}

		@Override
		protected Object doInBackground() throws Exception
		{
			// this.args 是一个数组,保存了execute()传进来的参数
			MyFileUtils.copyFile(srcFile, dstFile);
			return null;
		}

		@Override
		protected void done()
		{
			// 关闭对话框
			if(waitDialog!=null)
			{
				waitDialog.setVisible(false);
				waitDialog = null;
			}
		}
		
	}
	
	
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值