Java JFrame在以前显示时停止显示
这是我们的后续问题。我已经复制了代码并在Eclipse(Indigo)中成功运行了它,但由于某种神秘的原因,它停止了工作。我能够连续运行它好几次,但现在我很幸运,如果我能让它工作的话 在调试下用JUnit运行它,我已经升级到了Java JFrame在以前显示时停止显示,java,html,swing,jframe,jtextpane,Java,Html,Swing,Jframe,Jtextpane,这是我们的后续问题。我已经复制了代码并在Eclipse(Indigo)中成功运行了它,但由于某种神秘的原因,它停止了工作。我能够连续运行它好几次,但现在我很幸运,如果我能让它工作的话 在调试下用JUnit运行它,我已经升级到了javax.swing.SwingUtilities.invokeLater(新的Runnable(){行;下一步直接跳到System.in.read()行,有效地跳过了显示逻辑 我怀疑它与invokeLater方法有关。根据我的研究,invokeLater将它的Runna
javax.swing.SwingUtilities.invokeLater(新的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();
}
}
}
公共类导航{
@试验
public void testForms()引发IOException{
System.out.println(“in testForms()”;
javax.swing.SwingUtilities.invokeLater(新的Runnable(){
公开募捐{
System.out.println(“in run()”);
javax.swing.JFrame jf=newjavax.swing.JFrame();
jf.设置大小(300300);
jf.setVisible(真);
setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
JTextPane textPane=新的JTextPane();
setContentType(“text/html”);
textPane.setEditable(false);
textPane.setText(“+”+”)
+ ""
+“
”
+“
”
+“
”
+ "" + ""
+ "" + "");
jf.getContentPane().setLayout(
新的BoxLayout(jf.getContentPane(),BoxLayout.Y_轴);
jf.getContentPane().add(textPane);
HTMLEditorKit=(HTMLEditorKit)textPane.getEditorKit();
套件。setAutoFormSubmission(错误);
textPane.addHyperlinkListener(新的HyperlinkListener(){
公共无效hyperlinkUpdate(HyperlinkEvent e){
if(FormSubmitEvent的实例){
System.out.println(((FormSubmitEvent)e.getData());
}
}
});
}
}
);
//尝试捕获可能堵塞队列的异常?
试一试{
System.in.read();
}捕获(IOE异常){
System.out.println(“System.in.read()错误”);
e、 printStackTrace();
抛出新的IOException(e);
}
试一试{
finalize();//又一次绝望的尝试
}捕获(可丢弃的e){
e、 printStackTrace();
}
}
}
就Java而言,JFrame
是否可能
显示,但它实际上没有显示在屏幕上,所以我可以
点击提交按钮?这是什么原因?为什么会起作用
连续几次,然后突然停止
是,因为在添加所有组件之前,在JFrame
实例上调用setVisible(true)
代码中还有其他可能导致问题的异常:
- 在将所有组件添加到
JFrame
- 不要调用
而是在设置JFrame#setSize(..)
可见之前调用JFrame
JFrame#pack()
- 您的
不应嵌套在SwingUtilities.invokeLater(…)
类本身中,而应使其围绕JFrame
swingutilites.invokeLater(…)中的
实例的创建JFrame
- 不要调用
System.in.read()
- 不要依赖调用方法来创建
实例(JFrame
),请确保testForms()
实例是在类构造函数中创建的JFrame
import javax.swing.BoxLayout;
import javax.swing.JTextPane;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import javax.swing.text.html.FormSubmitEvent;
import javax.swing.text.html.HTMLEditorKit;
public class JavaApplication26 {
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
//create new instance of JFrame
new Navigate();
}
});
}
}
class Navigate {
public Navigate() {
initComponents();
}
private void initComponents() { //this is a constructor
javax.swing.JFrame jf = new javax.swing.JFrame();
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>");
HTMLEditorKit kit = (HTMLEditorKit) textPane.getEditorKit();
kit.setAutoFormSubmission(false);
textPane.addHyperlinkListener(new HyperlinkListener() {
@Override
public void hyperlinkUpdate(HyperlinkEvent e) {
if (e instanceof FormSubmitEvent) {
System.out.println(((FormSubmitEvent) e).getData());
}
}
});
//add components
jf.getContentPane().setLayout(new BoxLayout(jf.getContentPane(), BoxLayout.Y_AXIS));
jf.getContentPane().add(textPane);
jf.pack();//pack
jf.setVisible(true);
}
}
导入javax.swing.BoxLayout;
导入javax.swing.JTextPane;
导入javax.swing.event.HyperlinkEvent;
导入javax.swing.event.HyperlinkListener;
导入javax.swing.text.html.FormSubmitEvent;
导入javax.swing.text.html.HTMLEditorKit;
公共类JavaApplication26{
公共静态void main(字符串[]args){
javax.swing.SwingUtilities.invokeLater(新的Runnable(){
@凌驾
公开募捐{
//创建JFrame的新实例
新导航();
}
});
}
}
类导航{
公共导航(){
初始化组件();
}
private void initComponents(){//这是一个构造函数
javax.swing.JFrame jf=newjavax.swing.JFrame();
setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
JTextPane textPane=新的JTextPane();
setContentType(“text/html”);
textPane.setEditable(false);
textPane.setText(“+”+”)
+ ""
+“
”
+“
”
+“
”
+ "" + ""
+ "" + "");
HTMLEditorKit=(HTMLEditorKit)