1 / 17

JAVA 程式設計與資料結構

JAVA 程式設計與資料結構. 第八章 GUI Introduction I. GUI. 使用 java.swing 的 package 來設計視窗介面,我們稱之為 Graphic User Interface(GUI) 。 在設計 GUI 的時候,需要 import javax.swing.*; 才可以編譯。. GUI 物件. Top-Level Containers : Applet, Dialog, Frame.

ifeoma-head
Télécharger la présentation

JAVA 程式設計與資料結構

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

  3. GUI物件 • Top-Level Containers : Applet, Dialog, Frame. • General-Purpose Containers : Panel, Scroll Pane, Split pane, Tabbed pane, Tool bar. • Special-Purpose Containers : Internal frame, Layered pane, Root pane. • Basic Controls : Buttons, Combo box, List, Menu, Slider, Spinner, Text field. • Uneditable Information Displays : Label, Progress bar, Tool tip. • 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 setLocation() setTitle() getContentPane() setSize()

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

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

  9. JLabel • JLabel就是一個可以顯示文字的地方,而在JLabel上的文字是無法編輯的。 • 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 JMenu JMenuBar JMenuItem

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

More Related