ch12 GUI BASICS

12-1 Introduction

uses GUI to demonstrate OOP

12-2 Swing vs. AWT

Swing: JButton
AWT: Button

12-3 The Java GUI API

12-3-1 Component Classes

p.431 Figure 12.1 Java GUI programming utilizes the classes shown in this hierarchical diagram.

12-3-2 Container Classes

p.431 Table 12.1 GUI Container Classes

Container Class Description
java.awt.Container 用來包含其它容器,寫程式中不常用
javax.swing.JFrame 獨立的視窗
javax.swing.JPanel 視窗中用來「包含」其它物件的「容器」
javax.swing.JApplet i 現在不常用
javax.swing.JDialog 對話方塊

12-3-3 GUI Helper Classes

p.432 Table 12.2 GUI Helper Classes

Helper Class Description
java.awt.Graphics 圖形 (重新繪製畫面)
java.awt.Color 顏色
java.awt.Font 字型
java.awt.FontMetrics 字型屬性
java.awt.Dimension 物件的寬度與高度
java.awt.LayoutManager 版面安排(重要)

12-4 Frames

14-4-1 Creating a Frame

參考 p.432 程式 Listing 12.1 MyFrame.java

p.433 Figure 12.2 JFrame is a top-level container to hold GUI components.

14-4-2 Adding Components to a Frame

參考 p.434 程式 Listing 12.2 MyFrameWithComponents.java

12-5 Layout Managers

12-5-1 FlowLayout

p.435 Figure 12.4 FlowLayout lays out components row by row.

參考 p.435 程式 Listing 12.3 ShowFlowLayout.java

12-5-2 GridLayout

p.437 Figure 12.6 GridLayout lays out components in equal-sized cells on a grid.

參考 p.438 程式 Listing 12.4 ShowGridLayout.java

12-5-3 BorderLayout

p.439 Figure 12.8 BorderLayout lays out components in five areas.

參考 p.440 程式 Listing 12.5 ShowBorderLayout.java

12-5-4 Properties of Layout Managers

12-6 Using Panels as Subcontainers

參考 p.441 程式 Listing 12.6 TestPanels.java

12-7 The Color Class

public Color(int r, int g, int b);

Color color = new Color(128, 100, 100);

12-8 The Font Class

public Font(String name, int style, int size);
Font font1 = new Font("SansSerif", Font.BOLD, 16);
Font font1 = new Font("標楷體", Font.BOLD, 24);

p.444 Tip 顯示電腦安裝的所有字型

GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment();
String[] fontnames = e.getAvailableFontFamilyNames();
for (int i = 0; i < fontnames.length; i++)
	System.out.println(fontnames[i]);

12-9 Common Features of Swing GUI Components

p.444 Figure 12.11 All the Swing GUI components inherit the public methods from Component, Container, and JComponent.

參考 p.445 程式 Listing 12.7 TestSwingCommonFeatures.java

12-10 Image Icons

ImageIcon icon = new ImageIcon("image/us.gif");

參考 p.447 程式 Listing 12.8 TestImageIcon.java