1. 程式人生 > >Eclipse生成Swing應用程式以及用Java Web Start釋出

Eclipse生成Swing應用程式以及用Java Web Start釋出

Eclipse生成Swing應用程式的基本過程及示例<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

一、Swing工程

1、新建一個專案

專案->Java>Java專案,然後點選“下一步”,給專案命名為“SwingTest”,點選“完成”。

2、新建一個包

在剛才建立的專案上,點選右鍵,新建->包,命名為“src”,點選“完成”。

3、新建一個類

在剛才建立的包上,點選右鍵,新建->類,命名為“ButtonDemo”,點選“完成”。

程式碼如下:

/* 履歴

* Copyright Intec Dalian Center, All Rights Reserved

*

* 富山大學教育研究活動等

* 業績データベースシステム

*

* パッケージ名:

* クラス名:ButtonDemo.java

*

* 作成者: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 ButtonDemo() {

// 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's 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 that when 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(ChangeEvent e) {

boolean t = showColorsButton.isSelected();

redButton.setEnabled(t); // Enable or disable all

whiteButton.setEnabled(t); // radio buttons depending on

blueButton.setEnabled(t); // state of check box

}

});

// Create the "exit on close" 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.setEnabled(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;

if (redButton.isSelected())

c = Color.red;

else if (whiteButton.isSelected())

c = Color.white;

else

c = Color.blue;

colorBox.setBackground(c);

}

// Main program simply constructs the ButtonDemo

// application object, and then 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、執行新建的類

執行->執行為->2.java應用程式

如果一切正常,OK,可以進行打包了。

5、打Jar

專案上右鍵->匯出->JAR檔案,下一步,然後選擇存放路徑,如圖1。然後繼續下一步,到選擇程式入口點的類。如圖2。然後完成。Jar包就打好了。

<?xml:namespace prefix = v ns = "urn:schemas-microsoft-com:vml" />

1

 

2

二、配置Java Web Start

1、tomcatwebapps資料夾下,新建一個資料夾,命名為“SwingTest”,然後把我們打好的jar包,放進來。

2、同時,在新建兩個檔案,一個是index.html,一個是index.jnlp

內容分別如下:

Index.html

<a href="index.jnlp">Launch Application</a>

index.jnlp

<?xml version="1.0" encoding="utf-8"?>

<jnlp spec="1.0+" codebase="http://localhost:8080/SwingTest" href="index.jnlp">

<information>

<title>HelloWorld</title>

<vendor>IBM - JWS example of HelloWorld</vendor>

<description>HelloWorld - Example of JWS</description>

<description kind="short">HelloWorld example</description>

</information>

<resources>

<j2se version="1.4"/>

<jar href="hello.jar"/>

</resources>

<application-desc main-class="src.ButtonDemo"/>

</jnlp>

3、tomcatconf資料夾下,修改server.xml檔案,增加這麼一段話,具體路徑按照個人具體情況來進行修改。

<Context path="/ SwingTest " reloadable="true" docBase="D:/jakarta-tomcat-4.1.30/webapps/SwingTest"/>

OK,到這裡我們應該都配置好了,下面,讓我們啟動tomcat,來執行試一試。

 

如果這一切都出來,恭喜你,一個簡單的Java Web Start釋出Swing應用程式已經成功完成了。