一、理论知识部分

Java的抽象窗口工具箱(Abstract Window Toolkit, AWT)包含在java.awt包中,它提供了许多用来设计GUI的组件类和容器类。

大部分AWT组件都有其Swing的等价组件,Swing组件的名字一般是在AWT组件名前面添加一个字母“J”。

通常把由Component类的子类或间接子类创建的对象称为一个组件。

组件类的常用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 setLocation(int x,int y)

容器是Java中能容纳和排列组件的组件。常用的容器是框架(Frame,JFrame)。

Container类提供了一个方法add(),用来在容器类 组件对象中添加其他组件。

容器本身也是一个组件,可以把一个容器添加到 另一个容器里,实现容器嵌套。

框架定位:常用Component类的setLocation和setBounds方法。常用属性:Title:框架标题;IconImage:框架图标。

确定框架大小:通过调用Toolkit类的方法来得到屏幕尺寸信息。

Jframe的结构:它由根面板、层级面板、玻璃面板和内容面板(content pane)四层面板构成。

Swing程序员最关心的是内容面板,也称为内容窗格。

在AWT中可调用add()方法把组件直接添加到AWT Frame中,在Swing中组件则添加到内容窗格里。

其中内容窗格是用来添加组件的,添加代码如下:Container contentPane = getContentPane(); Component c=…; contentPane.add(c);

用户也可以自行创建一个组件类,并在组件上进行绘制,此时需要重载paintComponent()。用户的自建组件也可添加到内容窗格里。

paintComponent(Graphics g)定义在JComponent类中,该方法在窗口需要重新绘图时(如扩大窗口或极小化窗口),被系统自动调用.

paintComponent()方法被调用时,系统就自动产生一个Graphics类型的参数,传递给paintComponent方法中的参数g。

二、实验部分

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);

}

}

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);//设置用户在此窗体上发起 "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(() -> // lambda表达式
        {JFrame frame = new SizedFrame();frame.setTitle("SizedFrame");frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 设置用户在此窗体上发起 "close" 时默认执行的操作frame.setVisible(true);// 设置窗口可见
        });}
}class SizedFrame extends JFrame {public SizedFrame() {// get screen dimensions
Toolkit kit = Toolkit.getDefaultToolkit();// 生成Toolkit对象Dimension screenSize = kit.getScreenSize();// 返回Dimension对象的屏幕大小int screenHeight = screenSize.height;// 获得Dimension对象屏幕的高度int screenWidth = screenSize.width;// 获得Dimension对象屏幕的宽度// 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 掌握在框架中添加组件;

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 rectangledouble 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 centerdouble 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 textdouble x = (getWidth() - bounds.getWidth()) / 2;double y = (getHeight() - bounds.getHeight()) / 2;// add ascent to y to reach the baselinedouble 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 = 350;private static final int DEFAULT_HEIGHT = 250;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 componentfor (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)

三:实验总结:

通过这次试验我了解了空框架创建方法、确定框架常用属性的设置方法、自定义组件的用法,还接触了解了2D图形的绘制方法,字体的设置的方法及图像的显示方法。通过做练习题集,对之前知识进行回顾,温故知新。

转载于:https://www.cnblogs.com/sisi-713/p/9965202.html

徐思201771010132《面向对象程序设计(Java)》第十二周学习总结相关推荐

  1. 201771010118马昕璐《面向对象程序设计java》第八周学习总结

    第一部分:理论知识学习部分 1.接口 在Java程序设计语言中,接口不是类,而是对类的一组需求描述,由常量和一组抽象方法组成.Java为了克服单继承的缺点,Java使用了接口,一个类可以实现一个或多个 ...

  2. 20177101010101 白玛次仁《面向对象程序设计》第十八周学习总结

    实验十八  总复习 实验时间 2018-12-30 1.实验目的与要求 (1) 综合掌握java基本程序结构: (2) 综合掌握java面向对象程序设计特点: (3) 综合掌握java GUI 程序设 ...

  3. 201671010103 2016-2017-2 《Java程序设计》第十二周学习心得

    Java的抽象窗口工具箱(Abtract Window Toolkit,AWT )包含在java.awt中它提供了许多用来设计GUI的组件类和容器类:而Swing类库被包含在javax.swing包里 ...

  4. 面向对象程序设计上机练习十二(运算符重载)

    面向对象程序设计上机练习十二(运算符重载) Time Limit: 1000MS  Memory Limit: 65536KB Submit  Statistic Problem Descriptio ...

  5. 面向对象程序设计——Java语言 第3周编程题 查找里程(10分)

    面向对象程序设计--Java语言 第3周编程题 查找里程(10分) 题目内容 下图为国内主要城市之间的公路里程: 你的程序要读入这样的一张表,然后,根据输入的两个城市的名称,给出这两个城市之间的里程. ...

  6. 20145240《信息安全系统设计基础》第十二周学习总结

    20145240<信息安全系统设计基础>第十二周学习总结 前三周博客链接 第九周学习总结 第十周学习总结 第十一周学习总结 视频学习内容总结 指针数组与数组指针 数组指针(也称行指针) 定 ...

  7. 20145206 《信息安全系统设计基础》第十二周学习总结

    20145206 <信息安全系统设计基础>第十二周学习总结 本周学习目标 1.第九周代码检查 2.第十周代码检查 3.第十一周代码检查 博客链接 20145206 <信息安全系统设计 ...

  8. 第十二周学习周报(20180521-20180527)

    第十二周学习周报 一.本周学习情况 1.学习了吴恩达老师微专业课<深度学习工程师> 第三篇:结构化机器学习项目 第一周内容:机器学习(ML)策略(1) 2.看了fast-rcnn算法详解和 ...

  9. 20135327郭皓——信息安全系统设计基础第十二周学习总结

    第十二周(11.23-11.29): 学习计时:共6小时 读书: 代码: 作业: 博客: 一.学习目标 掌握进程控制 掌握信号处理的方法 掌握管道和fifo进行进程间通信的方法 二.学习资源  编译. ...

  10. 第十二周-学习进度条

      第十二周 所花时间(包括上课) 20h 代码量(行) 230 博客园(篇) 2 了解到的知识点 fragment的相关知识 转载于:https://www.cnblogs.com/liujinxi ...

最新文章

  1. 数据蒋堂 | 多维分析预汇总的功能盲区
  2. 如何避免JSP文件中的Java代码?
  3. python判断英文字母_python判断字符串中是否含有英文 | 个人学习笔记记录
  4. 【转】glTexImage2D()和gluBuild2DMipmaps() [将载入的位图文件(*.bmp)转换成纹理贴图]+glTexParameteri()纹理过滤函数...
  5. 唐雄燕点评NFV产业进程:2016年将迎来试点年
  6. rabbitmq 用户管理
  7. git 存在多个commit 时将修改,追加到某次commit 上
  8. C3P0连接池的基本配置与使用
  9. 偏微分方程数值解法pdf_单摆-微分方程浅谈
  10. proteus网络编号
  11. 使用 jsbarcode 生成条形码
  12. CentOS官网下载对应版本
  13. 一款网页游戏外挂开发-数据抓包2
  14. 应用nslookup命令查看A记录、MX记录、CNAME记录和NS记录
  15. PDF怎么直接翻译成中文
  16. 答大三犹豫考研的同学兼向大一学生叨叨几句
  17. 思杰虚拟服务器退出管理主机,详解Citrix思杰XenServer虚拟化(8)
  18. 并发容器J.U.C -- AQS组件(一)
  19. 广州技术沙龙第 3 期报名开始!
  20. 温度上报实时监控项目——客户端

热门文章

  1. UART read过程分析
  2. 安装算量软件价格_鹏业安装算量软件正版购买渠道
  3. 200万年薪请不到!清华姚班到底有多牛X?
  4. [刀塔自走棋] 一些数据
  5. Linux usbkey自动登陆,Usbkey怎么用?|3分钟让您了解Usbkey使用方法
  6. 华为er路由器设置虚拟服务器,华为AR111-S路由器双线路策略路由配置笔记
  7. Centos 7.6 Install shc
  8. textarea字数实时统计方案
  9. vscode插件开发流程
  10. php类 汉子转拼音 通讯录按字母顺序排列