2
* SwingApplication.js - a translation into JavaScript of
3
* SwingApplication.java, a java.sun.com Swing example.
5
* @author Roger E Critchlow, Jr.
8
importPackage(Packages.javax.swing);
9
importPackage(Packages.java.awt);
10
importPackage(Packages.java.awt.event);
12
function createComponents() {
13
var labelPrefix = "Number of button clicks: ";
15
var label = new JLabel(labelPrefix + numClicks);
16
var button = new JButton("I'm a Swing button!");
17
button.setMnemonic(KeyEvent.VK_I);
18
button.addActionListener(new ActionListener({
19
actionPerformed : function() {
21
label.setText(labelPrefix + numClicks);
24
label.setLabelFor(button);
27
* An easy way to put space between a top-level container
28
* and its contents is to put the contents in a JPanel
29
* that has an "empty" border.
31
var pane = new JPanel();
32
pane.setBorder(BorderFactory.createEmptyBorder(
38
pane.setLayout(new GridLayout(0, 1));
46
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
49
//Create the top-level container and add contents to it.
50
var frame = new JFrame("SwingApplication");
51
frame.getContentPane().add(createComponents(), BorderLayout.CENTER);
53
//Finish setting up the frame, and show it.
54
frame.addWindowListener(new WindowAdapter({
55
windowClosing : function() {
56
java.lang.System.exit(0);
60
frame.setVisible(true);
2
* SwingApplication.js - a translation into JavaScript of
3
* SwingApplication.java, a java.sun.com Swing example.
5
* @author Roger E Critchlow, Jr.
8
var swingNames = JavaImporter();
10
swingNames.importPackage(Packages.javax.swing);
11
swingNames.importPackage(Packages.java.awt);
12
swingNames.importPackage(Packages.java.awt.event);
14
function createComponents()
17
var labelPrefix = "Number of button clicks: ";
19
var label = new JLabel(labelPrefix + numClicks);
20
var button = new JButton("I'm a Swing button!");
21
button.mnemonic = KeyEvent.VK_I;
22
// Since Rhino 1.5R5 JS functions can be passed to Java method if
23
// corresponding argument type is Java interface with single method
24
// or all its methods have the same number of arguments and the
25
// corresponding arguments has the same type. See also comments for
26
// frame.addWindowListener bellow
27
button.addActionListener(function() {
29
label.setText(labelPrefix + numClicks);
31
label.setLabelFor(button);
34
* An easy way to put space between a top-level container
35
* and its contents is to put the contents in a JPanel
36
* that has an "empty" border.
38
var pane = new JPanel();
39
pane.border = BorderFactory.createEmptyBorder(30, //top
43
pane.setLayout(new GridLayout(0, 1));
54
setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
57
//Create the top-level container and add contents to it.
58
var frame = new swingNames.JFrame("SwingApplication");
59
frame.getContentPane().add(createComponents(), BorderLayout.CENTER);
61
// Pass JS function as implementation of WindowListener. It is allowed since
62
// all methods in WindowListener have the same signature. To distinguish
63
// between methods Rhino passes to JS function the name of corresponding
64
// method as the last argument
65
frame.addWindowListener(function(event, methodName) {
66
if (methodName == "windowClosing") {
67
java.lang.System.exit(0);
71
//Finish setting up the frame, and show it.
73
frame.setVisible(true);