这是从Display and interact with an HTML form in a Swing application开始的问题。我已经复制了代码并在Eclipse(Indigo)中成功运行它,但出于某种神秘的原因,它停止工作。我能够连续运行数次,但现在我很幸运,如果我能够完全实现它的话。JFrame在之前显示之前停止显示
在调试下运行它与JUnit,它我单步加强到javax.swing.SwingUtilities.invokeLater(new Runnable() {
行;下一步直接跳到System.in.read()
行,有效地跳过显示逻辑。
我怀疑它有一些东西给invokeLater
方法。从我的研究中,invokeLater
将其Runnable()
放在调度队列中,并且每当VM接近它时将运行Runnable
。有一些提到,如果一个Runnable()
经验和例外它不会“放松”,我认为这意味着它不会关闭,并最终会阻止队列。我怀疑我的一个调用已经停止并阻塞队列,但不知道为什么或如何清除队列。
问题: 有什么办法可以清除队列吗?我已经停止并重新启动Eclipse几次,认为这将清除调度队列,但没有运气。
是否有可能就Java而言,JFrame
是显示,但它并没有实际显示在屏幕上,所以我可以点击提交按钮?什么会造成这种情况?为什么它会连续多次工作,然后突然停止?
下面是相关的代码。
JUnit的存根:
@Test
public final void testTestForms() {
// https://stackoverflow.com/questions/6145405/display-and-interact-with-an-html-form-in-a-swing-application
Navigate n = new Navigate();
try {
n.testForms();
n= null; // in desperate attempt to garbage collect
} catch (IOException e) {
System.out.println("Error invoking n.testForms()");
e.printStackTrace();
n= null;
}
n = null;
}
研究专题:
public class Navigate {
@Test
public void testForms() throws IOException {
System.out.println("in testForms()");
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
System.out.println("in run()");
javax.swing.JFrame jf = new javax.swing.JFrame();
jf.setSize(300, 300);
jf.setVisible(true);
jf.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
JTextPane textPane = new JTextPane();
textPane.setContentType("text/html");
textPane.setEditable(false);
textPane.setText("<html>" + "<body>" + "<form action=\"#\">"
+ "<input name=\"input1\" type=\"text\" />"
+ "<input name=\"input2\" type=\"text\" /><br/>"
+ "<input name=\"cb1\" type=\"checkbox\" /><br/>"
+ "<input name=\"rb1\" type=\"radio\" /><br/>"
+ "<input type=\"submit\" value=\"go\" />" + "</form>"
+ "</body>" + "</html>");
jf.getContentPane().setLayout(
new BoxLayout(jf.getContentPane(), BoxLayout.Y_AXIS));
jf.getContentPane().add(textPane);
HTMLEditorKit kit = (HTMLEditorKit) textPane.getEditorKit();
kit.setAutoFormSubmission(false);
textPane.addHyperlinkListener(new HyperlinkListener() {
public void hyperlinkUpdate(HyperlinkEvent e) {
if (e instanceof FormSubmitEvent) {
System.out.println(((FormSubmitEvent) e).getData());
}
}
});
}
}
);
// try to catch exceptions that could plug the queue?
try {
System.in.read();
} catch (IOException e) {
System.out.println("Error with System.in.read()");
e.printStackTrace();
throw new IOException(e);
}
try {
finalize(); // another desperate attempt
} catch (Throwable e) {
e.printStackTrace();
}
}
}
你试过'SwingUtilities.invokeAndWait'代替? – MadProgrammer
@MadProgrammer +1是可以工作的。 –