【发布时间】:2020-07-16 16:38:32
【问题描述】:
我有下一个问题:
我正在使用 Java 中的 2 个类,它们都扩展了 Thread,但我希望这些线程并行运行。
这是我的示例:带有用于进度条动画的线程的动画类和另一个带有用于启动应用程序启动画面的线程的类(12 秒),当我实现时,我看到了 Splash,但我没有看到动画进度条。
主类:
public class Run extends AppController {
private final static Logger LOGGER = Logger.getLogger(Run.class.getName());
public static Splash splash;
public Run() {
}
/**
* Application main class.
* @param args the command line arguments
*/
public static void main(String[] args) {
// application splash form
splash = new Splash(I18n.CUSTOMERS.getString("App.Title"),
ViewHelpers.ICONS16 + "app.png",
ViewHelpers.IMAGES + "splash.png");
splash.setVisible(true);
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
try {
controller = new Run();
controller.setSplash(splash);
Thread.sleep(12000);
} catch (InterruptedException ex) {
LOGGER.log(Level.SEVERE, null, ex);
}
controller.start();
}
});
}
}
Splash 类:
public class Splash extends JFrame {
//Title of Splash form.
private final String title;
//Icon path of Splash form.
private final String iconPath;
//Image path of Splash form.
private final String imagePath;
static JProgressBar progressBar;
/**
* Create splash form
*
* @param title title of splash form for taskbar
* @param iconPath icon path for taskbar
* @param imagePath image path for splash form
*/
public Splash(String title, String iconPath, String imagePath) {
this.title = title;
this.iconPath = iconPath;
this.imagePath = imagePath;
initComponents();
}
private void initComponents() {
setTitle(title);
setIconImage(new ImageIcon(getClass().getResource(iconPath)).getImage());
JLabel imgSplash = new JLabel(new ImageIcon(getClass().getResource(imagePath)));
setResizable(false);
setUndecorated(true);
setAlwaysOnTop(true);
progressBar = new JProgressBar(0, 100);
progressBar.setValue(0);
progressBar.setStringPainted(true);
progressBar.setPreferredSize(new Dimension(40, 20));
getContentPane().add(imgSplash, BorderLayout.CENTER);
getContentPane().add(progressBar, BorderLayout.SOUTH);
pack();
setLocationRelativeTo(null);
animation fill = new animation();
fill.start();
}
// function to increase progress
public class animation extends Thread implements Runnable{
public void run(){
int i = 0;
try {
for(i=1; i <= 100; i++){
// fill the menu bar
progressBar.setValue(i);
progressBar.repaint();
// delay the thread
Time.time(250);
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
}
最后是控制器类:
public abstract class AppController {
public static AppController controller;
protected AppView appView;
private Splash splash;
public AppController() {
}
/** Start the application and show the application view. **/
public void start() {
if (splash != null) {
splash.setVisible(false);
splash.dispose();
}
//launch the next window
appView = new AppView(I18n.CUSTOMERS.getString("App.Title"));
appView.setVisible(true);
}
public void setSplash(Splash splash) {
this.splash = splash;
}
}
【问题讨论】:
-
Nit : 不需要在 Animation 类中扩展 Thread 和 implmenet Runnable 。只需 1 个即可
-
多线程部分似乎是正确的。您能否在动画 run() 方法中使用打印语句来验证它是否被触发。然后您就会确定问题出在您正在调用的 API 上,而不是线程上
-
我同意你的建议,这是我的结果:动画 RUN0 SPLASH RUN ANIMATION RUN10 ANIMATION RUN20 ANIMATION RUN30 ANIMATION RUN40 ANIMATION RUN50 ANIMATION RUN60 ANIMATION RUN70 ANIMATION RUN80 ANIMATION RUN90 ANIMATION END SPLASH END 子api的问题?
-
是的。如您所见,动画和进度条都被调用了。所以,也许 API 不适合查看进度条。
标签: java multithreading progress-bar external splash-screen