用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - java代码库

用JInternalFrame实现内部窗口 内部框架 子窗口

2013-01-25 作者: 小蜜锋举报

[java]代码库

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.beans.PropertyVetoException;

import javax.swing.JButton;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.plaf.basic.BasicInternalFrameUI;

/**
 * MDIFrame is a frame using JInternalFrame to implements MDI as Word on
 * Windows.
 * 
 * @author Cheng
 * @version 1.0.0 for GPF MDI test
 */
@SuppressWarnings("serial")
public class MDIFrame extends JFrame {

	/** The desktop pane. */
	final JDesktopPane desktopPane = new JDesktopPane();

	/** The operation pane with restore and close buttons. */
	final JPanel opPane = new JPanel();

	/**
	 * Instantiates a new mDI frame.
	 */
	public MDIFrame() {
		setTitle("MDI Frame");
		setSize(600, 550);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		final JMenuBar bar = new JMenuBar();
		JMenuItem exit = new JMenuItem("Exit");
		exit.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}

		});
		bar.add(exit);
		// operation pane with two button, set invisible at first
		opPane.setLayout(new FlowLayout(FlowLayout.TRAILING));
		JButton restore = new JButton("#");
		restore.setMargin(new Insets(0, 0, 0, 0));
		restore.setPreferredSize(new Dimension(15, 15));
		restore.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				InnerFrame i = (InnerFrame) desktopPane.getSelectedFrame();
				try {
					// notice this method, when JInternalFrame set maximun false
					// this internal frame will be set as old size
					i.setMaximum(false);
				} catch (PropertyVetoException ex) {
					ex.printStackTrace();
				}
				i.showNorthPanel();
				opPane.setVisible(false);
			}

		});
		opPane.add(restore);
		JButton close = new JButton("X");
		close.setMargin(new Insets(0, 0, 0, 0));
		close.setPreferredSize(new Dimension(15, 15));
		close.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				JInternalFrame i = desktopPane.getSelectedFrame();
				i.dispose();
				opPane.setVisible(false);
			}

		});
		opPane.add(close);
		bar.add(opPane);
		opPane.setVisible(false);
		setJMenuBar(bar);

		Container content = getContentPane();
		content.add(desktopPane, BorderLayout.CENTER);
		final JPanel ctrlPane = new JPanel();
		JButton add = new JButton("add");
		add.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				InnerFrame iFrame = new InnerFrame();
				iFrame.setVisible(true);
				desktopPane.add(iFrame);
			}

		});
		ctrlPane.add(add);
		content.add(ctrlPane, BorderLayout.SOUTH);
		setVisible(true);
	}

	/**
	 * The Class InnerFrame.
	 */
	class InnerFrame extends JInternalFrame {

		/** The is hidden. */
		boolean isHidden = false;

		/** The old ui. */
		BasicInternalFrameUI oldUi = null;

		/**
		 * Instantiates a new inner frame.
		 */
		public InnerFrame() {
			oldUi = (BasicInternalFrameUI) getUI();
			setSize(200, 200);
			maximizable = true;
			closable = true;
			addComponentListener(new ComponentAdapter() {
				public void componentResized(ComponentEvent e) {
					InnerFrame selectedFrame = (InnerFrame) e.getSource();
					if (selectedFrame.isMaximum()) {
						selectedFrame.hideNorthPanel();
						opPane.setVisible(true);
						try {
							selectedFrame.setMaximum(true);
						} catch (PropertyVetoException ex) {
							ex.printStackTrace();
						}
					}
				}
			});
		}

		/**
		 * Hide north panel.
		 */
		public void hideNorthPanel() {
			((BasicInternalFrameUI) this.getUI()).setNorthPane(null);
			this.putClientProperty("JInternalFrame.isPalette", Boolean.TRUE);
			isHidden = true;
		}

		/**
		 * Show north panel.
		 */
		public void showNorthPanel() {
			this.setUI(oldUi);
			this.putClientProperty("JInternalFrame.isPalette", Boolean.FALSE);
			isHidden = false;
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see javax.swing.JInternalFrame#updateUI()
		 */
		public void updateUI() {
			super.updateUI();
			if (isHidden) {
				hideNorthPanel();
			}
		}
	}

	/**
	 * The main method.
	 * 
	 * @param args
	 *            the arguments
	 */
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {

			@Override
			public void run() {
				new MDIFrame();
			}

		});
	}
}

[代码运行效果截图]


用JInternalFrame实现内部窗口 内部框架 子窗口


网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...