首页 技术 正文
技术 2022年11月21日
0 收藏 817 点赞 4,194 浏览 8451 个字

第十二周学习总结


第一部分:理论知识

内容概要: AWT与Swing简介;框架的创建;图形程序设计; 显示图像;

1.AWT组件:

201771010134杨其菊《面向对象程序设计java》第十二周学习总结

2.Swing 组件层次关系

201771010134杨其菊《面向对象程序设计java》第十二周学习总结

3 .AWT与Swing的关系:大部分AWT组件都有其Swing的等价组件; Swing组件的名字一般是在AWT组件名前面添加一个字母“J”,如:JButton,JFrame,JPanel等。

4.组件:通常把由Component类的子类或间接子类创建的对象称为一个组件。 例:Button button = new Button();TextField textField = new TextField();Label label = new Label();

5.组件类的常用API

java.awt.Component 1.0

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

6.容器:容器是Java中能容纳和排列组件的组件。 常用的容器是框架(Frame,JFrame)例:Frame fra = new Frame(“这是一个窗口”);

7. java.awt.Frame 类的常用API

void setResizable(boolean b) 缩放框架

void setTitle(String s) 设置框架标题

void setIconImage(Image image) 将Image用作框架图标

8.添加组件

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

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

9.框架(Frame)的创建

(1)创建空框架:在Java中,常采用框架(Frame)创建初始界面,即GUI的顶层窗口

(2)AWT库中有一个基于对等体的Frame类;该类的Swing版本为JFrame,JFrame是Frame子类。


第二部分:实验部分

实验十二  图形程序设计

实验时间 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);

}

}

 package First; import javax.swing.*;
public class SimpleFrameTest1
{
public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.setBounds(0, 0,300, 200);//设置窗口的位置(0,0)以及大小(宽高);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置默认的关闭操作;
frame.setVisible(true); //boolean设置窗口是否可见;
}
}

201771010134杨其菊《面向对象程序设计java》第十二周学习总结

201771010134杨其菊《面向对象程序设计java》第十二周学习总结

l 在elipse IDE中调试运行教材407页程序10-1,结合程序运行结果理解程序;与上面程序对比,思考异同;

l 掌握空框架创建方法;

l 了解主线程与事件分派线程概念;

l 掌握GUI顶层窗口创建技术。

 package First; 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//父类JFrame
{
private static final int DEFAULT_WIDTH = 300;//静态私有属性宽;
private static final int DEFAULT_HEIGHT = 200;//静态私有属性高; public SimpleFrame()//构造器
{
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
}
}

201771010134杨其菊《面向对象程序设计java》第十二周学习总结

测试程序2:

l 在elipse IDE中调试运行教材412页程序10-2,结合程序运行结果理解程序;

l 掌握确定框架常用属性的设置方法。

 package First;
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);
}
}

201771010134杨其菊《面向对象程序设计java》第十二周学习总结

测试程序3:

l 在elipse IDE中调试运行教材418页程序10-3,结合运行结果理解程序;

l 掌握在框架中添加组件;

l 掌握自定义组件的用法。

 package First; 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); }
}

201771010134杨其菊《面向对象程序设计java》第十二周学习总结

测试程序4:

l 在elipse IDE中调试运行教材424 -425页程序10-4,结合程序运行结果理解程序;

l 掌握2D图形的绘制方法。

 package First; 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); }
}

201771010134杨其菊《面向对象程序设计java》第十二周学习总结

测试程序5:

l 在elipse IDE中调试运行教材432页-433程序10-5,结合程序运行结果理解程序;

l 了解2D图形中字体的设置的方法;

 package First; 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); }
}

201771010134杨其菊《面向对象程序设计java》第十二周学习总结

测试程序6:

l 在elipse IDE中调试运行教材436页-437程序10-6,结合程序运行结果理解程序;

l 了解2D图形图像的显示方法。

 package First; 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); }
}

201771010134杨其菊《面向对象程序设计java》第十二周学习总结

实验2:课后完成PTA平台题目集:2018秋季西北师范大学面向对象程序设计(Java)练习题集(ch6-ch9)


第三部分:总结

1. 本周学习了图形用户界面的一些基础的设计操作,比如窗口显示不同图形,文字等等,由于设计性较强还是觉得很有意思;

2.对过去的内容的提升:通过练习题集练习学过的内容;

3.不断更新学习方法、学习内容;

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,000
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,512
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,358
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,141
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,771
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,849