201771010110孔維瀅《面向物件程式設計(java)》第十二週學習總結
理論知識部分
1.Java的抽象視窗工具箱(AbstractWindow Toolkit,AWT)包含在java.awt包中,它提供了許 多用來設計GUI的元件類和容器類。
2.Swing使用者介面庫是非基於對等體的GUI工具箱。Swing類庫被放在javax.swing包裡。
3.大部分AWT元件都有其Swing的等價元件。
Swing元件的名字一般是在AWT元件名前面新增一個字母“J”,如:JButton,JFrame,JPanel等。
4.元件:通常把由Component類的子類或間接子類建立的物件稱為一個元件。
5.容器是Java中能容納和排列元件的元件。
常用的容器是框架(Frame,JFrame)
例: Frame fra= new Frame(“這是一個視窗”);
Container類提供了一個方法add(),用來在容器類元件物件中新增其他元件。容器本身也是一個元件,可以把一個容器新增到 另一個容器裡,實現容器巢狀。
6.框架:在Java中,常採用框架(Frame)建立初始介面, 即GUI的頂層視窗
AWT庫中有一個基於對等體的Frame類。
框架定位與框架屬性:
定位: 常用Component類的setLocation和setBounds方法 常用屬性
Title:框架標題
IconImage:框架圖示
確定框架大小:通過呼叫Toolkit類的方法來得到螢幕尺寸資訊。
7.在AWT中可呼叫add()方法把元件直接新增到AWT Frame中,在Swing中元件則新增到內容窗格里。
8.Graphics2D類是Graphics類的子類,Graphics 2D類物件通常可用Graphics物件轉換而來。
例: public void paintComponent(Graphics g) { graphics2D g2=(graphics 2D)g; ….. }
9.Java 2D圖形類使用浮點數座標系,這樣可為座標指定單位。
getWidth()返回double值,應進行轉換: float f=(float)r.getWidth();
10.2D庫為每個圖形類提供兩個版本的靜態內部類: Retangle2D.Float 和 Retangle2D.Double
11.Graphics2D類的setPaint方法(Graphics類為 setColor方法)用來設定顏色。
例:g2.setPaint(Color.RED);
g2.drawString(“Set Color”,100,100);
通過指定紅綠藍三色比例,用Color類物件來複合成 一種新的顏色。
Color構造器如: Color(intredness,intgreenness,intblueness)
12.字型的使用:
(1)AWT中定義的五種邏輯字型名:
SanaSerif
Serif
Monospaced
Dialog
DialogInput
(2)字型風格:
Font.PLAIN
Font.BOLD
Fond.ITALIC
Fond.BOLD+ Font.ITALIC
(3)設定字型:
Font serif=new Font(“Serif”,Font.BOLD,14);
g2.setFont(serif);
13.影象:
(1)在Java應用程式中,一旦影象儲存在本地或因 特網的某個位置上,就可以將它們直接讀入到java 應用程式中。
String filename = “…”;
Image image= ImageIcon(filename).getImage();
(2)完成將一個影象檔案載入到程式中,再呼叫 Graphics類提供的DrawImage()顯示它。
public void paintComponent(Graphics g) { … g.drawImage(image, x, y, null); }
實驗部分:
測試程式1:
import javax.swing.*; public class SimpleFrameTest1 { public static void main(String[] args) { JFrame frame = new JFrame(); //構造JFrame類物件 frame.setBounds(0, 0, 300, 200);//移動元件並調整其大小。 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//設定預設關閉操作,退出並關閉 frame.setVisible(true);//設定Visible屬性,元件可見 } }
輸出結果:
import java.awt.*; import javax.swing.*; /** * @version 1.33 2015-05-12 * @author Cay Horstmann */ public class SimpleFrameTest { public static void main(String[] args) { EventQueue.invokeLater(() ->//Lambda表示式 { SimpleFrame frame = new SimpleFrame();//構造SimpleFrame類物件 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//設定預設關閉操作,退出並關閉 frame.setVisible(true);//設定Visible屬性,元件可見 }); } } class SimpleFrame extends JFrame { private static final int DEFAULT_WIDTH = 300;//設定靜態屬性 private static final int DEFAULT_HEIGHT = 200; public SimpleFrame()//構造器 { setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);//使用給定的寬度和高度,設定組建的大小 } }
輸出結果:
測試程式2:
import java.awt.*; import javax.swing.*; /** * @version 1.34 2015-06-16 * @author Cay Horstmann */ public class SizedFrameTest { public static void main(String[] args) { EventQueue.invokeLater(() -> { JFrame frame = new SizedFrame();//構建一個SizedFrame類物件 frame.setTitle("SizedFrame");//設定Title屬性,確定框架標題欄中的文字 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//設定預設關閉操作,退出並關閉 frame.setVisible(true);//設定Visible屬性,元件可見 }); } } class SizedFrame extends JFrame { public SizedFrame() { // 獲取螢幕尺寸 Toolkit kit = Toolkit.getDefaultToolkit();//靜態方法,生成Toolkit物件 Dimension screenSize = kit.getScreenSize();//返回Dimension物件的螢幕大小 int screenHeight = screenSize.height;//通過物件訪問一個屬性值,獲得Dimension物件的螢幕寬度和高度 int screenWidth = screenSize.width; // 設定框架寬度,高度,並讓平臺拾取螢幕位置 setSize(screenWidth / 2, screenHeight / 2); setLocationByPlatform(true); // 設定框架圖示 Image img = new ImageIcon("icon.gif").getImage(); setIconImage(img); } }
輸出結果:
測試程式3:
package notHelloWorld; import javax.swing.*; import java.awt.*; /** * @version 1.33 2015-05-12 * @author Cay Horstmann */ public class NotHelloWorld { public static void main(String[] args) { EventQueue.invokeLater(() -> { JFrame frame = new NotHelloWorldFrame();//構建一個SizedFrame類物件 frame.setTitle("NotHelloWorld");//設定Title屬性,確定框架標題欄中的文字 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//設定預設關閉操作,退出並關閉 frame.setVisible(true);//設定Visible屬性,元件可見 }); } } /** * A frame that contains a message panel */ class NotHelloWorldFrame extends JFrame { public NotHelloWorldFrame() { add(new NotHelloWorldComponent()); pack();//調整視窗大小,要考慮到其元件的首選大小 } } /** * A component that displays a message. */ class NotHelloWorldComponent extends JComponent { public static final int MESSAGE_X = 75; public static final int MESSAGE_Y = 100; private static final int DEFAULT_WIDTH = 300; private static final int DEFAULT_HEIGHT = 200; public void paintComponent(Graphics g)//覆蓋這個方法來描述應該如何繪製自己的元件 { g.drawString("Not a Hello, World program", MESSAGE_X, MESSAGE_Y); } public Dimension getPreferredSize() //要覆蓋這個方法,返回這個組建的首選大小 { return new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT); } }
輸出結果:
測試程式4:
package draw; import java.awt.*; import java.awt.geom.*; import javax.swing.*; /** * @version 1.33 2007-05-12 * @author Cay Horstmann */ public class DrawTest { public static void main(String[] args) { EventQueue.invokeLater(() -> { JFrame frame = new DrawFrame(); frame.setTitle("DrawTest"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); }); } } /** * A frame that contains a panel with drawings */ class DrawFrame extends JFrame { public DrawFrame() { add(new DrawComponent()); pack(); } } /** * A component that displays rectangles and ellipses. */ class DrawComponent extends JComponent { private static final int DEFAULT_WIDTH = 400; private static final int DEFAULT_HEIGHT = 400; public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; // 畫一個矩形 double leftX = 100; double topY = 100; double width = 200; double height = 150; Rectangle2D rect = new Rectangle2D.Double(leftX, topY, width, height); g2.draw(rect); // 畫出封閉的橢圓 Ellipse2D ellipse = new Ellipse2D.Double(); ellipse.setFrame(rect); g2.draw(ellipse); // 畫一條對角線 g2.draw(new Line2D.Double(leftX, topY, leftX + width, topY + height)); // 畫一個圓心相同的圓 double centerX = rect.getCenterX(); double centerY = rect.getCenterY(); double radius = 150; Ellipse2D circle = new Ellipse2D.Double(); circle.setFrameFromCenter(centerX, centerY, centerX + radius, centerY + radius); g2.draw(circle); } public Dimension getPreferredSize() { return new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT); } }
輸出結果:
測試程式5:
package font; import java.awt.*; import java.awt.font.*; import java.awt.geom.*; import javax.swing.*; /** * @version 1.34 2015-05-12 * @author Cay Horstmann */ public class FontTest { public static void main(String[] args) { EventQueue.invokeLater(() -> { JFrame frame = new FontFrame(); frame.setTitle("FontTest"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); }); } } /** * A frame with a text message component */ class FontFrame extends JFrame { public FontFrame() { add(new FontComponent()); pack(); } } /** * A component that shows a centered message in a box. */ class FontComponent extends JComponent { private static final int DEFAULT_WIDTH = 300; private static final int DEFAULT_HEIGHT = 200; public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; String message = "Hello, World!"; Font f = new Font("Serif", Font.BOLD, 36); g2.setFont(f); // 用同樣的中心畫一個圓,測量資訊的大小 FontRenderContext context = g2.getFontRenderContext(); Rectangle2D bounds = f.getStringBounds(message, context); // 設定(x,y) =文字左上角 double x = (getWidth() - bounds.getWidth()) / 2; double y = (getHeight() - bounds.getHeight()) / 2; // 增加上升到y以達到基線 double ascent = -bounds.getY(); double baseY = y + ascent; // 畫出message g2.drawString(message, (int) x, (int) baseY); g2.setPaint(Color.LIGHT_GRAY); // 畫出基線 g2.draw(new Line2D.Double(x, baseY, x + bounds.getWidth(), baseY)); // 繪製包圍的矩形 Rectangle2D rect = new Rectangle2D.Double(x, y, bounds.getWidth(), bounds.getHeight()); g2.draw(rect); } public Dimension getPreferredSize() { return new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT); } }
輸出結果:
測試程式6:
package image; import java.awt.*; import javax.swing.*; /** * @version 1.34 2015-05-12 * @author Cay Horstmann */ public class ImageTest { public static void main(String[] args) { EventQueue.invokeLater(() -> { JFrame frame = new ImageFrame(); frame.setTitle("ImageTest"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); }); } } /** * A frame with an image component */ class ImageFrame extends JFrame { public ImageFrame() { add(new ImageComponent()); pack(); } } /** * A component that displays a tiled image */ class ImageComponent extends JComponent { private static final int DEFAULT_WIDTH = 300; private static final int DEFAULT_HEIGHT = 200; private Image image; public ImageComponent() { image = new ImageIcon("blue-ball.gif").getImage(); } public void paintComponent(Graphics g) { if (image == null) return; int imageWidth = image.getWidth(null); int imageHeight = image.getHeight(null); // 在左上角繪製圖像 g.drawImage(image, 0, 0, null); // 將影象平鋪到元件上 for (int i = 0; i * imageWidth <= getWidth(); i++) for (int j = 0; j * imageHeight <= getHeight(); j++) if (i + j > 0) g.copyArea(0, 0, imageWidth, imageHeight, i * imageWidth, j * imageHeight); } public Dimension getPreferredSize() { return new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT); } }
輸出結果:
實驗總結:
通過本週的學習,我掌握了AWT與Swing的概念,內容,還有Swing庫中容器,元件和框架的的定義,學習了框架的使用方法。雖然看似簡單,但是我在課本中學習到的只是一小部分,還需要更加系統的學習,讓它更加美觀,實用。
其次,通過練習,我覺得對於之前的學習,我還需加強,不能邊學邊忘,而是把這些知識都串聯起來,運用起來。