gpt4 book ai didi

Java swing Loader 图像无法正常显示?

转载 作者:行者123 更新时间:2023-12-02 12:18:37 24 4
gpt4 key购买 nike

我已经尝试过以下代码。功能-

  1. 点击按钮
  2. 它将调用一个需要一些时间来处理的方法。
  3. 需要显示加载程序图像,以便用户可以看到处理正在进行。

我在下面尝试过,但如果 loaderLabel1.setVisible(true);在方法调用之前不显示图像,如果我们评论 loaderLabel1.setVisible(false);然后在方法完成后显示加载程序图像。

威尔actionPerformed方法除非完成,否则不会将可见性应用于标签?如果是的话有没有其他方法可以解决这个问题?

import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class TestLoader extends JPanel{


static ImageIcon loading1 = new ImageIcon("D:\\WORKSPACE\\STUDY\\images\\ajax-loader.gif");



static JLabel loaderLabel1 = new JLabel(loading1, JLabel.CENTER);


public TestLoader(){
super(new GridLayout(0, 1));
System.out.println("---------TEST ------");

JPanel submitPanel = new JPanel();
submitPanel.add(clearMessageButton);
this.add(submitPanel);

JPanel LOADER_PANEL = new JPanel();
loaderLabel1.setVisible(false);
LOADER_PANEL.add(loaderLabel1);
this.add(LOADER_PANEL);
}


JButton clearMessageButton = new JButton(new AbstractAction("Test Result") {
@Override
public void actionPerformed(ActionEvent arg0) {

loaderLabel1.setVisible(true);

TestMethod();

loaderLabel1.setVisible(false);

}});

public void TestMethod(){
System.out.println(" =========START TestMethod =========");
try {
Thread.currentThread().sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(" =========complete TestMethod =========");
}

/**
* @param args
*/
public static void main(final String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
TestLoader pf = new TestLoader();
pf.display();
}
});


}

private void display() {
JFrame frame = new JFrame("TEST LOADER");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(this);
frame.pack();
frame.setResizable(true);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}

最佳答案

SwingWorker 类允许您在不同的线程中执行任务;下面是如何更改代码以使用 SwingWorker 的示例:

JButton clearMessageButton = new JButton(new AbstractAction("Test Result") {
@Override
public void actionPerformed(ActionEvent arg0) {
SwingWorker worker = new SwingWorker() {

@Override
protected Object doInBackground() throws Exception {
loaderLabel1.setVisible(true);
TestMethod();
return true;
}

public void TestMethod() {
System.out.println(" =========START TestMethod =========");
try {
Thread.currentThread().sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(" =========complete TestMethod =========");
}

protected void done() {
loaderLabel1.setVisible(false);
};
};
worker.execute();
}
});

注意 doInBackground() 方法在另一个线程中执行工作,以及在 doInBackground() 完成后调用的 did() 方法。

关于Java swing Loader 图像无法正常显示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45968822/

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