?
?1
import
?javax.swing.
*
;
?2
import
?java.awt.
*
;
?3
import
?java.awt.event.
*
;
?4
?
?5
public
?
class
?myfirstswing
{
?6
????
?7
?????myfirstswing()
{
?8
???????JFrame?frame
=
new
?JFrame();????????????????
//
創建一個JFrame
?9
???????Container?contentPane
=
frame.getContentPane();??
//
創建一個容器
10
???????JButton?button
=
new
?JButton(
"
hello?swing!
"
);????
//
創建一個標有HELLO?WORLD!的按鈕
11
???????contentPane.add(button);??????????????????????
//
將button加入容器
12
???????frame.setSize(
200
,
120
);???????????????????????
//
設置我們的窗口大小
13
???????frame.setVisible(
true
);????????????????????????
//
創建窗口可視化
14
???????
15
????????
/**?*/
/**
*read*
*/
16
????????
/**/
/*
處理關閉窗口的操作,若你沒寫這一段,就算你已經關閉窗口了,但程序并不會終止。
17
?????????
*/
18
????????frame.addWindowListener(
19
???????????
new
?WindowAdapter()
{
20
???????????????
public
?
void
?windowClosing(WindowEvent?e)
{
21
??????????????????System.exit(
0
);?
22
???????????????}
?
23
???????????}
?
24
????????);
25
???????
/**?*/
/**
*read*
*/
26
????}
27
?
public
?
static
?
void
??main(String?args[])
{
28
???????
29
??????
new
?myfirstswing();????????????????????????????
//
main()函數調用myfirstswing
30
????}
31
??}
32

?2

?3

?4

?5



?6

?7



?8

?9

10

11

12

13

14

15


16


17

18

19



20



21

22

23

24

25


26

27



28

29

30

31

32

地震讓大伙知道:居安思危,才是生存之道。
