|
/* JPLExample - a JPL example with some AWT mixed in
* for good measure.
*/
import java.awt.*;
import java.awt.event.*;
public class JPLExample {
// Create an action listener that responds to a button
// press. This makes use of the JDK 1.1 anonymous class
// feature. The call to the class constructor is followed
// by the definition of one of its methods, allowing us to
// print something when the user hits a button.
ActionListener al = new ActionListener() {
public void actionPerformed(ActionEvent e)
System.out.println("Please do not press this button again.");
}
};
// This window adapter will respond to events that
// close the window, such as selecting close or exit
// from the window options menu.
WindowAdapter wa = new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
};
// A Canvas with a cute Image in it.
Canvas c = new Canvas() {
{ this.setSize(564,125); } // Instance initializer.
// An image containing the text "Java-PerL".
Image img = Toolkit.getDefaultToolkit().getImage("jpl.gif");
public void paint(Graphics g) {
g.drawImage(img, 0, 0, this);
}
};
/* The constructor for this class. */
public JPLExample() {
// Create a new Frame.
Frame f = new Frame();
// Add the window adapter to the Frame.
f.addWindowListener(wa);
// Add the Canvas containing the nifty little image.
f.add("North", c);
// Call the Perl method, passing the Frame as an argument.
plSample(f);
// Pack and show the Frame.
f.pack(); f.show();
}
/* A Perl method that creates/adds a Button to the Frame. */
perl void plSample(Frame f) {{
# Import the Button class.
use JPL::Class 'java::awt::Button';
# Create a new Button.
my $btn = new java::awt::Button;
# Look up method signature for setLabel() and set the
# label text of the Button.
my $setLabel = getmeth('setLabel', ['java.lang.String'], []);
$btn->$setLabel("Hi There!");
# Look up the addActionListener() method.
my $addActionListener = getmeth('addActionListener',
['java.awt.event.ActionListener'], []);
# Add the ActionListener that we created at the top of
# this class. Since it's a field of this class, it can be
# accessed through the corresponding method: (al).
$btn->$addActionListener( $self->al() );
# Look up a version of add() that lets us specify a
# position, such as North, South, Center, etc.
my $add = getmeth('add', ['java.lang.String',
'java.awt.Component'], ['java.awt.Component']);
# Add the button to the south end of the Frame.
$f->$add("South", $btn);
}}
/* The main method. */
public static void main(String[] argv) {
JPLExample jex = new JPLExample();
}
|
|