当前位置: 首页 > news >正文

网站建设教程所需文字可以直接打开的网站正能量

网站建设教程所需文字,可以直接打开的网站正能量,服装网站建设风格,网页制作专业选择swingworker如果要使用Swing用J​​ava编写桌面或Java Web Start程序#xff0c;您可能会觉得需要通过创建自己的线程在后台运行某些程序。 没有什么可以阻止您在Swing中使用标准的多线程技术#xff0c;并且需要遵循通常的注意事项。 如果有多个线程访问相同的变量#xf… swingworker 如果要使用Swing用J​​ava编写桌面或Java Web Start程序您可能会觉得需要通过创建自己的线程在后台运行某些程序。 没有什么可以阻止您在Swing中使用标准的多线程技术并且需要遵循通常的注意事项。 如果有多个线程访问相同的变量则需要使用同步方法或代码块或诸如AtomicInteger或ArrayBlockingQueue之类的线程安全类。 但是对于那些粗心的人来说是一个陷阱。 与大多数用户界面API一样您无法从自己创建的线程更新用户界面。 好吧正如每个Java本科生都知道的那样您通常可以 但是您不应该。 如果这样做有时您的程序会运行而其他时候则无法。 您可以通过使用专门的SwingWorker类来解决此问题。 在本文中我将向您展示即使您正在使用Thread类如何使您的程序正常运行然后我们将继续研究SwingWorker解决方案。 为了演示我创建了一个Swing程序。 如您所见它由两个标签和一个开始按钮组成。 此刻单击开始按钮将调用一个不执行任何操作的处理程序方法。 这是Java代码 import java.awt.Font; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.List; import java.util.concurrent.ExecutionException;import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.SwingUtilities; import javax.swing.SwingWorker;public class MainFrame extends JFrame {private JLabel countLabel1 new JLabel(0);private JLabel statusLabel new JLabel(Task not completed.);private JButton startButton new JButton(Start);public MainFrame(String title) {super(title);setLayout(new GridBagLayout());countLabel1.setFont(new Font(serif, Font.BOLD, 28));GridBagConstraints gc new GridBagConstraints();gc.fill GridBagConstraints.NONE;gc.gridx 0;gc.gridy 0;gc.weightx 1;gc.weighty 1;add(countLabel1, gc);gc.gridx 0;gc.gridy 1;gc.weightx 1;gc.weighty 1;add(statusLabel, gc);gc.gridx 0;gc.gridy 2;gc.weightx 1;gc.weighty 1;add(startButton, gc);startButton.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent arg0) {start();}});setSize(200, 400);setDefaultCloseOperation(EXIT_ON_CLOSE);setVisible(true);}private void start() {}public static void main(String[] args) {SwingUtilities.invokeLater(new Runnable() {Overridepublic void run() {new MainFrame(SwingWorker Demo);}});} } 我们将添加一些代码到start方法中以响应单击开始按钮而调用。 首先让我们尝试一个普通线程。 private void start() {Thread worker new Thread() {public void run() {// Simulate doing something useful.for(int i0; i10; i) {// Bad practicecountLabel1.setText(Integer.toString(i));try {Thread.sleep(1000);} catch (InterruptedException e) {}}// Bad practicestatusLabel.setText(Completed.);}};worker.start();} 实际上此代码似乎有效至少对我而言。 该程序最终看起来像这样 但是不建议您这样做。 我们正在从自己的线程中更新GUI在某些情况下这肯定会引发异常。 如果要从另一个线程更新GUI则应使用SwingUtilities安排更新代码在事件分发线程上运行。 下面的代码很好但是像恶魔一样丑陋。 private void start() {Thread worker new Thread() {public void run() {// Simulate doing something useful.for(int i0; i10; i) {final int count i;SwingUtilities.invokeLater(new Runnable() {public void run() {countLabel1.setText(Integer.toString(count));}});try {Thread.sleep(1000);} catch (InterruptedException e) {}}SwingUtilities.invokeLater(new Runnable() {public void run() {statusLabel.setText(Completed.);}});}};worker.start();} 当然必须做些什么使我们的代码更优雅 SwingWorker班 SwingWorker是使用Thread class 专门为Swing设计的替代方法。 这是一个抽象类它带有两个模板参数这使它看起来非常凶猛并使大多数人无法使用它。 但是实际上它并不像看起来那样复杂。 让我们看一些仅运行后台线程的代码。 对于第一个示例我们将不会使用任何一个模板参数因此将它们都设置为Void 这是Java的等效于原始void类型的类带有小写的“ v”。 运行后台任务 通过实现doInBackground方法并调用execute来运行代码我们可以在后台运行任务。 SwingWorkerVoid, Void worker new SwingWorkerVoid, Void() {Overrideprotected Void doInBackground() throws Exception {// Simulate doing something useful.for (int i 0; i 10; i) {Thread.sleep(1000);System.out.println(Running i);}return null;}};worker.execute(); 请注意 SwingWorker是一件简单的事情因此如果我们想再次运行代码则需要创建另一个SwingWorker 您无法重新启动同一个。 很简单嘿 但是如果我们想在运行代码后以某种状态更新GUI该怎么办 您无法从doInBackground更新GUI因为它不在主事件分配线程中运行。 但是有一个解决方案。 我们需要利用第一个模板参数。 线程完成后更新GUI 我们可以通过从doInBackground()返回一个值然后覆盖过去的done()来更新GUI从而可以安全地更新GUI。 我们使用get()方法检索从doInBackground()返回的值 因此第一个模板参数确定doInBackground()和get()的返回类型。 SwingWorkerBoolean, Void worker new SwingWorkerBoolean, Void() {Overrideprotected Boolean doInBackground() throws Exception {// Simulate doing something useful.for (int i 0; i 10; i) {Thread.sleep(1000);System.out.println(Running i);}// Here we can return some object of whatever type// we specified for the first template parameter.// (in this case were auto-boxing true).return true;}// Can safely update the GUI from this method.protected void done() {boolean status;try {// Retrieve the return value of doInBackground.status get();statusLabel.setText(Completed with status: status);} catch (InterruptedException e) {// This is thrown if the threads interrupted.} catch (ExecutionException e) {// This is thrown if we throw an exception// from doInBackground.}}};worker.execute(); 如果我们要在进行过程中更新GUI怎么办 这就是第二个模板参数的用途。 从正在运行的线程更新GUI 要从正在运行的线程更新GUI我们使用第二个模板参数。 我们调用publish()方法来“发布”我们要用来更新用户界面的值可以是第二个模板参数指定的任何类型。 然后我们覆盖process()方法该方法接收我们发布的值。 实际上process()接收已发布值的列表因为在实际调用process()之前可能会发布多个值。 在此示例中我们只是将最新值发布到用户界面。 SwingWorkerBoolean, Integer worker new SwingWorkerBoolean, Integer() {Overrideprotected Boolean doInBackground() throws Exception {// Simulate doing something useful.for (int i 0; i 10; i) {Thread.sleep(1000);// The type we pass to publish() is determined// by the second template parameter.publish(i);}// Here we can return some object of whatever type// we specified for the first template parameter.// (in this case were auto-boxing true).return true;}// Can safely update the GUI from this method.protected void done() {boolean status;try {// Retrieve the return value of doInBackground.status get();statusLabel.setText(Completed with status: status);} catch (InterruptedException e) {// This is thrown if the threads interrupted.} catch (ExecutionException e) {// This is thrown if we throw an exception// from doInBackground.}}Override// Can safely update the GUI from this method.protected void process(ListInteger chunks) {// Here we receive the values that we publish().// They may come grouped in chunks.int mostRecentValue chunks.get(chunks.size()-1);countLabel1.setText(Integer.toString(mostRecentValue));}};worker.execute(); 我希望您喜欢这个非常有用的SwingWorker类的介绍。 在我的网站Cave of Programming中 您可以找到更多的教程包括完整的免费视频教程以及有关SwingAndroid和Servlets的多线程课程。 参考来自Java出现日历博客的JCG合作伙伴 John Purcell的SwingWorker中的Java Swing中的多线程 。 翻译自: https://www.javacodegeeks.com/2012/12/multi-threading-in-java-swing-with-swingworker.htmlswingworker
http://www.pierceye.com/news/32109/

相关文章:

  • 做这种灰色的网站犯法wordpress 卡片式主题
  • 专注微商推广的网站网页和网站的概念
  • 签订网站建设协议应注意事项wordpress 清单 主题
  • 建设银行招聘官方网站网站下载免费新版
  • 江津哪里找做网站的免费建立自己的个人网站
  • 微网站 注册能免费用服务器的网站
  • 尊园地产做的网站网站导航为什么用ul列表做
  • 网站备案 取消电子商务就业方向及就业前景
  • wordpress建站不知道密码搜索引擎优化是做什么
  • 专业的单位网站开发开发折纸效果网站
  • 整站seo服务度娘网站灯笼要咋做呢
  • 广州住房和城乡建设局网站猫咪官网18点击进入
  • 互联网官方网站wordpress 显示用户名
  • 东莞网站建设企慕百度注册页面
  • 房产网站建设方案dw做框架网站
  • 网站备案后可以改名吗易语言可以做网站么
  • asp.net网站开发教程 pdf免费网站建设支持ftp
  • 网站建设推广是什么工作程序员编程培训
  • 谷歌推广外贸建站wordpress 获取当前分类名
  • 宁国做网站视频号怎么经营
  • 杭州网站建设费用多少钱杭州制作网站的公司
  • 合肥网站优化选哪家企业核名网站
  • 建设局网站港府名都常州 wordpress
  • 重庆大渡口营销型网站建设公司推荐有建网站的公司吗
  • 兰州做家教去哪个网站比较好策划公司收费明细
  • 安全的网站建设服务怎么搭建国外ip
  • 广州康体设备网站建设7一12岁手工
  • 什么叫网站索引容桂网站制作价位
  • 做有源代码的网站有什么好处帝国网站地图插件
  • 调查网站做调查不容易过怎么制作手机网站