我正在查看SplashDemo.javaJava Sun教程.该类说明了如何在应用程序启动时创建启动画面.SplashDemo直接在main方法中创建一个框架.在这个例子中,为什么不通过使用在事件调度线程上运行GUI代码运行invokeLater?
这是完整的来源:
/*
* Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* …Run Code Online (Sandbox Code Playgroud) private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
int count = jSlider1.getValue();
int delay = jSlider2.getValue();
int valueOfSlider = jSlider2.getValue();
int valueOfSlider2 = jSlider1.getValue();
while (count > 0)
{
count--;
String count2 = ""+count;
jLabel3.setText(count2);
try {Thread.sleep(delay); }
catch (InterruptedException ie) { }
}
Run Code Online (Sandbox Code Playgroud)
它最终将显示jLabel上的最终数字,但它不会逐步更新数字.任何帮助
来自java.sun的简单代码:
public class BasicApp implements Runnable {
JFrame mainFrame;
JLabel label;
public void run() {
mainFrame = new JFrame("BasicApp");
label = new JLabel("Hello, world!");
label.setFont(new Font("SansSerif", Font.PLAIN, 22));
mainFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
mainFrame.setVisible(false);
// Perform any other operations you might need
// before exit.
System.exit(0);
}
});
mainFrame.add(label);
mainFrame.pack();
mainFrame.setVisible(true);
}
public static void main(String[] args) {
Runnable app = new BasicApp();
try {
SwingUtilities.invokeAndWait(app);
} catch (InvocationTargetException ex) {
ex.printStackTrace();
} catch (InterruptedException ex) …Run Code Online (Sandbox Code Playgroud) 我正在构建一个Swing应用程序,其中一部分功能应该是直观地和可听地处理和输出一些文本(使用Mary TTS).我需要一些关于GUI和文本处理类进行通信的最佳方法的建议.
GUI类是JPanel的子类.在其中我有一个实现Runnable的类,名为LineProcesser,它准备将文本分派给音频播放器.我正在使用一个线程执行器来阻止它离开EDT(这可能不是最好的方式,但它似乎达到了我所追求的结果).
我的目的是让LineProcessor运行所有文本并在每行的末尾更新JTextArea.此外,它需要停止并等待某些点的用户输入.用户输入完成后,GUI类应该告诉它继续处理.
以下代码说明了我目前拥有的内容:
public class MyPanel extends JPanel {
ExecutorService lineExecutor = Executors.newSingleThreadExecutor();
Runnable lineProcessor = new LineProcessor();
public class LineProcessor implements Runnable {
private int currentLineNo = 0;
public LineProcessor() {
// ...
}
@Override
public void run() {
// call getText();
// call playAudio();
currentLineNo++;
}
}
}
private JButton statusLbl = new JLabel();
private JButton mainControlBtn = new JButton();
private void mainControlBtnActionPerformed(ActionEvent evt) {
if (mainControlBtn.getText().equals("Start")) {
lineExecutor.submit(lineProcessor);
mainControlBtn.setText("Running");
}
}
}
Run Code Online (Sandbox Code Playgroud)
LineProcessor如何通知他们需要更改的GUI组件以及如何从GUI中暂停和重新启动它们?我对是否需要Swing Worker,属性/事件监听器或其他东西感到困惑?我读过的例子很有意义,但我看不出如何将它们应用到我这里的代码中.
在我的应用程序中,在AWT的事件调度线程(EDT)中启动的某些进程可能会在特定情况下中断.然后它等待用户的输入.唉,由于进程位于EDT中,整个应用程序冻结,用户无法重新启动进程,从而造成死锁.有没有办法打断EDT并从另一个线程中启动一个新的事件泵?然后,用户将能够与UI交互.
我想要做的事情看起来或多或少像打开一个模态对话框,除了我不想要一个对话框,因为我的组件很复杂.我宁愿在我的根窗格中显示它.所以我看看它是如何在java.awt.Dialog中完成的,并对它有很好的理解,但是使用的最重要的类(EventDispatchThread,SequencedEvent,...)受到保护,因此对我来说无法访问.
谢谢大家的答案.
我会更具体.我实际上正在开发一个嵌入了自己开发的脚本语言的应用程序.我正在为这种语言开发调试器.调试器(作为所有调试器)将在满足断点时停止脚本执行.脚本化进程可以从代码中的许多地方(包括来自EDT)触发,因此将该进程从EDT中取出不是一种选择.我希望将调试器UI嵌入到应用程序中(准确地在侧窗格中).因此,当遇到断点时,我需要中断当前线程(可能是EDT,主要不是说),并且至少调试器的UI仍然应该响应.另外我正在开发JDK 1.4,所以无法使用JDK7唉.
我目前正在做的是打开嵌入了调试器的JDialog.一切正常,但正如我所说,我对此解决方案并不完全满意,因为我真的希望我的调试器嵌入到我的主窗口中.
我正在尝试创建一个游戏,我从加载屏幕开始,它将加载所有需要的文件.我想显示完成的百分比,但它不起作用.在Loading_files完成后,JPanel仅加载repaints().我不知道什么是错的.
Main.java
package Main;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Main extends JFrame{
//-------------- Final Variables --------------
//Screen Related
public final static int JFRAME_WIDTH = 800;
public final static int JFRAME_HEIGHT = 600;
public final static Dimension SCREEN_SIZE = Toolkit.getDefaultToolkit().getScreenSize();
public final static int SCREEN_WIDTH = SCREEN_SIZE.width;
public final static int SCREEN_HEIGHT = SCREEN_SIZE.height;
//Game Related
public final static String NAME = "Xubris";
public final static …Run Code Online (Sandbox Code Playgroud) 我一直在阅读有关并发的内容(这让我很头疼).
我了解你可以设置一个任务,使用以下命令从主线程在EDT上运行:
SwingUtilities.invokeLater
Run Code Online (Sandbox Code Playgroud)
但你可以设置一个任务在EDT的主线程上运行吗?
如:
Thread mymainthread=Thread.currentThread();//<referring to the thread that initially kicks off public static void main
public void mousePressed(MouseEvent e){ //queue a task to run on mymainthread }
Run Code Online (Sandbox Code Playgroud)
可以吗?这是个坏主意吗?
关于SO的另一个问题(这里)谈到创建一个新线程,但如果我的目标是单线程(+ EDT)应用程序,那么继续使用main是不是更安全和更简单?.......或者也许我错了.
编辑:我应该解释:我想创建在主线程上相互通信的对象(在慢循环中运行),所以我不希望它们中的任何一个在不同的线程,edt或其他方面实例化.
我有一个包含几个按钮的对话框.单击某个特定按钮时,它的ActionListener会启动一个需要几秒钟才能完成的过程.在此期间,我想向用户提供一些反馈.采取一种简单的方法,我在"计算..."对话框中有一个标签,该标签最初是不可见的.代码段看起来像这样
button_OpenHoursReport.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
lbl_Computing.setVisible(true);
new runAndRenderReport();
RunAndRenderReport.main(null);
lbl_Computing.setVisible(false);
}
});
Run Code Online (Sandbox Code Playgroud)
问题是,在RunAndRenderReport完成之前,lbl_Computing文本不可见.显然,这对用户没什么帮助.不知道从哪里开始.这与线程有关吗?如果是这样,我可以使用一些指导如何开始.
我正在练习渲染和绘制图形,我似乎无法找出为什么eclipse在1/5左右给我一个错误.
Exception in thread "Thread-3" java.lang.NullPointerException
at game.StartingPoint.run(StartingPoint.java:74)
at java.lang.Thread.run(Unknown Source)
Run Code Online (Sandbox Code Playgroud)
这是我的线程的问题吗?我怎样才能解决这个问题?
这是我的源代码.
StartingPoint.java:
package game;
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
public class StartingPoint extends Applet implements Runnable {
Image i;
Graphics doubleG;
Ball b1;
Ball b2;
public StartingPoint() {
}
@Override
public void init() {
}
@Override
public void start() {
setSize(480, 360);
Thread thread = new Thread(this);
thread.start();
b1 = new Ball(40, 40);
b2 = new Ball(70, 200);
}
@Override
public void stop() { …Run Code Online (Sandbox Code Playgroud) 我想知道如果没有采取任何行动,你是否可以在java中发生一个动作事件.我的意思是欺骗它认为一个动作发生时它没有然后运行一个动作事件.我问这个的原因是因为它似乎很容易更新事件调度线程上运行的事情而不会发生实际事件.如果你们有人听说过这个,请告诉我.谢谢,
java ×10
swing ×9
concurrency ×3
thread-sleep ×2
actionevent ×1
applet ×1
awt ×1
swingworker ×1