Java Tutorial/Swing/JFrame States

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

Deiconifies a frame; the maximized bits are not affected.

import java.awt.Frame;
public class Main {
  public static void main(String[] argv) throws Exception {
    Frame frame = new Frame();
    frame.setSize(300, 300);
    frame.setVisible(true);
    deiconify(frame);
  }
  public static void deiconify(Frame frame) {
    int state = frame.getExtendedState();
    // Clear the iconified bit
    state &= ~Frame.ICONIFIED;
    // Deiconify the frame
    frame.setExtendedState(state);
  }
}





Determining When a Component Has Been Made Visible, Moved, or Resized

import java.awt.ruponent;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.ruponentAdapter;
import java.awt.event.ruponentEvent;
import java.awt.event.ruponentListener;
import javax.swing.JFrame;
public class Main {
  public static void main() {
    ComponentListener listener = new ComponentAdapter() {
      public void componentShown(ComponentEvent evt) {
        Component c = (Component) evt.getSource();
        System.out.println("Component is now visible");
      }
      public void componentHidden(ComponentEvent evt) {
        Component c = (Component) evt.getSource();
        System.out.println("Component is now hidden");
      }
      public void componentMoved(ComponentEvent evt) {
        Component c = (Component) evt.getSource();
        Point newLoc = c.getLocation();
        System.out.println("Get new location");
      }
      public void componentResized(ComponentEvent evt) {
        Component c = (Component) evt.getSource();
        Dimension newSize = c.getSize();
        System.out.println("Get new size");
      }
    };
    JFrame frame = new JFrame();
    frame.setSize(300, 300);
    frame.addComponentListener(listener);
    frame.setVisible(true);
  }
}





Determining When a Frame or Window Is Iconized or Maximized

import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowStateListener;
public class Main {
  public static void main() {
    Frame frame = new Frame();
    WindowStateListener listener = new WindowAdapter() {
      public void windowStateChanged(WindowEvent evt) {
        int oldState = evt.getOldState();
        int newState = evt.getNewState();
        if ((oldState & Frame.ICONIFIED) == 0 && (newState & Frame.ICONIFIED) != 0) {
          System.out.println("Frame was iconized");
        } else if ((oldState & Frame.ICONIFIED) != 0 && (newState & Frame.ICONIFIED) == 0) {
          System.out.println("Frame was deiconized");
        }
        if ((oldState & Frame.MAXIMIZED_BOTH) == 0 && (newState & Frame.MAXIMIZED_BOTH) != 0) {
          System.out.println("Frame was maximized");
        } else if ((oldState & Frame.MAXIMIZED_BOTH) != 0 && (newState & Frame.MAXIMIZED_BOTH) == 0) {
          System.out.println("Frame was minimized");
        }
      }
    };
    frame.addWindowStateListener(listener);
    frame.setVisible(true);
  }
}





Determining When a Frame or Window Is Opened or Closed

import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
public class Main {
  public static void main() {
    Frame frame = new Frame();
    WindowListener listener = new WindowAdapter() {
      public void windowOpened(WindowEvent evt) {
        Frame frame = (Frame) evt.getSource();
        System.out.println(frame.getTitle());
      }
      public void windowClosing(WindowEvent evt) {
        Frame frame = (Frame) evt.getSource();
        System.out.println(frame.getTitle());
      }
      public void windowClosed(WindowEvent evt) {
        Frame frame = (Frame) evt.getSource();
        System.out.println(frame.getTitle());
      }
    };
    frame.addWindowListener(listener);
    frame.setVisible(true);
  }
}





Iconifies a frame; the maximized bits are not affected.

import java.awt.Frame;
public class Main {
  public static void main() {
    Frame frame = new Frame();
    frame.setSize(300, 300);
    frame.setVisible(true);
    iconify(frame);
  }
  public static void iconify(Frame frame) {
    int state = frame.getExtendedState();
    // Set the iconified bit
    state |= Frame.ICONIFIED;
    // Iconify the frame
    frame.setExtendedState(state);
  }
}





Maximizes a frame; the iconified bit is not affected

import java.awt.Frame;
public class Main {
  public static void main(String[] argv) throws Exception {
    Frame frame = new Frame();
    frame.setSize(300, 300);
    frame.setVisible(true);
    minimize(frame);
  }
  public static void minimize(Frame frame) {
    int state = frame.getExtendedState();
    // Clear the maximized bits
    state &= ~Frame.MAXIMIZED_BOTH;
    // Maximize the frame
    frame.setExtendedState(state);
  }
}





Setting the Bounds for a Maximized Frame

import java.awt.Frame;
import java.awt.Rectangle;
public class Main {
  public static void main() {
    Frame frame = new Frame();
    Rectangle bounds = new Rectangle(20, 20, 200, 200);
    frame.setMaximizedBounds(bounds);
    frame.setVisible(true);
  }
}