- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想通过单击 Jbutton 从另一个也具有 main 方法的类中调用并执行一个具有 main 方法的类。Example1 的类具有完全独立运行的 main 方法:
import org.nlogo.app.App;
public class Example1 {
public static void main(String[] argv) {
App.main(argv);
try {
java.awt.EventQueue.invokeAndWait(
new Runnable() {
public void run() {
try {
App.app().open("C:/Users/fmar825/Desktop/fire.nlogo");
}
catch(java.io.IOException ex) {
ex.printStackTrace();
}}});
}
catch(Exception ex) {
ex.printStackTrace();
}
}
}
但是当我从另一个类调用它并通过单击 Jbutton 来执行时。
private void okbtnActionPerformed(java.awt.event.ActionEvent evt) {
Example1 object=new Example1();
try {
object.main(new String[0] );
} catch (Exception e) {
e.printStackTrace();
}
}
我收到以下错误消息。
ava.lang.Error: Cannot call invokeAndWait from the event dispatcher thread
at java.awt.EventQueue.invokeAndWait(EventQueue.java:1289)
at java.awt.EventQueue.invokeAndWait(EventQueue.java:1282)
at org.nlogo.awt.EventQueue$.invokeAndWait(EventQueue.scala:19)
at org.nlogo.app.App$.main(App.scala:150)
at org.nlogo.app.App.main(App.scala)
at Example1.main(Example1.java:4)
at User.okbtnActionPerformed(User.java:995)
at User.access$1600(User.java:31)
at User$19.actionPerformed(User.java:604)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6525)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6290)
at java.awt.Container.processEvent(Container.java:2234)
at java.awt.Component.dispatchEventImpl(Component.java:4881)
at java.awt.Container.dispatchEventImpl(Container.java:2292)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
at java.awt.Container.dispatchEventImpl(Container.java:2278)
at java.awt.Window.dispatchEventImpl(Window.java:2739)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:746)
at java.awt.EventQueue.access$400(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:697)
at java.awt.EventQueue$3.run(EventQueue.java:691)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:719)
at java.awt.EventQueue$4.run(EventQueue.java:717)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:716)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
java.lang.NullPointerException
at org.nlogo.window.RuntimeErrorDialog.suppressJavaExceptionDialogs(RuntimeErrorDialog.java:63)
at org.nlogo.app.App.handle(App.scala:779)
at org.nlogo.util.Exceptions$.handle(Exceptions.scala:21)
at org.nlogo.app.App$$anon$7.uncaughtException(App.scala:305)
at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:1057)
at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:1052)
at java.awt.EventDispatchThread.processException(EventDispatchThread.java:223)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:215)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
最佳答案
将 invokeAndWait
更改为 invokeLater
本质上,您的 JButton
正在事件调度线程的上下文中执行 ActionListener
,这就是它的工作原理。当您调用 Example1.main
时,它会尝试在事件调度线程的上下文中调用 Runnable
,但会阻止当前线程(即 EDT)直到其完成。这显然是无法实现的,为了防止可能的线程死锁,invokeAndWait
将抛出异常。
您“还可以”将Example1
中的代码更改为...
Runnable runnable = new Runnable() {
public void run() {
try {
App.app().open("C:/Users/fmar825/Desktop/fire.nlogo");
} catch(java.io.IOException ex) {
ex.printStackTrace();
}
}
};
if (EventQueue.isDispatchThread()) {
runnable.run();
} else {
try {
java.awt.EventQueue.invokeAndWait(runnable);
} catch(Exception ex) {
ex.printStackTrace();
}
}
它检查当前线程是否是 EDT,如果是,则直接调用 Runnable
的 run
方法,否则调用 invokeAndWait
。这将更好地模仿从 EDT 上下文中调用时的原始行为
可运行示例
对我来说效果很好...
父
(GUI)类...import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Parent {
public static void main(String[] args) {
new Parent();
}
public Parent() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
setLayout(new GridBagLayout());
JButton makeItSo = new JButton("Make it so");
makeItSo.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Child.main(new String[0]);
}
});
add(makeItSo);
}
}
}
子
(其他主
)类import java.awt.EventQueue;
import javax.swing.JOptionPane;
public class Child {
public static void main(String[] args) {
Runnable runnable = new Runnable() {
public void run() {
JOptionPane.showMessageDialog(null, "I'm a banana");
}
};
if (EventQueue.isDispatchThread()) {
System.out.println("Execute within the EDT");
runnable.run();
} else {
try {
System.out.println("Invoke and Wait");
java.awt.EventQueue.invokeAndWait(runnable);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
}
关于java - 如何使用 Jbutton 执行具有 main 方法的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28977842/
这是代码片段。 请说出这种用小内存存储大数据的算法是什么。 public static void main(String[] args) { long longValue = 21474836
所以我使用 imap 从 gmail 和 outlook 接收电子邮件。 Gmail 像这样编码 =?UTF-8?B?UmU6IM69zq3OvyDOtc68zrHOuc67IG5ldyBlbWFpb
很久以前就学会了 C 代码;想用 Scheme 尝试一些新的和不同的东西。我正在尝试制作一个接受两个参数并返回两者中较大者的过程,例如 (define (larger x y) (if (> x
Azure 恢复服务保管库有两个备份配置选项 - LRS 与 GRS 这是一个有关 Azure 恢复服务保管库的问题。 当其驻留区域发生故障时,如何处理启用异地冗余的恢复服务保管库?如果未为恢复服务启
说,我有以下实体: @Entity public class A { @Id @GeneratedValue private Long id; @Embedded private
我有下一个问题。 我有下一个标准: criteria.add(Restrictions.in("entity.otherEntity", getOtherEntitiesList())); 如果我的
如果这是任何类型的重复,我会提前申请,但我找不到任何可以解决我的具体问题的内容。 这是我的程序: import java.util.Random; public class CarnivalGame{
我目前正在使用golang创建一个聚合管道,在其中使用“$ or”运算符查询文档。 结果是一堆需要分组的未分组文档,这样我就可以进入下一阶段,找到两个数据集之间的交集。 然后将其用于在单独的集合中进行
是否可以在正则表达式中创建 OR 条件。 我正在尝试查找包含此类模式的文件名列表的匹配项 第一个案例 xxxxx-hello.file 或者案例二 xxxx-hello-unasigned.file
该程序只是在用户输入行数时创建菱形的形状,因此它有 6 个 for 循环; 3 个循环创建第一个三角形,3 个循环创建另一个三角形,通过这 2 个三角形和 6 个循环,我们得到了一个菱形,这是整个程序
我有一个像这样的查询字符串 www.google.com?Department=Education & Finance&Department=Health 我有这些 li 标签,它们的查询字符串是这样
我有一个带有静态构造函数的类,我用它来读取 app.config 值。如何使用不同的配置值对类进行单元测试。我正在考虑在不同的应用程序域中运行每个测试,这样我就可以为每个测试执行静态构造函数 - 但我
我正在寻找一个可以容纳多个键的容器,如果我为其中一个键值输入保留值(例如 0),它会被视为“或”搜索。 map, int > myContainer; myContainer.insert(make_
我正在为 Web 应用程序创建数据库,并正在寻找一些建议来对可能具有多种类型的单个实体进行建模,每种类型具有不同的属性。 作为示例,假设我想为“数据源”对象创建一个关系模型。所有数据源都会有一些共享属
(1) =>CREATE TABLE T1(id BIGSERIAL PRIMARY KEY, name TEXT); CREATE TABLE (2) =>INSERT INTO T1 (name)
我不确定在使用别名时如何解决不明确的列引用。 假设有两个表,a 和 b,它们都有一个 name 列。如果我加入这两个表并为结果添加别名,我不知道如何为这两个表引用 name 列。我已经尝试了一些变体,
我的查询是: select * from table where id IN (1,5,4,3,2) 我想要的与这个顺序完全相同,不是从1...5,而是从1,5,4,3,2。我怎样才能做到这一点? 最
我正在使用 C# 代码执行动态生成的 MySQL 查询。抛出异常: CREATE TABLE dump ("@employee_OID" VARCHAR(50)); "{"You have an er
我有日期 2016-03-30T23:59:59.000000+0000。我可以知道它的格式是什么吗?因为如果我使用 yyyy-MM-dd'T'HH:mm:ss.SSS,它会抛出异常 最佳答案 Sim
我有一个示例模式,它的 SQL Fiddle 如下: http://sqlfiddle.com/#!2/6816b/2 这个 fiddle 只是根据 where 子句中的条件查询示例数据库,如下所示:
我是一名优秀的程序员,十分优秀!