我有Java Applet的下一个代码源:
package m2mcom.web;
import m2mcom.entities.AutomatedTelnetClient;
import javax.swing.JApplet;
import javax.swing.SwingUtilities;
import javax.swing.JLabel;
public class Displaytext extends JApplet {
//Called when this applet is loaded into the browser.
public void init() {
//Execute a job on the event-dispatching thread; creating this applet's GUI.
try {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
AutomatedTelnetClient telnet = new AutomatedTelnetClient();
String answer = telnet.request();
JLabel lbl = new JLabel(answer);
add(lbl);
}
});
} catch (Exception e) {
System.err.println("createGUI didn't complete successfully");
}
}
}
telnet请求在AutomatedTelnetClient类中生成,一旦获得响应,会话也将关闭。 当我执行.html文件时,没有绘制任何内容,屏幕为空。任何人都知道为什么会这样做。是否有其他方法可以将字符串添加到Applet中?难道问题是来自SwingUtilities的inovkeAndWait?
这是HTML:
<HTML>
<HEAD>
</HEAD>
<BODY>
<div >
<APPLET CODE="m2mcom.web.Displaytext.class" WIDTH="800" HEIGHT="500">
</APPLET>
</div>
</BODY>
</HTML>
答案 0 :(得分:0)
尽量不要运行
AutomatedTelnetClient telnet = new AutomatedTelnetClient();
在SwingUtilities线程中。 b / c telnet请求很可能是异步调用。 尝试直接运行上面的代码而不使用线程,看看你得到了什么。
--- --- UPDATE
另一个问题可能是我的代码中的最后一行,它将标签添加到applet的contentPane组件中。这可能是问题所在。
如果这两个修复程序不起作用,则很可能您的 AutomatedTelnetClient 类逻辑中存在问题。
package m2mcom.web;
import m2mcom.entities.AutomatedTelnetClient;
import javax.swing.JApplet;
import javax.swing.SwingUtilities;
import javax.swing.JLabel;
public class Displaytext extends JApplet {
//Called when this applet is loaded into the browser.
public void init() {
//Execute a job on the event-dispatching thread; creating this applet's GUI.
try {
AutomatedTelnetClient telnet = new AutomatedTelnetClient();
String answer = telnet.request();
JLabel lbl = new JLabel(answer);
getContentPane().add(lbl);
} catch (Exception e) {
System.err.println("createGUI didn't complete successfully");
}
}
}