下面列出了java.awt.Window#pack ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
protected void repackIfNecessary() {
Window window = SwingUtilities.getWindowAncestor(locationPane);
if (window == null)
return;
window.pack();
// TODO: make this a little smarter
/*
* if(isExpanded()==false) { window.pack(); return; }
*
* Dimension realSize = locationPane.getSize(); Dimension preferredSize
* = locationPane.getPreferredSize();
*
* if(realSize.height<preferredSize.height) { int deltaHeight =
* preferredSize.height-realSize.height; Dimension d = window.getSize();
* d.height += deltaHeight; window.setSize(d); }
*/
}
private void enlargeAsNecessary(int currentWidth,
int currentHeight,
int requestedWidth,
int requestedHeight) {
if ((currentWidth >= requestedWidth) && (currentHeight >= requestedHeight)) {
/* the panel is large enough */
return;
}
Window window = SwingUtilities.getWindowAncestor(this);
if (window == null) {
return;
}
try {
requestedSize = new Dimension(requestedWidth, requestedHeight);
window.pack();
} finally {
requestedSize = null;
}
}
private void expandWindowToFitNewConnectorForm() {
Window window = SwingUtilities.getWindowAncestor(this);
if (window == null) {
return;
}
Dimension currSize = getSize();
Dimension prefSize = getPreferredSize();
if ((currSize.width >= prefSize.width) && (currSize.height >= prefSize.height)) {
/* the dialog is large enough to fit the form */
return;
}
try {
requestedSize = new Dimension(
Math.max(currSize.width, prefSize.width),
Math.max(currSize.height, prefSize.height));
window.pack();
} finally {
requestedSize = null;
}
}
public void classChoiceChanged(Object chosen) {
this.chosenObject = chosen;
JComponent newChosenObjectEditor = null;
if (this.chosenObject instanceof OptionHandler) {
OptionHandler chosenOptionHandler = (OptionHandler) this.chosenObject;
newChosenObjectEditor = new OptionsConfigurationPanel(
chosenOptionHandler.getPurposeString(), chosenOptionHandler.getOptions());
}
if (this.chosenObjectEditor != null) {
remove(this.chosenObjectEditor);
}
this.chosenObjectEditor = newChosenObjectEditor;
if (this.chosenObjectEditor != null) {
add(this.chosenObjectEditor, BorderLayout.CENTER);
}
Component component = this;
while ((component != null) && !(component instanceof JDialog)) {
component = component.getParent();
}
if (component != null) {
Window window = (Window) component;
window.pack();
}
}
public static void packAndCenterWindowOn(Window window, Component centeredOn) {
window.pack();
Dimension prefSize = window.getPreferredSize();
Dimension minSize = window.getMinimumSize();
int width = Math.max(prefSize.width, minSize.width);
int height = Math.max(prefSize.height, minSize.height);
int x;
int y;
if (centeredOn != null) {
Point where = centeredOn.getLocationOnScreen();
Dimension size = centeredOn.getSize();
x = where.x + (size.width - width) / 2;
y = where.y + (size.height - height) / 2;
} else {
Rectangle bounds = getMaximumWindowBounds(window);
x = bounds.x + (bounds.width - width) / 2;
y = bounds.y + (bounds.height - height) / 2;
}
window.setLocation(x, y);
forceOnScreen(window);
}
public static void startSplashScreen()
{
int width=275,height=148;
Window win=new Window( new Frame() );
win.pack();
BshCanvas can=new BshCanvas();
can.setSize( width, height ); // why is this necessary?
Toolkit tk=Toolkit.getDefaultToolkit();
Dimension dim=tk.getScreenSize();
win.setBounds(
dim.width/2-width/2, dim.height/2-height/2, width, height );
win.add("Center", can);
Image img=tk.getImage(
Interpreter.class.getResource("/bsh/util/lib/splash.gif") );
MediaTracker mt=new MediaTracker(can);
mt.addImage(img,0);
try { mt.waitForAll(); } catch ( Exception e ) { }
Graphics gr=can.getBufferedGraphics();
gr.drawImage(img, 0, 0, can);
win.setVisible(true);
win.toFront();
splashScreen = win;
}
private void show(Window window, String command) {
JButton button = new JButton(command);
button.setActionCommand(command);
button.addActionListener(this);
button.setFont(button.getFont().deriveFont(64.0f));
window.add(button);
window.pack();
window.setVisible(true);
}
private void show(Window window, String command) {
JButton button = new JButton(command);
button.setActionCommand(command);
button.addActionListener(this);
button.setFont(button.getFont().deriveFont(64.0f));
window.add(button);
window.pack();
window.setVisible(true);
}
private void repack() {
contents.animateStates(layoutRunnable);
Window w = SwingUtilities.getWindowAncestor(this);
if (w != null) {
// TODO: this feature never really got off the ground, nor was it
// essential.
// if(animateResizing) {
// contents.resizeWindow();
// } else {
w.pack();
// }
}
}
private void show(Window window, String command) {
JButton button = new JButton(command);
button.setActionCommand(command);
button.addActionListener(this);
button.setFont(button.getFont().deriveFont(64.0f));
window.add(button);
window.pack();
window.setVisible(true);
}
/**
* Refreshes panel with options corresponding to the selected connector type.
* This method is called when a user selects new connector type.
*/
@Override
public void actionPerformed (ActionEvent e) {
int selectedIndex = ((JComboBox) e.getSource ()).getSelectedIndex ();
refresh (selectedIndex, Properties.getDefault ().getProperties ("debugger"));
Window w = SwingUtilities.getWindowAncestor(this);
if (w != null) {
w.pack (); // ugly hack...
}
}
private void show(Window window, String command) {
JButton button = new JButton(command);
button.setActionCommand(command);
button.addActionListener(this);
button.setFont(button.getFont().deriveFont(64.0f));
window.add(button);
window.pack();
window.setVisible(true);
}
private void show(Window window, String command) {
JButton button = new JButton(command);
button.setActionCommand(command);
button.addActionListener(this);
button.setFont(button.getFont().deriveFont(64.0f));
window.add(button);
window.pack();
window.setVisible(true);
}
private static void splashAndLicense()
{
splashWindow = new Window(masterWindow);
splashWindow.setLayout(new BorderLayout());
Image theImage = null;
String splashImageName;
if (Sage.get("ui/splash_image", null) != null)
{
theImage = Toolkit.getDefaultToolkit().createImage(Sage.get("ui/splash_image", null));
ImageUtils.ensureImageIsLoaded(theImage);
}
else
{
theImage = ImageUtils.fullyLoadImage(isTrueClient() ? (is64BitJVM() ? "images/splashclient64.gif" : "images/splashclient.gif") :
(is64BitJVM() ? "images/splash64.gif" : "images/splash.gif" ));
}
ActiveImage splashImage = new ActiveImage(theImage);
splashWindow.add(splashImage, "Center");
splashText = new Label(Sage.rez("Module_Init", new Object[] { "Application" }), Label.CENTER)
{
public void paint(Graphics g)
{
super.paint(g);
g.setColor(Color.black);
g.drawRect(0, 0, getWidth()-1, getHeight()-1);
}
};
splashText.setBackground(new Color(42, 103, 190));
splashText.setForeground(Color.white);
splashWindow.add(splashText, "South");
Dimension scrSize = Toolkit.getDefaultToolkit().getScreenSize();
splashWindow.pack();
Point center = GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint();
splashWindow.setLocation(center.x - splashWindow.getWidth()/2, center.y - splashWindow.getHeight()/2);
splashWindow.setVisible(true);
}
/**
* Center on parent ( absolute true/false (exact center or 25% upper left) )
*
* @param child
* @param absolute
*/
public static void centerOnParent(final Window child, final boolean absolute) {
child.pack();
boolean useChildsOwner = child.getOwner() != null
? ((child.getOwner() instanceof JFrame) || (child.getOwner() instanceof JDialog))
: false;
final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
final Dimension parentSize = useChildsOwner ? child.getOwner().getSize() : screenSize;
final Point parentLocationOnScreen =
useChildsOwner ? child.getOwner().getLocationOnScreen() : new Point(0, 0);
final Dimension childSize = child.getSize();
childSize.width = Math.min(childSize.width, screenSize.width);
childSize.height = Math.min(childSize.height, screenSize.height);
child.setSize(childSize);
int x;
int y;
if ((child.getOwner() != null) && child.getOwner().isShowing()) {
x = (parentSize.width - childSize.width) / 2;
y = (parentSize.height - childSize.height) / 2;
x += parentLocationOnScreen.x;
y += parentLocationOnScreen.y;
} else {
x = (screenSize.width - childSize.width) / 2;
y = (screenSize.height - childSize.height) / 2;
}
if (!absolute) {
x /= 2;
y /= 2;
}
child.setLocation(x, y);
}
public static void packLater(final Window win, final Component parent) {
win.pack();
win.setLocationRelativeTo(parent);
win.addWindowListener(new WindowAdapter() {
@Override
public void windowOpened(WindowEvent e) {
win.pack();
win.setLocationRelativeTo(parent);
}
});
}
private void show(Window window, String command) {
JButton button = new JButton(command);
button.setActionCommand(command);
button.addActionListener(this);
button.setFont(button.getFont().deriveFont(64.0f));
window.add(button);
window.pack();
window.setVisible(true);
}
private void show(Window window, String command) {
JButton button = new JButton(command);
button.setActionCommand(command);
button.addActionListener(this);
button.setFont(button.getFont().deriveFont(64.0f));
window.add(button);
window.pack();
window.setVisible(true);
}
private void show(Window window, String command) {
JButton button = new JButton(command);
button.setActionCommand(command);
button.addActionListener(this);
button.setFont(button.getFont().deriveFont(64.0f));
window.add(button);
window.pack();
window.setVisible(true);
}
/**
* Creates the Splash screen window and configures it.
*/
protected void initialize(ImageIcon iimage) {
image = iimage.getImage();
// if no image, return
if (image == null) {
throw new IllegalArgumentException("Image specified is invalid.");
}
// System.out.println(" here in splash ");
MediaTracker tracker = new MediaTracker(this);
tracker.addImage(image,0);
try {
tracker.waitForAll();
}
catch(Exception e) {
System.out.println(e.getMessage());
}
// create dialog window
f = new Frame();
dialog = new Window(f);
dialog.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
Dimension s = new Dimension(image.getWidth(this) + 2,
image.getHeight(this) + 2);
setSize(s);
dialog.setLayout(new BorderLayout());
dialog.setSize(s);
dialog.add(this,BorderLayout.CENTER);
dialog.pack();
// position splash screen
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
int x = (screen.width - s.width)/2;
if (x < 0) {
x = 0;
}
int y = (screen.height - s.height)/2;
if (y < 0) {
y = 0;
}
dialog.setLocation(x, y);
dialog.validate();
}