201771010118 馬昕璐《面向對象程序設計java》第十二周學習總結
第一部分:理論知識學習部分
用戶界面:用戶與計算機系統(各種程序)交互的接口
圖形用戶界面:以圖形方式呈現的用戶界面
AET:Java 的抽象窗口工具箱包含在java.awt包中,它提供了許多用來設計GUI的組件類和容器類。
Swing:用戶界面庫是非基於對等體的GUI工具箱,具有更豐富並且更方便的用戶界面元素集合。
Swing組件層次關系:
大部分AWT組件都有其Swing的等價組件。
組件:構成圖形用戶界面的元素,拿來即用。通常把由Component類的子類或間接子類創建的對象稱為一個組件。
容器:容器是Java中能容納和排列組件的組件。常用的容器是框架(Frame,JFrame)
添加組件:
框架創建:
創建空框架:用框架(Frame)創建初始界面 JFrame frame = new JFrame();
框架定位與框架屬性:常用Component類的setLocation和setBounds方法
Title:框架標題 IconImage:框架
確定框架大小:通過調用Toolkit類的方法來得到屏幕尺寸信息
創建框架時的常用類及API:
– boolean isVisible() 檢查組件是否可見
– void setVisible(boolean b) 設置組件可見
– void setSize(int width,int height) 把組件縮放到指定寬度和高度
– void setBounds(int x,int y,int width,int height) 移動並縮放組件
– Dimension getSize() 得到組件的大小
– void setSize(Dimension d) 把組件縮放到指定的大小
void setResizable(boolean b) 縮放框架
– void setTitle(String s) 設置框架標題
– void setIconImage(Image image) 將Image用作框架圖標
static Toolkit getDefaultToolkit() 返回默認工具箱
– Dimension getScreenSize()得到用戶屏幕大小
2D對象:Java SE了包含一個Java 2D庫,該庫提供了一個非常強大的圖形操作集
Java 2D圖形類使用浮點數坐標系,這樣可為坐標指定單位
2D庫為每個圖形類提供兩個版本的靜態內部類
二:實驗部分
1、實驗目的與要求
(1) 掌握Java GUI中框架創建及屬性設置中常用類的API;
(2) 掌握Java GUI中2D圖形繪制常用類的API;
(3) 了解Java GUI中2D圖形中字體與顏色的設置方法;
(4) 了解Java GUI中2D圖像的載入方法。
2、實驗內容和步驟
實驗1: 導入第10章示例程序,測試程序並進行代碼註釋。
測試程序1:
l 運行下列程序,觀察程序運行結果。
import javax.swing.*; public class SimpleFrameTest { public static void main(String[] args) { JFrame frame = new JFrame(); frame.setBounds(0, 0,300, 200); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } } |
l 在elipse IDE中調試運行教材407頁程序10-1,結合程序運行結果理解程序;與上面程序對比,思考異同;
l 掌握空框架創建方法;
l 了解主線程與事件分派線程概念;
l 掌握GUI頂層窗口創建技術。
package simpleFrame; 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(() -> { SimpleFrame frame = new SimpleFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); }); } } 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:
l 在elipse IDE中調試運行教材412頁程序10-2,結合程序運行結果理解程序;
l 掌握確定框架常用屬性的設置方法。
package sizedFrame; 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(); frame.setTitle("SizedFrame"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); }); } } class SizedFrame extends JFrame { public SizedFrame() { // get screen dimensions Toolkit kit = Toolkit.getDefaultToolkit(); Dimension screenSize = kit.getScreenSize(); int screenHeight = screenSize.height; int screenWidth = screenSize.width; // set frame width, height and let platform pick screen location setSize(screenWidth / 2, screenHeight / 2); setLocationByPlatform(true); // set frame icon Image img = new ImageIcon("icon.gif").getImage(); setIconImage(img); } }
測試程序3:
l 在elipse IDE中調試運行教材418頁程序10-3,結合運行結果理解程序;
l 掌握在框架中添加組件;
掌握自定義組件的用法。
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(); frame.setTitle("NotHelloWorld"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); }); } } /** * 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:
l 在elipse IDE中調試運行教材424 -425頁程序10-4,結合程序運行結果理解程序;
l 掌握2D圖形的繪制方法。
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; // draw a rectangle double leftX = 100; double topY = 100; double width = 200; double height = 150; Rectangle2D rect = new Rectangle2D.Double(leftX, topY, width, height); g2.draw(rect); // draw the enclosed ellipse Ellipse2D ellipse = new Ellipse2D.Double(); ellipse.setFrame(rect); g2.draw(ellipse); // draw a diagonal line g2.draw(new Line2D.Double(leftX, topY, leftX + width, topY + height)); // draw a circle with the same center 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:
l 在elipse IDE中調試運行教材432頁-433程序10-5,結合程序運行結果理解程序;
l 了解2D圖形中字體的設置的方法;
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); // measure the size of the message FontRenderContext context = g2.getFontRenderContext(); Rectangle2D bounds = f.getStringBounds(message, context); // set (x,y) = top left corner of text double x = (getWidth() - bounds.getWidth()) / 2; double y = (getHeight() - bounds.getHeight()) / 2; // add ascent to y to reach the baseline double ascent = -bounds.getY(); double baseY = y + ascent; // draw the message g2.drawString(message, (int) x, (int) baseY); g2.setPaint(Color.LIGHT_GRAY); // draw the baseline g2.draw(new Line2D.Double(x, baseY, x + bounds.getWidth(), baseY)); // draw the enclosing rectangle 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:
l 在elipse IDE中調試運行教材436頁-437程序10-6,結合程序運行結果理解程序;
l 了解2D圖形圖像的顯示方法。
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); // draw the image in the upper-left corner g.drawImage(image, 0, 0, null); // tile the image across the component 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); } }
實驗2:課後完成PTA平臺題目集:2018秋季西北師範大學面向對象程序設計(Java)練習題集(ch6-ch9)
總結:通過這周JAVA課程學習了圖形程序設計,學到了在圖形用戶界面中的組件和容器的相關內容,在框架和繪畫方面有了一定的了解。
201771010118 馬昕璐《面向對象程序設計java》第十二周學習總結