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

转载于:https://www.cnblogs.com/nwpulq/archive/2009/04/17/1438017.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值