Presentation is loading. Please wait.

Presentation is loading. Please wait.

JAVA 程式設計與資料結構 第八章 GUI Introduction I. GUI  使用 java.swing 的 package 來設計視窗介 面,我們稱之為 Graphic User Interface(GUI) 。  在設計 GUI 的時候,需要 import javax.swing.*;

Similar presentations


Presentation on theme: "JAVA 程式設計與資料結構 第八章 GUI Introduction I. GUI  使用 java.swing 的 package 來設計視窗介 面,我們稱之為 Graphic User Interface(GUI) 。  在設計 GUI 的時候,需要 import javax.swing.*;"— Presentation transcript:

1 JAVA 程式設計與資料結構 第八章 GUI Introduction I

2 GUI  使用 java.swing 的 package 來設計視窗介 面,我們稱之為 Graphic User Interface(GUI) 。  在設計 GUI 的時候,需要 import javax.swing.*; 才可以編譯。

3 GUI 物件 1. Top-Level Containers : Applet, Dialog, Frame. 2. General-Purpose Containers : Panel, Scroll Pane, Split pane, Tabbed pane, Tool bar. 3. Special-Purpose Containers : Internal frame, Layered pane, Root pane. 4. Basic Controls : Buttons, Combo box, List, Menu, Slider, Spinner, Text field. 5. Uneditable Information Displays : Label, Progress bar, Tool tip. 6. Interactive Displays of Highly Formatted Information : Color chooser, File chooser, Table, Text, Tree.

4 JFrame  JFrame 屬於 GUI 中的 上層 Container(Top- Level Containers) , 所有的 GUI 物件都得 有一個 Top-Level Container 來承裝,所 以除了 Applet 跟 Dialog 之外,我們只 可以用 JFrame 來承裝 其他 GUI 物件。

5 JFrame Example 設定關閉視窗 即結束程式之 動作

6 JFrame setTitle() setLocation() setSize() getContentPane()

7 JButtons  JButton 便是按鈕,是屬於 Basic Controls 中的一種。 Button 可用來激發事件。 需加入 Event Listener 方 能激發 配置在 JFrame 之中

8 JButtons Event Listener 使用 addActionListener() 方法來將此物件加諸在 JButton 上,如 ok.addActionListener(new Button_ActionListener());

9 JLabel  JLabel 就是一個可以顯示文字的地方,而 在 J Label 上的文字是無法編輯的。  JLabel 的建立方式與 JButton 類似,若是 沒有使用 Layout Manager ,那麼可以使用 繼承自 component 物件的方法 setBounds() 來安排其在 JFrame 中的位置。  使用 add() 方法將其加入在 contentPane 中, 如 contentPane.add(label);  使用 setText() 方法來給定 JLabel 之上的文 字。

10 與 HTML 合用  為了方便做修改, JButton 與 JLabel 上的 文字都可以加上 HTML 的語法,來改變 其型態。例如:

11 建立有圖形的 Label  使用 ImageIcon 物件來建立。  先建立 ImageIcon 物件: –ImageIcon icon = new ImageIcon("nextArrow.gif", "To Next Page");  接下來宣告 JLabel 物件: –label = new JLabel("Next Page", icon, JLabel.CENTER);

12 JTextField  Text Field 指的是一行空白處,而且 JTextField 允許使用者輸入一行的文字。 也就是說,此處的文字是可編輯的。  根據 Constructor 來建構: –JTextField jtf = new JTextField(“ 在此輸入文字 ”, 500);  使用 getText() 方法自 Text Field 物件上取 得文字。  取得輸入資訊之後便可以做進一步的設 計或分析。

13 JTextArea  JTextArea 剛好可以算是 JTextField 的延伸, JTextField 只有一行,而 JTextArea 則可以 有許多行 (multiple lines) 。  其設定及建立方式與 JTextField 類似。

14 JScrollPane  JscrollPane 也是一個 Pane ,不過包含了 橫向及縱向的 Scroll ,可讓我們看到超出 JTextArea 範圍外的文字。

15 Menus  Menus 便是在 title 之下一行加上可以選擇 的選項。 Menu 跟 Button 一樣,都是可以 激發事件的物件。  我們可在 JFrame 之中加入 JMenuBar 物 件,然後在 JMenuBar 之中加入 JMenu 物件,然後再在 JMenu 之下加入 JMenuItem 物件。

16 Menus JMenuBar JMenu JMenuItem

17 Canvas  Canvas 物件是用來繪圖或是放置圖片的 地方。  可設計 class 繼承自 Canvas 物件,重寫 paint() 方法,來設定如何繪圖。可使用 repaint() 方法來重畫。


Download ppt "JAVA 程式設計與資料結構 第八章 GUI Introduction I. GUI  使用 java.swing 的 package 來設計視窗介 面,我們稱之為 Graphic User Interface(GUI) 。  在設計 GUI 的時候,需要 import javax.swing.*;"

Similar presentations


Ads by Google