nwpulq

2009年4月17日

FileOutput类---载自《java软件开发》Russel Winder & Graham Roberts

import java.io.*;
/**
 * A simple output class to write values to a file of characters.
 * If any file errors occur, methods in this class will display
 * an error message and terminate the program.
 *
 * @version 1.1 1999.09.10
 * @author Graham Roberts
 * @author Russel Winder
 *
 */
public class FileOutput {
    /**
     * Instance variables to store file name.
     */
    private String filename = "";
    /**
     * Instance variables to store stream.
     */
    private BufferedWriter writer = null;
    /**
     * Construct <CODE>FileOutput</CODE> object given a file name.
     */
    public FileOutput(final String name) {
        filename = name;
        try {
            writer = new BufferedWriter(new FileWriter(filename));
        }
        catch(IOException e) {
            error("Can't open file: " + filename);
        }
    }
    /**
     * Construct <CODE>FileOutput</CODE> object given a
     * <CODE>File</CODE> object..
     */
    public FileOutput(final File file) {
        filename = file.getName();
        try {
            writer = new BufferedWriter(new FileWriter(filename));
        }
        catch(IOException e) {
            error("Can't open file: " + filename);
        }
    }
    /**
     * Close the file when finished
     */
    public final synchronized void close() {
        try {
            writer.close();
        }
        catch(IOException e) {
            error("Can't close file: " + filename);
        }
    }
    /**
     * Write an <CODE>int</CODE> value to a file.
     */
    public final synchronized void writeInteger(final int i) {
        try {
            writer.write(Integer.toString(i));
        }
        catch(IOException e) {
            error("writeInteger failed for file: " + filename);
        }
    }
    /**
     * Write an <CODE>long</CODE> value to a file.
     */
    public final synchronized void writeLong(final long l) {
        try {
            writer.write(Long.toString(l));
        }
        catch(IOException e) {
            error("writeLong failed for file: " + filename);
        }
    }
    /**
     * Write an <CODE>double</CODE> value to a file.
     */
    public final synchronized void writeDouble(final double d) {
        try {
            writer.write(Double.toString(d));
        }
        catch(IOException e) {
            error("writeDouble failed for file: " + filename);
        }
    }
    /**
     * Write an <CODE>float</CODE> value to a file.
     */
    public final synchronized void writeFlaot(final float f) {
        try {
            writer.write(Float.toString(f));
        }
        catch(IOException e) {
            error("writeFloat failed for file: " + filename);
        }
    }
    /**
     * Write an <CODE>char</CODE> value to a file.
     */
    public final synchronized void writeCharacter(final char c) {
        try {
            writer.write(c);
        }
        catch(IOException e) {
            error("writeCharacter failed for file: " + filename);
        }
    }
    /**
     * Write an <CODE>string</CODE> value to a file.
     */
    public final synchronized void writeString(final String s) {
        try {
            writer.write(s);
        }
        catch(IOException e) {
            error("writeString failed for file: " + filename);
        }
    }
    /**
     * Write a newline to a file.
     */
    public final synchronized void writeNewline() {
        try {
            writer.write('\n');
        }
        catch(IOException e) {
            error("writeNewline failed for file: " + filename);
        }
    }
    /**
     *  Deal with a file error
     */
    private void error(String msg) {
        System.err.println(msg);
        System.err.println("Unable to continue executing program.");
        System.exit(0);
    }
}

posted @ 2009-04-17 14:25 李奇 阅读(118) 评论(0) 编辑

DrawFrame类---摘自《Java软件开发》Russel Winder & Graham Roberts

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
 * This class provides a basic window, with a quit button, that can
 * be used to create simple GUIs.
 *
 * @version 1.1 2000.01.07
 * @author Graham Roberts
 * @author Russel Winder
 */
public class DrawFrame extends JFrame {
    /**
     * Override of the method to add a <CODE>JPanel</CODE> to the
     * <CODE>DrawFrame</CODE> so that the <CODE>JPanel</CODE> is
     * centred.
     */
    public void add(final JPanel panel) {
        getContentPane().add(panel, BorderLayout.CENTER);
    }
    /**
     * Terminate the program when the user wants to quit.
     */
    private void quit() {
        System.exit(0);
    }
    /**
     * construct a <CODE>DrawFrame</CODE>
     */
    public DrawFrame(final String title) {
        // Initialize the JFrame ensuring the titlebar is set.
        super(title);
        // Set up the quit button with it's listener.
        Button quitButton = new Button("Quit");
        quitButton.addActionListener(new ActionListener() {
            public void actionPerformed(final ActionEvent e) {
                quit();
            }
        });
        // put all the buttons into a JPanel.
        JPanel buttonPanel = new JPanel(new FlowLayout());
        buttonPanel.add(quitButton);
        // Create the contents of the frame. Use BorderLayout with the
        // top (Center) part being the drawing area and the bottom
        // (South) strip holding a quit button.
        getContentPane().setLayout(new BorderLayout());
        getContentPane().add(buttonPanel, BorderLayout.SOUTH);
        // Ensure that windows close events from the window manager are
        // caught and acted upon.
        addWindowListener(new WindowAdapter() {
            public void windowClosing(final WindowEvent evt) {
                quit();
            }
        });
    }
    /**
     * Position a window in the centre of the screen.
     */
    public void centreOnScreen() {
        Dimension displaySize = getToolkit().getScreenSize();
        Dimension windowSize = getSize();
        int x = (displaySize.width - windowSize.width) / 2;
        int y = (displaySize.height - windowSize.height) / 2;
        if(x < 0) {
            x = 0;
        }
        if(y < 0) {
            y = 0;
        }
        setLocation(x,y);
    }
}

posted @ 2009-04-17 14:14 李奇 阅读(146) 评论(0) 编辑

导航

统计

公告