Sat Feb 09, 2013 1:18 am
import java.awt.*;
import java.util.*;
import java.applet.Applet;
public class AppletButton extends Applet implements Runnable {
int frameNumber = 1;
String loadedClass;
String btnString;
String newTitle;
int widthVar = 0;
int heightVar = 0;
Button button;
Thread frameThread;
Label labelObj;
boolean loadedBoolean = false;
public void init() {
loadedClass = getParameter("WINDOWCLASS");
if (loadedClass == null) {
loadedClass = "simpleFrame";
}
btnString = getParameter("BUTTONTEXT");
if (btnString == null) {
btnString = "Click here to bring up a " + loadedClass;
}
newTitle = getParameter("WINDOWTITLE");
if (newTitle == null) {
newTitle = loadedClass;
}
String windowWidthString = getParameter("WINDOWWIDTH");
if (windowWidthString != null) {
try {
widthVar = Integer.parseInt(windowWidthString);
} catch (NumberFormatException e) {
//Use default width.
}
}
String windowHeightString = getParameter("WINDOWHEIGHT");
if (windowHeightString != null) {
try {
heightVar = Integer.parseInt(windowHeightString);
} catch (NumberFormatException e) {
//Use default height.
}
}
setLayout(new GridLayout(2,0));
add(button = new Button(btnString));
button.setFont(new Font("Arial", Font.PLAIN, 14));
add(labelObj = new Label("", Label.CENTER));
}
public void start() {
if (frameThread == null) {
frameThread = new Thread(this, "Bringing Up " + loadedClass);
frameThread.start();
}
}
public synchronized void run() {
Class classNameToLoad = null;
Class tmp = null;
String name = null;
// Check class
try {
classNameToLoad = Class.forName(loadedClass);
} catch (Exception e) {
// The specified class isn't anywhere that we can find.
labelObj.setText("Can't create window: Couldn't find class "
+ loadedClass);
button.disable();
return;
}
// Find out whether the class is a Frame.
for (tmp = classNameToLoad, name = tmp.getName();
!( name.equals("java.lang.Object") ||
name.equals("java.awt.Frame") ); ) {
tmp = tmp.getSuperclass();
name = tmp.getName();
}
if ((name == null) || name.equals("java.lang.Object")) {
labelObj.setText("Can't create window: "
+ loadedClass +
" isn't a Frame subclass.");
button.disable();
return;
} else if (name.equals("java.awt.Frame")) {
while (frameThread != null) {
while (loadedBoolean == false) {
try {
wait();
} catch (InterruptedException e) {
}
}
//We've been asked to bring up a window.
loadedBoolean = false;
Frame window = null;
try {
window = (Frame)classNameToLoad.newInstance();
} catch (Exception e) {
labelObj.setText("Couldn't create instance of class "
+ loadedClass);
button.disable();
return;
}
if (frameNumber == 1) {
window.setTitle(newTitle);
} else {
window.setTitle(newTitle + ": " + frameNumber);
}
frameNumber++;
//Set the window's size.
window.pack();
if ((widthVar > 0) | (heightVar > 0)) {
window.resize(Math.max(widthVar,
window.size().width),
Math.max(heightVar,
window.size().height));
}
window.show();
labelObj.setText("");
}
}
}
public synchronized boolean action(Event event, Object what) {
if (event.target instanceof Button) {
//signal the window thread to build a window
labelObj.setText("Please wait until we load the class");
loadedBoolean = true;
notify();
}
return true;
}
}
class simpleFrame extends Frame {
public simpleFrame() {
resize(400, 400);
}
}
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com
Powered by phpBB © phpBB Group.