[CLUE-Tech] Java swing
Roger Frank
rfrank at rfrank.net
Wed Dec 27 16:31:05 MST 2000
On Tuesday 26 December 2000 20:47, Jeff wrote:
> What is your javac command that you are using to compile your test program?
Jeff, here's what I've done and how I use javac. The first three steps work
fine, but I cannot get step four to work.
First I need to put /usr/jdk-sun1.2.2/bin into my path:
export PATH=/usr/jdk-sun1.2.2/bin:$PATH
I compile and run a simple standalong Java applicaiton:
javac HelloWorldApp.java
java HelloWorld
This is the source:
/**
* The HelloWorldApp class implements an application that
* simply displays "Hello World!" to the standard output.
*/
class HelloWorldApp {
public static void main(String[] args) {
System.out.println("Hello World!"); //Display the string.
}
}
That works fine. It is a standalone application (note the main()).
Now to try it from HTML. The application changes only slightly:
import java.applet.Applet;
import java.awt.Graphics;
public class HelloWorld extends Applet {
public void paint(Graphics g) {
g.drawString("Hello world!", 50, 25);
}
}
Note there is no main(), so it's going to be called from
somewhere else (from HTML). Here is the file HelloWorld.html:
<HTML>
<HEAD>
<TITLE>Hello World</TITLE>
</HEAD>
<BODY>
<APPLET CODE="HelloWorld.class" WIDTH="160" HEIGHT="310"
ALT="hello world failed">
Java is disabled
</APPLET>
</BODY>
</HTML>
This, step 2, works fine also.
I compile the class with javac HelloWorld.java
Then with a Java-enabled browser, open the file HelloWorld.html
and everything is just fine. The applet runs, Hello world! shows
up in the browser window.
Now on to swing, step 3:
Here is a simple program which uses swing:
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
public class FrameTest2
{ public static void main(String[] args)
{ EmptyFrame frame = new EmptyFrame();
frame.setTitle("Close me!");
frame.show();
}
}
class EmptyFrame extends JFrame
{ public EmptyFrame()
{ final int DEFAULT_FRAME_WIDTH = 300;
final int DEFAULT_FRAME_HEIGHT = 300;
setSize(DEFAULT_FRAME_WIDTH, DEFAULT_FRAME_HEIGHT);
WindowCloser listener = new WindowCloser();
addWindowListener(listener);
}
private class WindowCloser extends WindowAdapter
{ public void windowClosing(WindowEvent event)
{ System.exit(0);
}
}
}
Note this has a main(), so it can be run directly with java.
So this works: javac FrameTest2.java; java FrameTest2
Step 3, swing in an application, works fine.
So what works is Java in an application, Java as an applet,
Java with swing in an application, but *not* Java with swing
as an applet.
Now I know my browser can handle swing since I can go to this URL:
http://java.sun.com/docs/books/tutorial/uiswing/start/swingApplet.html
and test it, successfully.
I need to to run locally, so I download HelloSwingApplet.java from
that page, compile it (javac HelloSwingApplet.java). I also
download the provided HelloSwingApplet.html from the same page.
I open HelloSwingApplet.html in my drowser, and it just doesn't
run the applet. It gives me very briefly
"load: class HelloSwingApplet.class not found"
and then "start: applet not initialized"
I know the .class file is in ~ where the .html file is, so I'm very
confused. Is there a search path I need to set up?
What would make my Java-wish come true would be a simple example
of an applet that uses swing and an HTML file that invokes it.
If I can get that to work, the rest should be easy.
--
Roger Frank
Ponderosa High School, Colorado
More information about the clue-tech
mailing list