|
package com.pcx.test;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
?
public class TestInternalFrame {
????final int DESKTOP_WIDTH = 480;
????final int DESKTOP_HEIGHT = 360;
????final int FRAME_DISTANCE = 30;
?
????JFrame jf = new JFrame("父視窗");
????// 定義一個虛擬桌面
????private JDesktopPane desktop = new JDesktopPane();
????// 儲存下一個內部視窗的座標點
????private int nextFrameX;
????private int nextFrameY;
????// 定義內部視窗為虛擬桌面的1/2大小
????private int width = DESKTOP_WIDTH / 2;
????private int height = DESKTOP_HEIGHT / 2;
????// 為主視窗定義2個菜單
????JMenu fileMenu = new JMenu("檔案");
????// 定義newAction用於建立菜單和工具按鈕
????Action newAction = new AbstractAction("建立", new ImageIcon("ico/new.png")) {
????????public void actionPerformed(ActionEvent event) {
????????????// 建立內部視窗
????????????final JInternalFrame iframe = new JInternalFrame("新文檔", true, // 可改變大小
????????????????????true, // 可關閉
????????????????????true, // 可最大化
????????????????????true); // 可最小化
????????????iframe.add(new JScrollPane(new JTextArea(8, 40)));
????????????// 將內部視窗添加到虛擬桌面中
????????????desktop.add(iframe);
????????????// 設定內部視窗的原始位置(內部視窗預設大小是0X0,放在0,0位置)
????????????iframe.reshape(nextFrameX, nextFrameY, width, height);
????????????// 使該視窗可見,並嘗試選中它
????????????iframe.show();
????????????// 計算下一個內部視窗的位置
????????????nextFrameX += FRAME_DISTANCE;
????????????nextFrameY += FRAME_DISTANCE;
????????????if (nextFrameX + width > desktop.getWidth())
????????????????nextFrameX = 0;
????????????if (nextFrameY + height > desktop.getHeight())
????????????????nextFrameY = 0;
????????}
????};
????// 定義exitAction用於建立菜單和工具按鈕
????Action exitAction = new AbstractAction("退出", new ImageIcon("ico/exit.png")) {
????????public void actionPerformed(ActionEvent event) {
????????????System.exit(0);
????????}
????};
?
????public void init() {
????????// 為視窗安裝菜單條和工具條
????????JMenuBar menuBar = new JMenuBar();
????????JToolBar toolBar = new JToolBar();
????????jf.setJMenuBar(menuBar);
????????menuBar.add(fileMenu);
????????fileMenu.add(newAction);
????????fileMenu.add(exitAction);
????????toolBar.add(newAction);
????????toolBar.add(exitAction);
????????desktop.setPreferredSize(new Dimension(480, 360));
????????// 將虛擬桌面添加到頂級JFrame容器中
????????jf.add(desktop);
????????jf.add(toolBar, BorderLayout.NORTH);
????????jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
????????jf.pack();
????????jf.setVisible(true);
????}
?
????public static void main(String[] args) {
????????new TestInternalFrame().init();
????}
} |