日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > java >内容正文

java

java swingworker_Java中的SwingWorker

發(fā)布時(shí)間:2023/12/20 java 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java swingworker_Java中的SwingWorker 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

L&F物質(zhì)的輸出(因?yàn)槟鷮?duì)EDT的不確定性有待測(cè)試)

run:

JButton openDialog >>> Is there EDT ??? == true

Worker started >>> Is there EDT ??? == false

waiting 30seconds

Worker endeded >>> Is there EDT ??? == false

before JOptionPane >>> Is there EDT ??? == false

org.pushingpixels.substance.api.UiThreadingViolationException:

Component creation must be done on Event Dispatch Thread

和另外200行有關(guān)詳細(xì)信息

輸出是 "correct container created out of EDT"

我將在另一家L&F上進(jìn)行測(cè)試,Nimbus可能存在問(wèn)題,SystemLokkAndFeel在大多數(shù)情況下并不關(guān)心EDT上的重大錯(cuò)誤(對(duì)EDT的敏感性完全不同),默認(rèn)情況下,Metal在Windows平臺(tái)上沒(méi)有任何問(wèn)題,對(duì)于Java6,那么您的示例也可以在第二基礎(chǔ)上使用

從代碼

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.Dimension;

import java.awt.Font;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.*;

import javax.swing.plaf.FontUIResource;

public class Test {

public static void main(String[] args) throws Exception {

try {

for (UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {

if ("Nimbus".equals(info.getName())) {

UIManager.setLookAndFeel(info.getClassName());

UIManager.getLookAndFeelDefaults().put("nimbusOrange", (new Color(127, 255, 191)));

break;

}

}

} catch (ClassNotFoundException ex) {

} catch (InstantiationException ex) {

} catch (IllegalAccessException ex) {

} catch (javax.swing.UnsupportedLookAndFeelException ex) {

}

SwingUtilities.invokeLater(new Runnable() {

@Override

public void run() {

/*try {

UIManager.setLookAndFeel(

"org.pushingpixels.substance.api.skin.SubstanceOfficeBlue2007LookAndFeel");

UIManager.getDefaults().put("Button.font", new FontUIResource(new Font("SansSerif", Font.BOLD, 24)));

UIManager.put("ComboBox.foreground", Color.green);

} catch (Exception e) {

}*/

new Test().createAndShowUI();

}

});

}

private void createAndShowUI() {

JFrame frame = new JFrame();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

initComponents(frame);

frame.setPreferredSize(new Dimension(300, 300));//testing purposes

frame.pack();

frame.setVisible(true);

}

private void initComponents(final JFrame frame) {

final JDialog emailDialog = new JDialog(frame);

emailDialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);

emailDialog.setLayout(new BorderLayout());

JButton sendMailBtn = new JButton("Send Email");

sendMailBtn.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

//get content needed for email from old dialog

//get rid of old dialog

emailDialog.dispose();

//create new dialog

final JDialog emailProgressDialog = new JDialog(frame);

emailProgressDialog.add(new JLabel("Mail in progress"));

emailProgressDialog.pack();

emailProgressDialog.setVisible(true);

new Worker(emailProgressDialog, frame).execute();

}

});

emailDialog.add(sendMailBtn, BorderLayout.SOUTH);

emailDialog.pack();

JButton openDialog = new JButton("Open emailDialog");

openDialog.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

System.out.println("JButton openDialog >>> Is there EDT ??? == " + SwingUtilities.isEventDispatchThread());

emailDialog.setVisible(true);

}

});

frame.getContentPane().add(openDialog);

}

}

class Worker extends SwingWorker {

private final JDialog dialog;

private final JFrame frame;

Worker(JDialog dialog, JFrame frame) {

this.dialog = dialog;

this.frame = frame;

}

@Override

protected String doInBackground() throws Exception {

System.out.println("Worker started >>> Is there EDT ??? == " + SwingUtilities.isEventDispatchThread());

System.out.println("waiting 30seconds ");

Thread.sleep(30000);//simulate email sending

System.out.println("Worker endeded >>> Is there EDT ??? == " + SwingUtilities.isEventDispatchThread());

dialog.dispose();

System.out.println("before JOptionPane >>> Is there EDT ??? == " + SwingUtilities.isEventDispatchThread());

JOptionPane.showMessageDialog(frame, "Message sent", "Success", JOptionPane.INFORMATION_MESSAGE);

System.out.println("before JOptionPane >>> Is there EDT ??? == " + SwingUtilities.isEventDispatchThread());

return null;

}

}

分享編輯

總結(jié)

以上是生活随笔為你收集整理的java swingworker_Java中的SwingWorker的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。