设置JFrame剧中显示

网上查到的一种方法:

    /**
* 设置整个界面居中显示
*/
private void setCenterDisplay() {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = this.getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}

this.setLocation((screenSize.width - frameSize.width) / 2,
(screenSize.height - frameSize.height) / 2);
}

JfreeChart中实现的一种方法,位于jcommon.jar中的org.jfree.ui.RefineryUtilities中:

   /**
* Positions the specified frame in the middle of the screen.
*
*
@param frame the frame to be centered on the screen.
*/
public static void centerFrameOnScreen(final Window frame) {
positionFrameOnScreen(frame, 0.5, 0.5);
}

/**
* Positions the specified frame at a relative position in the screen, where 50% is considered
* to be the center of the screen.
*
*
@param frame the frame.
*
@param horizontalPercent the relative horizontal position of the frame (0.0 to 1.0,
* where 0.5 is the center of the screen).
*
@param verticalPercent the relative vertical position of the frame (0.0 to 1.0, where
* 0.5 is the center of the screen).
*/
public static void positionFrameOnScreen(final Window frame,
final double horizontalPercent,
final double verticalPercent) {

final Rectangle s = frame.getGraphicsConfiguration().getBounds();
final Dimension f = frame.getSize();
final int w = Math.max(s.width - f.width, 0);
final int h = Math.max(s.height - f.height, 0);
final int x = (int) (horizontalPercent * w) + s.x;
final int y = (int) (verticalPercent * h) + s.y;
frame.setBounds(x, y, f.width, f.height);

}



posted on 2012-02-10 15:05  散人  阅读(456)  评论(0编辑  收藏  举报

导航