Swing 是一个为Java设计的GUI工具包。Swing 是 JAVA基础类的一部分。Swing包括了图形用戶界面 (GUI) 器件 如:文本框,按钮,分隔窗格和表等。SWING提供许多比AWT更好的屏幕显示元素。它们用纯Java实现,所以同Java本身一样可以跨平台运行,同AWT不一样。 它们都是JFC的一部分,支持可更换的面板和主题,然而不是真的使用原生平台提供的设备,而是仅仅在表面上模仿他们。這意味着你可以在任意平台上使用JAVA支持的任意面板。轻量級元件的缺点则是执行速度较慢,优点就是可以在所有平台上采用统一的行为。
Java code
//
HelloWorldSwing.java代码
import
javax.swing.
*
;
public
class
HelloWorldSwing {
/**
* 创建并显示GUI。 出于线程安全的考虑, * 这个方法在事件调用线程中调用。
*/
private
static
void
createAndShowGUI() {
//
Make sure we have nice window decorations.
JFrame.setDefaultLookAndFeelDecorated(
true
);
//
Create and set up the window.
JFrame frame
=
new
JFrame(
"
HelloWorldSwing
"
); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//
Add the ubiquitous "Hello World" label.
JLabel label
=
new
JLabel(
"
Hello World
"
); frame.getContentPane().add(label);
//
Display the window.
frame.pack(); frame.setVisible(
true
); }
public
static
void
main(String[] args) {
//
Schedule a job for the event-dispatching thread:
//
creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(
new
Runnable() {
public
void
run() { createAndShowGUI(); } }); } }
Applet或Java小应用程序是一种在Web环境下,运行于客戶端的Java程序組件。它也是1990年代中期,Java在诞生后得以一炮走红的功臣之一。通常,每个Applet的功能都比较单一(例如仅用于显示一个舞动的Logo),因此它被称作“小应用程序”。
Applet必须运行于某个特定的“容器”,这个容器可以是浏览器本身,也可以是通过各种外挂程式,或者包括支持Applet的移动设备在內的其他各种程序来运行。与一般的Java应用程序不同,Applet不是通过main方法來运行的。在运行时Applet通常会与用戶进行互动,显示动态的画面,并且还会遵循严格的安全检查,阻止潜在的不安全因素(例如根据安全策略,限制Applet对客戶端文件系统的访问)。
Java code
Java Applet Java Applet用於HTML文件。 HTML代碼:
<
html
>
<
head
>
<
title
>
Hello World
</
title
>
</
head
>
<
body
>
HelloWorld Program says:
<
applet code
=
"
HelloWorld.class
"
width
=
"
600
"
height
=
"
100
"
>
</
applet
>
</
body
>
</
html
>
Java代碼:
import
java.applet.
*
;
import
java.awt.
*
;
public
class
HelloWorld
extends
Applet {
public
void
paint(Graphics g) { g.drawString(
"
Hello, world!
"
,
100
,
50
); } }
Java code
//
java HelloWorld
public
class
Hello {
public
static
void
main(String[] args) { System.out.println(
"
Hello, world!
"
); } }
你在写程序的时候,你可以extends Applet,也可以extends JApplet,这说明Applet是不同的 Applet是在写网页结构,而Swing是进行GUI开发。 下面是Applet程序和swing组件混合使用的DEMO:
Java code
import
javax.swing.
*
;
import
java.awt.event.
*
;
public
class
ButtonApplet
extends
JApplet { JButton button;
public
void
init() { button
=
new
JButton(
"
开始
"
); button.AddActionListener(
new
handle()); add(button); }
class
handle
implements
ActionListener {
public
void
actionPerformed(ActionEvent e) {
if
(button.getText()
==
"
开始
"
) button.setText
=
"
结束
"
;
else
button.setText
=
"
开始
"
; } } }
下面是网页显示
HTML code
<
html
>
<
head
>
<
title
>
Applet Demo
</
title
>
</
head
>
<
body
>
<
applet
code
="ButtonApplet.class"
width
="400"
height
="400"
></
applet
>
</
body
>
</
html
>
转载于:https://www.cnblogs.com/nanshouyong326/archive/2009/06/22/1508145.html
相关资源:数据结构—成绩单生成器