001: /*
002: * @(#)FrameDemo.java 1.5 06/10/10
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026: package basis.demos;
027:
028: import java.awt.*;
029: import java.awt.event.*;
030: import java.util.EventObject;
031: import basis.Builder;
032: import basis.DemoButton;
033: import basis.DemoButtonListener;
034:
035: public class FrameDemo extends Demo {
036: static int count;
037: Frame frame;
038:
039: public FrameDemo() {
040: count++;
041: setLayout(new GridLayout(3, 2));
042: DemoButton newFrameButton = new DemoButton("New");
043: DemoButton titleButton = new DemoButton("Title");
044: DemoButton sizeButton = new DemoButton("Size");
045: DemoButton resizableButton = new DemoButton("Resizable");
046: DemoButton locationButton = new DemoButton("Location");
047: DemoButton stateButton = new DemoButton("State");
048: newFrameButton.addDemoButtonListener(new DemoButtonListener() {
049: public void buttonPressed(EventObject e) {
050: final Frame frame = new Frame();
051: frame.add(new FrameDemo());
052: frame.setTitle("" + count);
053: frame.setSize(160, 120);
054: frame.addWindowListener(new WindowAdapter() {
055: public void windowClosing(WindowEvent e) {
056: frame.setVisible(false);
057: frame.dispose();
058: }
059: });
060: Toolkit toolkit = frame.getToolkit();
061: Dimension d = toolkit.getScreenSize();
062: frame.setLocation(
063: (int) (Math.random() * 2 * d.width / 3),
064: (int) (Math.random() * 2 * d.height / 3));
065: frame.setVisible(true);
066: }
067: });
068: titleButton.addDemoButtonListener(new DemoButtonListener() {
069: public void buttonPressed(EventObject e) {
070: Frame frame = FrameDemo.this .getFrame();
071: frame.setTitle(frame.getTitle() + "*");
072: }
073: });
074: sizeButton.addDemoButtonListener(new DemoButtonListener() {
075: public void buttonPressed(EventObject e) {
076: Frame frame = FrameDemo.this .getFrame();
077: if (!frame.isResizable()) {
078: return;
079: }
080: Toolkit toolkit = getToolkit();
081: Dimension d = toolkit.getScreenSize();
082: frame.setSize((int) (Math.random() * d.width / 2) + 60,
083: (int) (Math.random() * d.height / 2) + 60);
084: frame.validate();
085: }
086: });
087: resizableButton.addDemoButtonListener(new DemoButtonListener() {
088: public void buttonPressed(EventObject e) {
089: Frame frame = FrameDemo.this .getFrame();
090: if (frame.isResizable()) {
091: frame.setResizable(false);
092: DemoButton b = (DemoButton) e.getSource();
093: b.setForeground(Builder.SUN_RED);
094: } else {
095: frame.setResizable(true);
096: DemoButton b = (DemoButton) e.getSource();
097: b.setForeground(DemoButton.DEFAULT_COLOR);
098: }
099: }
100: });
101: locationButton.addDemoButtonListener(new DemoButtonListener() {
102: public void buttonPressed(EventObject e) {
103: Frame frame = FrameDemo.this .getFrame();
104: Toolkit toolkit = frame.getToolkit();
105: Dimension d = toolkit.getScreenSize();
106: frame.setLocation(
107: (int) (Math.random() * 2 * d.width / 3),
108: (int) (Math.random() * 2 * d.height / 3));
109: }
110: });
111: stateButton.addDemoButtonListener(new DemoButtonListener() {
112: public void buttonPressed(EventObject e) {
113: Frame frame = FrameDemo.this .getFrame();
114: frame
115: .setState(frame.getState() == Frame.NORMAL ? Frame.ICONIFIED
116: : Frame.NORMAL);
117: }
118: });
119: add(newFrameButton);
120: add(titleButton);
121: add(sizeButton);
122: add(resizableButton);
123: add(locationButton);
124: add(stateButton);
125: }
126:
127: Frame getFrame() {
128: if (frame == null) {
129: Container parent = getParent();
130: while (parent.getParent() != null) {
131: parent = parent.getParent();
132: }
133: frame = (Frame) parent;
134: }
135: return frame;
136: }
137: }
|