gpt4 book ai didi

java - JDialog 和 Thread 的 ClassCastException

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:58:19 24 4
gpt4 key购买 nike

我有一个带有线程的 JDialog。它偶尔会在创建对话框时给我一个 ClassCastException(这意味着我可以无一异常(exception)地成功),而且我不知道它应该在哪里发生。

这是我的 JDialog 类的一个片段

public class ConfirmExitDialog extends JDialog implements Runnable,
ActionListener {
private static final long serialVersionUID = -8762051370686039110L;
private Thread dialogThread;
private boolean running;
private int result, count = 60;
private HandleExit handleExit = null;

// GUI
private JOptionPane optionPane;
private JLabel msgLabel = new JLabel();
private JButton btnYes;
private JButton btnNo;

private void updateLabelText() {
msgLabel.setText("<html>Ønsker du at afslutte dagens salg?<br>Programmet afslutter automatisk om " + count + " sekunder.</html>");
}

public int getResult() {
return result;
}

public ConfirmExitDialog(Frame frame, HandleExit handleExit) {
super(frame, false);
this.handleExit = handleExit;

setTitle("Afslut dagens salg?");
display();

running = true;
if (dialogThread == null) {
dialogThread = new Thread(this, "ConfirmExitDialog");
dialogThread.start();
}
this.setModal(true);
}

public void close() {
if (dialogThread != null)
running = false;
}

private void display() {
setLayout(new BorderLayout());

// Buttons
btnYes = new JButton("Ja");
btnYes.addActionListener(this);
btnYes.setMnemonic('J');
add(btnYes, BorderLayout.WEST);
btnNo = new JButton("Nej");
btnNo.addActionListener(this);
btnNo.setMnemonic('N');
add(btnNo, BorderLayout.EAST);
JButton[] buttons = { btnYes, btnNo };

updateLabelText();
optionPane = new JOptionPane(msgLabel, JOptionPane.QUESTION_MESSAGE,
JOptionPane.YES_NO_OPTION, null, buttons, buttons[0]);
setContentPane(optionPane);

setDefaultCloseOperation(DISPOSE_ON_CLOSE);

// Handle window closing correctly.
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
/*
* Instead of directly closing the window, we're going to change the
* JOptionPane's value property.
*/
optionPane.setValue(new Integer(JOptionPane.CLOSED_OPTION));
}
});

pack();
setVisible(true);
}

private void countDown() {
updateLabelText();
count--;
}

@Override
public void run() {
// TODO Auto-generated method stub

try {
display();
while (running && count > 0) {
System.out.println("Countdown " + count);
pack();
countDown();
Thread.sleep(1000);
}

setVisible(false);
if (count == 0)
handleExit.closeApplication(true, true);


} catch (InterruptedException ie) {
// Thread stopped
}
}

@Override
public void actionPerformed(ActionEvent event) {
Object src = event.getSource();

if (src == btnYes) {
setVisible(false);
result = JOptionPane.YES_OPTION;
running = false;
handleExit.closeApplication(true, false);
}
if (src == btnNo) {
setVisible(false);
result = JOptionPane.NO_OPTION;
running = false;

}
}

这是异常转换:

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException at javax.swing.LayoutComparator.compare(LayoutComparator.java:61) at java.util.Arrays.mergeSort(Arrays.java:1293) at java.util.Arrays.mergeSort(Arrays.java:1282) at java.util.Arrays.sort(Arrays.java:1210) at java.util.Collections.sort(Collections.java:159) at javax.swing.SortingFocusTraversalPolicy.enumerateAndSortCycle(SortingFocusTraversalPolicy.java:119) at javax.swing.SortingFocusTraversalPolicy.getFirstComponent(SortingFocusTraversalPolicy.java:434) at javax.swing.LayoutFocusTraversalPolicy.getFirstComponent(LayoutFocusTraversalPolicy.java:148) at javax.swing.SortingFocusTraversalPolicy.getDefaultComponent(SortingFocusTraversalPolicy.java:511) at java.awt.FocusTraversalPolicy.getInitialComponent(FocusTraversalPolicy.java:152) at java.awt.Window.getMostRecentFocusOwner(Window.java:2131) at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:629) at java.awt.Component.dispatchEventImpl(Component.java:4502) at java.awt.Container.dispatchEventImpl(Container.java:2099) at java.awt.Window.dispatchEventImpl(Window.java:2478) at java.awt.Component.dispatchEvent(Component.java:4460) at java.awt.EventQueue.dispatchEvent(EventQueue.java:599) at java.awt.SequencedEvent.dispatch(SequencedEvent.java:101) at java.awt.EventQueue.dispatchEvent(EventQueue.java:597) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161) at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

谢谢。丹尼尔

最佳答案

强烈建议不要在 Swing 的 EventDispatchThread 以外的线程中修改 UI,因为它可能而且会导致奇怪的副作用(就像您遇到的那样)。

想象一下 UI 即将重新绘制自身(和布局内容),而您只是通过使用另一个线程同时更改 UI 的某些部分来进行干预。在这种情况下,很可能会发生困惑。

有关详细信息,请参阅 Java 教程类(class) Concurrency in Swing

处理此问题的正确方法是让后台线程执行任何与 UI 无关的工作,并在 EventDispatchThread 中安排一个 UI 更新作业(它将在处理事件之间执行该作业)。

例子:

    Thread t = new Thread() {

public void run () {

// do background work

SwingUtilities.invokeLater(new Runnable() {

public void run () {
// update UI parts
}
});
}
};
t.start();

SwingUtilities.invokeLater(Runnnable) 将安排一个 Runnable 供稍后执行,而SwingUtilities.invokeAndWait(Runnnable) 将安排一个 Runnable 并等待它执行。

关于java - JDialog 和 Thread 的 ClassCastException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2966552/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com