让Applet Viewer与我的代码一起显示 [英] Getting an Applet Viewer to display with my code
问题描述
目前,我正在学习Java课程,并且正在Java Illuminate 3rd Edition的第4章中学习.我们正在努力使Applet显示文本和/或设计和颜色.我正在使用Macbook,并且正在通过TextWrangler进行编码,并试图在终端上运行我的程序,但是由于某种原因,在编译代码后,我无法显示Applet.请参见下面的内容,以查看本书提供给我们的代码,他们希望我们通过运行代码来获得Applet Viewer,但我不知道该怎么做.
I am taking a Java course at the moment and am on Chapter 4 in Java Illuminate 3rd edition. we are working on getting an Applet to display text and or designs and colors. i am working with a macbook and i am doing my coding through TextWrangler and am trying to run my on my terminal, but for some reason after i compile my code i cannot get the Applet display to show. Please see below to see the code the book gives us and they expect us to get an Applet Viewer by running the code, as such, but i cannot find out how to do so.
/* Drawing Text
Anderson, Franceschi
*/
import javax.swing.JApplet;
import java.awt.Graphics;
public class DrawingTextApplet extends JApplet
{
public void paint( Graphics g )
{
super.paint( g );
g.drawString( "Programming is not", 140, 100 );
g.drawString( "a spectator sport!", 140, 115 ); //for every new line you add 15 to the Y cord.
}
}
推荐答案
Direct from the applet tag info. page. Take particular note of the multi-line comments.
Applet"Hello World"示例
此示例需要安装Java开发工具包.访问Java SE下载以获取最新的JDK.
Applet 'Hello World' Example
This example requires the Java Development Kit installed. Visit Java SE Downloads for the latest JDK.
/* <!-- Defines the applet element used by the appletviewer. -->
<applet code='HelloWorld' width='200' height='100'></applet> */
import javax.swing.*;
/** An 'Hello World' Swing based applet.
To compile and launch:
prompt> javac HelloWorld.java
prompt> appletviewer HelloWorld.java */
public class HelloWorld extends JApplet {
public void init() {
// Swing operations need to be performed on the EDT.
// The Runnable/invokeAndWait(..) ensures that happens.
Runnable r = new Runnable() {
public void run() {
// the crux of this simple applet
getContentPane().add( new JLabel("Hello World!") );
}
};
SwingUtilities.invokeAndWait(r);
}
}
这篇关于让Applet Viewer与我的代码一起显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!