Java Tutorial/Swing/JWindow — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 15:31, 31 мая 2010
Содержание
Center window
/*
* Copyright (c) 1998-2002 Carnegie Mellon University. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS"" AND
* ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
* NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
import java.awt.*;
public abstract class Win {
public static void center (Window window, Component ref) {
position (window, ref, 0.5, 0.5);
}
public static void position (Window frame, Component ref,
double xfrac, double yfrac) {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension size = frame.getSize();
Dimension refSize = (ref != null)
? ref.getSize()
: screenSize;
Point origin = (ref != null)
? ref.getLocationOnScreen ()
: new Point (0, 0);
int x = origin.x + relativePoint (xfrac, refSize.width, size.width);
int y = origin.y + relativePoint (yfrac, refSize.height, size.height);
// make sure frame is entirely on screen
x = Math.max (0, Math.min (screenSize.width - size.width, x));
y = Math.max (0, Math.min (screenSize.height - size.height, y));
frame.setLocation (x, y);
}
static int relativePoint (double frac, int parentLength, int childLength) {
if (frac < 0)
return (int) (frac * childLength);
else if (frac > 1)
return (int) (parentLength + (frac - 2) * childLength);
else
return (int) (frac * (parentLength - childLength));
}
public static Frame findFrame (Component comp) {
for (; comp!=null; comp = comp.getParent ())
if (comp instanceof Frame) {
return (Frame)comp;
}
return null;
}
public static Frame findFrameOrMakeFrame (Component parent) {
return (parent != null) ? findFrame (parent) : new Frame ();
}
}
Center Within Parent
/*
* Copyright (C) 2001-2004 Colin Bell
* colbell@users.sourceforge.net
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
import java.awt.ruponent;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.Frame;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Insets;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.Window;
import java.awt.geom.Rectangle2D;
import java.beans.PropertyVetoException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.SwingUtilities;
/**
* Common GUI utilities accessed via static methods.
*
* @author
*/
public class GUIUtils {
private static JFrame _mainFrame;
/**
* Centers <CODE>wind</CODE> within its parent. If it has no parent then
* center within the screen. If centering would cause the title bar to go
* above the parent (I.E. cannot see the titlebar and so cannot move the
* window) then move the window down.
*
* @param wind
* The Window to be centered.
*
* @throws IllegalArgumentException
* If <TT>wind</TT> is <TT>null</TT>.
*/
public static void centerWithinParent(Window wind) {
if (wind == null) {
throw new IllegalArgumentException("null Window passed");
}
final Container parent = wind.getParent();
if (parent != null && parent.isVisible()) {
center(wind, new Rectangle(parent.getLocationOnScreen(), parent.getSize()));
} else {
centerWithinScreen(wind);
}
}
/**
* Centers passed internal frame within its desktop area. If centering would
* cause the title bar to go off the top of the screen then move the window
* down.
*
* @param frame
* The internal frame to be centered.
*
* @throws IllegalArgumentException
* If <TT>frame</TT> is <TT>null</TT>.
*/
public static void centerWithinDesktop(JInternalFrame frame) {
if (frame == null) {
throw new IllegalArgumentException("null JInternalFrame passed");
}
final Container parent = frame.getDesktopPane();
if (parent != null && parent.isVisible()) {
center(frame, new Rectangle(new Point(0, 0), parent.getSize()));
}
}
/**
* Centers <CODE>wind</CODE> within the screen. If centering would cause the
* title bar to go off the top of the screen then move the window down.
*
* @param wind
* The Window to be centered.
*
* @throws IllegalArgumentException
* If <TT>wind</TT> is <TT>null</TT>.
*/
public static void centerWithinScreen(Window wind) {
if (wind == null) {
throw new IllegalArgumentException("null Window passed");
}
final Toolkit toolKit = Toolkit.getDefaultToolkit();
final Rectangle rcScreen = new Rectangle(toolKit.getScreenSize());
final Dimension windSize = wind.getSize();
final Dimension parentSize = new Dimension(rcScreen.width, rcScreen.height);
if (windSize.height > parentSize.height) {
windSize.height = parentSize.height;
}
if (windSize.width > parentSize.width) {
windSize.width = parentSize.width;
}
center(wind, rcScreen);
}
/**
* Centers <CODE>wind</CODE> within the passed rectangle.
*
* @param wind
* The Window to be centered.
* @param rect
* The rectangle (in screen coords) to center <CODE>wind</CODE>
* within.
*
* @throws IllegalArgumentException
* If <TT>Window</TT> or <TT>Rectangle</TT> is <TT>null</TT>.
*/
private static void center(Component wind, Rectangle rect) {
if (wind == null || rect == null) {
throw new IllegalArgumentException("null Window or Rectangle passed");
}
Dimension windSize = wind.getSize();
int x = ((rect.width - windSize.width) / 2) + rect.x;
int y = ((rect.height - windSize.height) / 2) + rect.y;
if (y < rect.y) {
y = rect.y;
}
wind.setLocation(x, y);
}
}
Difference between JFrame and JWindow
import javax.swing.JFrame;
import javax.swing.JWindow;
public class Main {
public static void main(String[] args) {
JFrame f = new JFrame("The Frame");
f.setSize(300, 300);
f.setLocation(100, 100);
JWindow w = new JWindow();
w.setSize(300, 300);
w.setLocation(500, 100);
f.setVisible(true);
w.setVisible(true);
}
}
Extending JWindow
If you need to extend JWindow, the class has two protected methods of importance:
protected void windowInit()
protected JRootPane createRootPane()
Have borders on a JWindow/JFrame
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JWindow;
public class Main{
public static void main(String s[]) {
JWindow win = new JWindow();
JPanel pan = new JPanel();
win.add(pan, "Center");
pan.setLayout(new FlowLayout());
pan.add(new JButton("Hello"));
win.setSize(200, 200);
win.setVisible(true);
}
}