Java Tutorial/Swing/Frame

Материал из Java эксперт
Перейти к: навигация, поиск

Get all windows with Frame.getFrames()

   <source lang="java">

import java.awt.Frame; import java.awt.Window; import javax.swing.JDialog; import javax.swing.JFrame; public class MainClass {

 public static void main(String[] args) {
   JDialog d1 = new JDialog((JFrame) null, "Dialog 1");
   d1.setName("Dialog 1");
   JDialog d2 = new JDialog((Window) null, "Dialog 2");
   d2.setName("Dialog 2");
   Frame f = new Frame();
   f.setName("Frame 1");
   Window w1 = new Window(f);
   w1.setName("Window 1");
   Window w2 = new Window(null);
   w2.setName("Window 2");
   System.out.println("FRAME WINDOWS");
   Frame[] frames = Frame.getFrames();
   for (Frame frame : frames)
     System.out.println(frame.getName() + ": " + frame.getClass());
 }

}</source>





Get Owning Frame for Component

   <source lang="java">

/*

* 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.Frame; import javax.swing.SwingUtilities; /**

* Common GUI utilities accessed via static methods.
* 
* @author 
*/

public class GUIUtils {

 /**
  * Return the owning Frame for the passed component of null
  * if it doesn"t have one.
  * 
  * @throws IllegalArgumentException
  *           If wind is null.
  */
 public static Frame getOwningFrame(Component comp) {
   if (comp == null) {
     throw new IllegalArgumentException("null Component passed");
   }
   if (comp instanceof Frame) {
     return (Frame) comp;
   }
   return getOwningFrame(SwingUtilities.windowForComponent(comp));
 }

}</source>





Set Frame Location Relative To

   <source lang="java">

import javax.swing.*; import java.awt.*; /**

* @author Adrian BER (beradrian@yahoo.ru)
*/

public class UIUtilities {

   public static void setFrameLocationRelativeTo(JFrame f, Component c) {
       if (c == null) {
           // Setting the position of the dialog on the center of the screen
           // in 1.4 should be replaced by

// f.setLocationRelativeTo(null);

           Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
           f.setLocation((int)d.getWidth()/2 - (int)f.getPreferredSize().getWidth()/2,
                   (int)d.getHeight()/2 - (int)f.getPreferredSize().getHeight()/2);
       }
   }

}</source>