Eclipse generates the basic procedure and example of SWING application
First, SWING project
1. Create a new project
Project -> Java-> Java project, then click "Next" to name "SwingTest" for the project, click "Finish".
2, create a new package
On the project established, click Right click, create a new-> package, name "SRC", click "Finish".
3, create a new class
On the package you just created, click Right click, create a new-> class, named "Buttondemo", click "Finish".
code show as below:
/ *
* Copyright Intec Dalian Center, All Rights Reserved
*
* Fushan University Education Research Activities, etc.
* Results デ タ タ ベ ス ス シ テム テム
*
* パ パ ケ ケ ジ:
* ク ラ ス: Buttondemo.java
*
* Benevo: Industrial Systems Dept. 0B6167
*
* バ ジョ ジョ ジョ: 1.00 2004-7-14
*
* /
Package SRC;
Import javax.swing. *;
Import javax.swing.border. *;
Import javax.swing.event. *;
Import java.awt. *;
Import java.awt.event. *;
// the main program class
Public Class Buttondemo Extends Jframe Implements ActionListener {
// GUI Objects Displayed in the frame window
ButtonGroup Group; // Groups Radio Buttons
JRADIOBUTTON RedButton; // First Radio Button
Jradiobutton WhiteButton; // Second Radio Button
Jradiobutton Bluebutton; // Third Radio Button
JPanel Colorbox; // Displays Selected Color
JCheckBox showcolorsbutton; // first Check Box
JCheckbox EXITONCLOSEBUTTON; // Second Check Box
JButton EXITBUTTON; // Plain Button
// Constructor Initializes The Gui Objects and Panels
Public buttemo () {
// SELECT LOCAL SYSTEM LOOK AND Feel
Try {
UIManager.SetLookAndfeel (UIManager.getsystemLookandfeelclassName ());
// uimanager.getcrossplatformlookandfeelclassname ());
} catch (exception e) {
}
// end Program When Window Closes
AddWindowListener (New Windowadapter () {
Public void windowClosing (WindowEvent E) {
System.exit (0);
}
});
/ / =========================================================================================================================================================================================== ========== // Radio Button Panel and GUI Objects
/ / =========================================================================================================================================================================================== ==========
// Create Radio Button Panel and an inner Pane
// TO HELP Display The Gui Objects Neatly
JPanel Radiopane = new jPanel ();
JPanel InnerRadiopane = new jPanel ();
Radiopane.setBorder (BorderFactory.createBevelBorder (bevelborder.raised);
Innerradiopane.setLayout (New BoxLayout (InnerRadiopane, BoxLayout.y_AXIS);
InnerRadiopane.setBorder (BorderFactory.createemptyBorder (10, 10, 10, 10));
// Construct the Radio Group and ITS Buttons
// All Button Events Go to the Program ActionListener
Group = new buttonGroup ();
RedButton = New Jradiobutton ("red");
WhiteButton = New Jradiobutton ("White");
BlueButton = New Jradiobutton ("Blue");
WhiteButton.SetSelected (true); // select one button
RedButton.addActionListener (this); // see actionPerformed ()
WhiteButton.AddActionListener (this);
BlueButton.AddActionListener (this);
Group.add (redbutton); // the group ensures this one
Group.add (WhiteButton); // Button is SELECTED, THE Previously
Group.add (bluebutton); // SELECTED Button IS Turned Off
// Construct A Small Panel for Displaying The SELECTED Color
Colorbox = new jPanel ();
ColorBox.SetBackground; Color.White;
ColorBox.SetPreferredSize (New Dimension (50, 50));
// add the gui Objects to the inner radio pane
Innerradiopane.Add (RedButton);
InnerRadiopane.Add (WhiteButton);
InnerRadiopane.Add (BlueButton);
Innerradiopane.Add (Box.Createrigidarea (New Dimension (0, 25))); // Spacer
InnerRadiopane.Add (ColorBox);
// add the inner Pane to the raised radio panel (Left Side)
Radiopane.Add (InnerRadiopane);
/ / =========================================================================================================================================================================================== ==========
// Check Box Panel and GUI Objects
/ / =========================================================================================================================================================================================== ==========
// Create Check Box Panel and an inner panel
// for a neat Appearance
JPanel Checkpane = New jPanel ();
JPanel InnerCheckpane = New jPanel ();
Checkpane.SetBorder (BorderFactory.createBevelBorder (bevelborder.raised);
InnerCheckPane.setLayout (New BoxLayout (InnerCheckpane, BoxLayout.y_AXIS);
InnerCheckpane.setBorder (BorderFactory.createemptyBorder (10, 10, 10, 10));
// Create the "show colors" Check Box Object and
// enable or disable the color radio buttons
ShowcolorsButton = New JCheckbox ("Show Colors");
ShowcolorsButton.SetSelected (True);
ShowcolorsButton.addchangelistener (New ChangeListener () {
Public void statechanged (changingeevent e) {boolean t = showcolorsbutton.isselected ();
Redbutton.senabled (t); // enable or disable all
WhiteButton.setenabled (t); // Radio Buttons Depending On
Bluebutton.setenabled (T); // State of Check Box
}
});
// Create the "exit onclose" Check Box Object and
// enable or disable the exit program button
EXITONCLOSEBUTTON = New JCHECKBOX ("EXIT On Close");
EXITONCLOSEBUTTON.ADDCHANGELISTENER (NEW CHANGELISTENER () {
Public Void StateChanged (ChangeEvent E) {
Boolean t = exitonclosebutton.isselected ();
EXITBUTTON.SETENABLED (T);
}
});
// Create the Plain "EXIT Program" Button
// and ITS Action Event Listener
EXITBUTTON = New JButton ("exit program");
EXITBUTTON.SETENABED (FALSE); // Initially Disabled
EXITBUTTON.ADDACTIONLISTENER (NEW ActionListener () {
Public Void ActionPerformed (ActionEvent E) {
System.exit (0);
}
});
// add the buttons to the inner Pane
InnerCheckpane.Add (showcolorsbutton);
InnerCheckpane.Add (ExitonClosebutton);
InnerCheckpane.Add (Box.Createrigidarea (New Dimension (0, 50));
InnerCheckpane.Add (ExitButton);
// add the inner pane to the raised check box panel
Checkpane.Add (InnerCheckpane);
// add the panels and gui Objects to the frame's content pane
Container content = getContentPane ();
Content.setLayout (New GridLayout (1, 3, 2, 2));
Content.add (Radiopane);
Content.add (Checkpane);
}
// Change The Colorbox Background Color When User
// selects a radio button.
Public Void ActionPerformed (ActionEvent E) {
Color C;
RedButton.isSelected ())
C = color.red;
Else if (WhiteButton.isSelected ())
C = color.white;
Else
C = color.blue;
Colorbox.setBackground;
}
// main program simply constructs the button // Application Object, and the Sizes and shows the window
Public static void main (String [] args) {
Buttondemo App = New Buttondemo ();
App.SetTitle ("Button and Check Box Demo);
App.SetSize (320, 240);
app.show ();
}
}
4. Run new class
Run -> Run to -> 2.java app
If everything is normal, OK, you can pack it.
5, play jar package
Right click on the project -> Export -> JAR file, the next step, then select the storage path, as shown in Figure 1. Then continue the next step to select the class of the program entry point. Figure 2. Then complete it. JAR package is already good.
figure 1
figure 2
Second, configure Java Web Start
1. Under the Tomcat's WebApps folder, create a folder, name "SwingTest", then put our JAR package, put it in.
2, at the same time, built two files, one is index.html, one is index.jnlp
The content is as follows:
INDEX.HTML
Index.jnlp
XML Version = "1.0" encoding = "UTF-8"?>
information>
resources>
jnlp>
3. Under the Tomcat's conf folder, modify the server.xml file, add such words, and the specific path is modified according to the specific situation of the individual.
OK, here we should be all configured, below, let us start Tomcat, and run the test. Enter http: // localhost: 8080 / swingtest / index.html in your browser, if there is no problem, the following figure should appear.
If all this comes out, congratulations, a simple Java Web Start release Swing application has been completed successfully.