⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pointfactory.java

📁 《透视Java》的源码
💻 JAVA
字号:
package covertjava.profile;

import javax.swing.*;
import java.util.ArrayList;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

/**
 * <p>Implements Factory pattern for points creation</p>
 * <p>Copyright: Copyright (c) 2004 Sams Publishing</p>
 * @author Alex Kalinovsky
 * @version 1.0
 */
public class PointFactory {

    protected ArrayList points = new ArrayList();
    protected static PointFactory instance = new PointFactory();

    public Point createPoint(int x, int y) {
        Point point = new Point(x, y);
        this.points.add(point);
        return point;
    }

    public void removePoint(Point point) {
        this.points.remove(point);
    }


    public void printTestPoints() {
        for (int i = 0; i < 5; i++) {
            Point point = createPoint(i, i);
            System.out.println("Point = " + point);
        }
    }


    public static PointFactory getInstance() {
        return instance;
    }


    public static void main(String[] args) throws Exception {
        JFrame frame = new JFrame("Points Test");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        JButton button = new JButton("Print Test Points");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                PointFactory.getInstance().printTestPoints();
            }
        });
        frame.getContentPane().add(button);
        frame.setSize(200, 100);
        frame.setVisible(true);
    }


}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -