zoukankan      html  css  js  c++  java
  • 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库中容器,组件和框架的的定义,学习了框架的使用方法。虽然看似简单,但是我在课本中学习到的只是一小部分,还需要更加系统的学习,让它更加美观,实用。

              其次,通过练习,我觉得对于之前的学习,我还需加强,不能边学边忘,而是把这些知识都串联起来,运用起来。

  • 相关阅读:
    想起来好久没更新博客了
    操作系统文件管理
    PreparedStatement是如何大幅度提高性能的
    Java中快速排序的实现
    详解HashMap的内部工作原理
    关于Java集合的总结
    浅谈JVM内存区域划分
    解决java压缩图片透明背景变黑色的问题
    Vmware15.5中centos7minimal版 窗口字体太小
    字符长度还是字节长度
  • 原文地址:https://www.cnblogs.com/Weiron/p/9977756.html
Copyright © 2011-2022 走看看