gpt4 book ai didi

java - 线程在 actionPerformed 方法内部 hibernate

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

首先我想说我知道这种方法是错误的所以我问这个问题纯粹是出于好奇。假设我有一个这样的 swing 应用程序:

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

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

public class ThreadSleeping {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
JButton button = new JButton("Load");
JLabel label = new JLabel();

public ThreadSleeping() {
panel.add(button);

button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
label.setIcon(new ImageIcon(
"C:/Users/Public/Pictures/Sample Pictures/Tulips.jpg"));
System.out.println("Tulips painted");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
label.setIcon(new ImageIcon(
"C:/Users/Public/Pictures/Sample Pictures/Koala.jpg"));
System.out.println("Koala painted");

}
});

frame.add(panel, BorderLayout.NORTH);
frame.add(label, BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setSize(1024, 768);
// frame.pack();
frame.setVisible(true);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new ThreadSleeping();
}
});
}
}

基本上,当我单击 Load 按钮时,我希望显示 Tulips.jpg 图像,然后 GUI 卡住 2 秒,之后我希望显示 Koala。 jpg 图像显示。但发生的事情是:我点击按钮,GUI 卡住 2 秒,然后显示 Koala.jpg。在此之前没有 Tulips.jpg。但让我感到困惑的是,当我将那些 System.out.println("Tulips painted");System.out.println("Koala painted"); 放在一起时。因此,当我单击按钮时,它会打印“Tulips painted”,2 秒后“Koala painted”。有人能告诉我这是怎么回事吗?问候。

最佳答案

  1. 在这种情况下有效,because you programatically freeze ouf Swing GUI, but there is/aren't another update(s),不是另一个 JComponent(s)

  2. 在 Swing GUI 有一些其他更新的情况下不起作用,Thread.sleep(int) freeze Event Dispatch Thread,

  3. 默认情况下,JComponents XxxModels 的所有更新永远不会显示在 JComponents View

  4. example until sleep ended you'll lost all updated to the GUI

关于java - 线程在 actionPerformed 方法内部 hibernate ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15600203/

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