張季躍 201771010139《面向對象程序設計(java)》第十二週學習總結

張季躍 201771010139《面向對象程序設計(java)》第十二周學習總結java

第二部分:實驗部分編程

一、實驗目的與要求框架

(1) 掌握Java GUI中框架建立及屬性設置中經常使用類的API;學習

(2) 掌握Java GUI中2D圖形繪製經常使用類的API;測試

(3) 瞭解Java GUI中2D圖形中字體與顏色的設置方法;字體

(4) 瞭解Java GUI中2D圖像的載入方法。spa

二、實驗內容和步驟線程

實驗1: 導入第9章示例程序,測試程序並進行代碼註釋。設計

二、實驗內容和步驟調試

實驗1: 導入第10章示例程序,測試程序並進行代碼註釋。

測試程序1:

運行下列程序,觀察程序運行結果。

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

   }

}

elipse IDE中調試運行教材407頁程序10-1,結合程序運行結果理解程序;與上面程序對比,思考異同;

l 掌握空框架建立方法;

l 瞭解主線程與事件分派線程概念;

掌握GUI頂層窗口建立技術。

測試程序1:

package 測試程序1;

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);    //窗口可見

   }

}

 

測試結果:

 

 

測試程序2:

elipse IDE中調試運行教材412頁程序10-2,結合程序運行結果理解程序;

l 掌握肯定框架經常使用屬性的設置方法。

 

測試程序2:

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

   }

}

 

測試結果:

 

 

測試程序3:

elipse IDE中調試運行教材418頁程序10-3,結合運行結果理解程序;

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

l 掌握自定義組件的用法。

 

測試程序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();

            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:

elipse IDE中調試運行教材424 -425頁程序10-4,結合程序運行結果理解程序;

掌握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:

elipse IDE中調試運行教材432頁-433程序10-5,結合程序運行結果理解程序;

瞭解2D圖形中字體的設置的方法;

測試程序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);

 

      // 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:

elipse IDE中調試運行教材436頁-437程序10-6,結合程序運行結果理解程序;

瞭解2D圖形圖像的顯示方法。

測試程序SetDemo

import java.util.*;

public class SetDemo {

    public static void main(String[] argv) {

        HashSet h = new HashSet(); //也能夠 Set h=new HashSet()

        h.add("One");

        h.add("Two");

        h.add("One"); // DUPLICATE

        h.add("Three");

        Iterator it = h.iterator();

        while (it.hasNext()) {

             System.out.println(it.next());

        }

    }

}

測試結果:

 

 

第三部分:實驗總結:

     在這一週的學習中我第一次接觸了怎樣用Java編程來設計一個本身的窗口,雖然設計的窗口十分簡潔,只能設計大小和插入基本文字圖案,但也算在Java設計窗口方面開了一個好頭。另外,在本週的測試中,我發現我對前幾周學習的知識有所遺忘,好幾道題目都是查找資料才作出來的,從此要增強對之前學習知識的鞏固。

相關文章
相關標籤/搜索