欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程语言 > java >内容正文

java

java swingworker_Java中的SwingWorker

发布时间:2023/12/20 java 57 豆豆
生活随笔 收集整理的这篇文章主要介绍了 java swingworker_Java中的SwingWorker 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

L&F物质的输出(因为您对EDT的不确定性有待测试)

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行有关详细信息

输出是 "correct container created out of EDT"

我将在另一家L&F上进行测试,Nimbus可能存在问题,SystemLokkAndFeel在大多数情况下并不关心EDT上的重大错误(对EDT的敏感性完全不同),默认情况下,Metal在Windows平台上没有任何问题,对于Java6,那么您的示例也可以在第二基础上使用

从代码

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;

}

}

分享编辑

总结

以上是生活随笔为你收集整理的java swingworker_Java中的SwingWorker的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。