第一部分:理论部分:
1.集合框架中的接口:
2.集合框架中的实现类:
3.AWT组件(java.awt.*):
4.Swing 组件层次关系:
5.集合类的特点:
集合类特点一: 只容纳对象。
集合类特点二: 容纳的对象都是Object类的实例。
实验十二 图形程序设计:
实验时间 2018-11-14
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); } } |
1 import javax.swing.*; 2 public class SimpleFrameTest 3 { 4 public static void main(String[] args) 5 { 6 JFrame frame = new JFrame(); 7 frame.setBounds(600, 300,300, 300);//设置窗口的弹出位置和窗口的大小 8 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//点击窗口执行关闭的操作 9 frame.setVisible(true); //设置的窗口是否可见 10 } 11 }
l 在elipse IDE中调试运行教材407页程序10-1,结合程序运行结果理解程序;与上面程序对比,思考异同;
l 掌握空框架创建方法;
l 了解主线程与事件分派线程概念;
l 掌握GUI顶层窗口创建技术。
1 package simpleFrame; 2 3 import java.awt.*; 4 import javax.swing.*; 5 /** 6 * @version 1.33 2015-05-12 7 * @author Cay Horstmann 8 */ 9 public class SimpleFrameTest 10 { 11 public static void main(String[] args) 12 { 13 EventQueue.invokeLater(() ->//通过线程开启了一个队列 14 { 15 SimpleFrame frame = new SimpleFrame(); 16 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 17 //设置用户在此窗体上发起"close"时默认执行的操作时,使用System exit方法退出应用程序. 18 frame.setVisible(true);//设置页面是否为可见 19 }); 20 } 21 } 22 23 class SimpleFrame extends JFrame 24 { 25 private static final int DEFAULT_WIDTH = 300;//定义宽为300 26 private static final int DEFAULT_HEIGHT = 200;//定义高为200 27 28 public SimpleFrame()//构造函数 29 { 30 setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); 31 } 32 }
测试程序2:
l 在elipse IDE中调试运行教材412页程序10-2,结合程序运行结果理解程序;
l 掌握确定框架常用属性的设置方法。
1 package sizedFrame; 2 3 import java.awt.*; 4 import javax.swing.*; 5 6 /** 7 * @version 1.34 2015-06-16 8 * @author Cay Horstmann 9 */ 10 public class SizedFrameTest 11 { 12 public static void main(String[] args) 13 { 14 EventQueue.invokeLater(() -> 15 { 16 JFrame frame = new SizedFrame(); 17 frame.setTitle("SizedFrame"); 18 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 19 frame.setVisible(true); 20 }); 21 } 22 } 23 24 class SizedFrame extends JFrame 25 { 26 public SizedFrame() 27 { 28 // 获取屏幕尺寸 29 30 Toolkit kit = Toolkit.getDefaultToolkit();//生成Toolkit对象 31 Dimension screenSize = kit.getScreenSize();//返回Dimension的屏幕大小 32 int screenHeight = screenSize.height;//获得Dimension对象的宽度 33 int screenWidth = screenSize.width;//获得Dimension对象的高度 34 35 // 设置框架宽度、高度并让平台选择屏幕位置 36 37 setSize(screenWidth / 2, screenHeight / 2);//将框架的大小设定为上面取值的50% 38 setLocationByPlatform(true); 39 40 //设置帧图标 41 42 Image img = new ImageIcon("icon.gif").getImage();//将图像设置为框架图标 43 setIconImage(img); 44 } 45 }
测试程序3:
l 在elipse IDE中调试运行教材418页程序10-3,结合运行结果理解程序;
l 掌握在框架中添加组件;
l 掌握自定义组件的用法。
1 package notHelloWorld; 2 3 import javax.swing.*; 4 import java.awt.*; 5 6 /** 7 * @version 1.33 2015-05-12 8 * @author Cay Horstmann 9 */ 10 public class NotHelloWorld 11 { 12 public static void main(String[] args) 13 { 14 EventQueue.invokeLater(() -> 15 { 16 JFrame frame = new NotHelloWorldFrame(); 17 frame.setTitle("NotHelloWorld"); 18 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 19 frame.setVisible(true); 20 }); 21 } 22 } 23 24 /** 25 * 包含消息面板的框架。 26 */ 27 class NotHelloWorldFrame extends JFrame 28 { 29 public NotHelloWorldFrame() 30 { 31 add(new NotHelloWorldComponent()); 32 pack(); 33 } 34 } 35 36 /** 37 *显示消息的组件。 38 */ 39 class NotHelloWorldComponent extends JComponent 40 { 41 public static final int MESSAGE_X = 75; 42 public static final int MESSAGE_Y = 100; 43 44 private static final int DEFAULT_WIDTH = 300; 45 private static final int DEFAULT_HEIGHT = 200; 46 47 public void paintComponent(Graphics g) 48 { 49 g.drawString("Not a Hello, World program", MESSAGE_X, MESSAGE_Y); 50 } 51 52 public Dimension getPreferredSize() { return new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT); } 53 }
测试程序4:
l 在elipse IDE中调试运行教材424 -425页程序10-4,结合程序运行结果理解程序;
l 掌握2D图形的绘制方法。
1 package draw; 2 3 import java.awt.*; 4 import java.awt.geom.*; 5 import javax.swing.*; 6 7 /** 8 * @version 1.33 2007-05-12 9 * @author Cay Horstmann 10 */ 11 public class DrawTest 12 { 13 public static void main(String[] args) 14 { 15 EventQueue.invokeLater(() -> 16 { 17 JFrame frame = new DrawFrame(); 18 frame.setTitle("DrawTest"); 19 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 20 frame.setVisible(true); 21 }); 22 } 23 } 24 25 /** 26 *包含图画面板的框架 27 */ 28 class DrawFrame extends JFrame 29 { 30 public DrawFrame() 31 { 32 add(new DrawComponent()); 33 pack(); 34 } 35 } 36 37 /** 38 * 显示矩形和椭圆的组件。 39 */ 40 class DrawComponent extends JComponent 41 { 42 private static final int DEFAULT_WIDTH = 400; 43 private static final int DEFAULT_HEIGHT = 400; 44 45 public void paintComponent(Graphics g) 46 { 47 Graphics2D g2 = (Graphics2D) g; 48 49 // 画一个长方形; 50 51 double leftX = 100; 52 double topY = 100; 53 double width = 200; 54 double height = 150; 55 56 Rectangle2D rect = new Rectangle2D.Double(leftX, topY, width, height); 57 g2.draw(rect); 58 59 // 画封闭椭圆 60 61 Ellipse2D ellipse = new Ellipse2D.Double(); 62 ellipse.setFrame(rect); 63 g2.draw(ellipse); 64 65 // 画一条对角线 66 67 g2.draw(new Line2D.Double(leftX, topY, leftX + width, topY + height)); 68 69 // 用同一个中心画一个圆 70 71 double centerX = rect.getCenterX(); 72 double centerY = rect.getCenterY(); 73 double radius = 150; 74 75 Ellipse2D circle = new Ellipse2D.Double(); 76 circle.setFrameFromCenter(centerX, centerY, centerX + radius, centerY + radius); 77 g2.draw(circle); 78 } 79 80 public Dimension getPreferredSize() { return new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT); } 81 }
测试程序5:
l 在elipse IDE中调试运行教材432页-433程序10-5,结合程序运行结果理解程序;
l 了解2D图形中字体的设置的方法;
1 package font; 2 3 import java.awt.*; 4 import java.awt.font.*; 5 import java.awt.geom.*; 6 import javax.swing.*; 7 8 /** 9 * @version 1.34 2015-05-12 10 * @author Cay Horstmann 11 */ 12 public class FontTest 13 { 14 public static void main(String[] args) 15 { 16 EventQueue.invokeLater(() -> 17 { 18 JFrame frame = new FontFrame(); 19 frame.setTitle("FontTest"); 20 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 21 frame.setVisible(true); 22 }); 23 } 24 } 25 26 /** 27 * 带有文本消息组件的框架 28 */ 29 class FontFrame extends JFrame 30 { 31 //构造器 32 public FontFrame() 33 { 34 add(new FontComponent()); 35 pack(); 36 } 37 } 38 39 /** 40 * 在框中显示中心消息的组件。 41 */ 42 class FontComponent extends JComponent 43 { 44 private static final int DEFAULT_WIDTH = 300; 45 private static final int DEFAULT_HEIGHT = 200; 46 47 public void paintComponent(Graphics g) 48 { 49 Graphics2D g2 = (Graphics2D) g; 50 51 String message = "柸中有雪"; 52 53 Font f = new Font("Serif", Font.BOLD, 36); 54 g2.setFont(f); 55 56 //测量消息的大小 57 58 FontRenderContext context = g2.getFontRenderContext(); 59 Rectangle2D bounds = f.getStringBounds(message, context); 60 61 // 设置(x,y)=文本左上角 62 63 double x = (getWidth() - bounds.getWidth()) / 2; 64 double y = (getHeight() - bounds.getHeight()) / 2; 65 66 // 增加到Y到达基线 67 68 double ascent = -bounds.getY(); 69 double baseY = y + ascent; 70 71 // 汲取信息 72 73 g2.drawString(message, (int) x, (int) baseY); 74 75 g2.setPaint(Color.LIGHT_GRAY); 76 77 // 绘制基线 78 79 g2.draw(new Line2D.Double(x, baseY, x + bounds.getWidth(), baseY)); 80 81 // 绘制封闭矩形 82 83 Rectangle2D rect = new Rectangle2D.Double(x, y, bounds.getWidth(), bounds.getHeight()); 84 g2.draw(rect); 85 } 86 87 public Dimension getPreferredSize() { return new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT); } 88 }
测试程序6:
l 在elipse IDE中调试运行教材436页-437程序10-6,结合程序运行结果理解程序;
l 了解2D图形图像的显示方法。
1 package image; 2 3 import java.awt.*; 4 import javax.swing.*; 5 6 /** 7 * @version 1.34 2015-05-12 8 * @author Cay Horstmann 9 */ 10 public class ImageTest 11 { 12 public static void main(String[] args) 13 { 14 EventQueue.invokeLater(() -> 15 { 16 JFrame frame = new ImageFrame(); 17 frame.setTitle("ImageTest"); 18 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 19 frame.setVisible(true); 20 }); 21 } 22 } 23 24 /** 25 *具有图像分量的帧 26 */ 27 class ImageFrame extends JFrame 28 { 29 public ImageFrame() 30 { 31 add(new ImageComponent()); 32 pack(); 33 } 34 } 35 36 /** 37 * 显示平铺图像的组件。 38 */ 39 class ImageComponent extends JComponent 40 { 41 private static final int DEFAULT_WIDTH = 1500; 42 private static final int DEFAULT_HEIGHT = 800; 43 44 private Image image; 45 46 public ImageComponent() 47 { 48 image = new ImageIcon("blue-ball.gif").getImage(); 49 } 50 51 public void paintComponent(Graphics g) 52 { 53 if (image == null) return; 54 55 int imageWidth = image.getWidth(null); 56 int imageHeight = image.getHeight(null); 57 58 // 在左上角绘制图像 59 60 g.drawImage(image, 0, 0, null); 61 // 在组件上平铺图像 62 63 for (int i = 0; i * imageWidth <= getWidth(); i++) 64 for (int j = 0; j * imageHeight <= getHeight(); j++) 65 if (i + j > 0) 66 g.copyArea(0, 0, imageWidth, imageHeight, i * imageWidth, j * imageHeight); 67 } 68 69 public Dimension getPreferredSize() { return new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT); } 70 }
实验总结:
System.exit( )和frame.setDefaultCloseOperation()的区别:
System.exit(0)是退出整个程序,如果有多个窗口,全部都销毁退出。
frame.setDefaultCloseOperation()是设置用户在此窗体上发起"close"时默认执行的操作。必须指定以下选项之一: 、
(1)DO_NOTHING_ON_CLOSE(在WindowConstants中定义):不执行任何操作,要求程序在已注册的 WindowListener 对象的 windowClosing 方法中处理该操作。
(2)HIDE_ON_CLOSE(在WindowConstants中定义):调用任意已注册的WindowListener对象后自动隐藏该窗体。
(3)DISPOSE_ON_CLOSE(在WindowConstants中定义):调用任意已注册WindowListener的对象后自动隐藏并释放该窗体。
(4)EXIT_ON_CLOSE(在JFrame中定义):使用System exit方法退出应用程序。仅在应用程序中使用。
默认情况下,该值被设置为 HIDE_ON_CLOSE
也就是说没有设置的话,默认点关闭时只是隐藏窗体,在后台进程中还可以看到,如果有多个窗口,只是销毁调用dispose的窗口,其他窗口仍然存在,整个应用程序还是处于运行状态。