Java JDesktopPane类(桌面面板类 )和JInternalFrame类(内部窗体类 )

JDesktopPane类简介

用于创建多文档界面或虚拟桌面的容器。用户可创建 JInternalFrame 对象并将其添加到 JDesktopPane。JDesktopPane 扩展了 JLayeredPane,以管理可能的重叠内部窗体。它还维护了对 DesktopManager 实例的引用,这是由 UI 类为当前的外观(L&F)所设置的。注意,JDesktopPane 不支持边界。

此类通常用作JInternalFrames 的父类,为 JInternalFrames 提供一个可插入的 DesktopManager 对象。特定于 L&F 的实现 installUI 负责正确设置 desktopManager 变量。JInternalFrame 的父类是 JDesktopPane 时,它应该将其大部分行为(关闭、调整大小等)委托给 desktopManager。

JDesktopPane构造函数

JDesktopPane():建立JDesktopPane组件

JDesktopPane类常用方法

getAllFrames():以数组的形式返回桌面中当前显示的所有JInternalFrame

getSelectedFrame():获得桌面中当前被选中的JInternalFrame,如果没有被选中的JInternalFrame,则返回null

removeAll():从桌面中移除所有的JInternalFrame

remove(int index):从桌面中移除位于指定索引的JInternalFrame

setSelectedFrame(JInternalFrame f):设置指定的JInternalFrame为当前被选中的窗体

setDragMode(int dragMode):设置窗体的拖动模式,入口参数值为LIVE_DRAG_MODE(在拖动窗体的过程中连续重绘被拖动的窗体)和OUTLINE_DRAG_MODE(在拖动窗体的过程中只连续重绘被拖动窗体的边框,拖动结束后再重绘被拖动的窗体)

JInternalFrame简介

该类是一个轻量级对象,用来创建支持拖动、关闭、图标化、调整大小、最小化、最大化、标题显示和菜单栏的内部窗体,JInternalFrame不能单独出现,必须依附在最上层组件上,该内部窗体需要显示在JDesktopPane类创建的桌面面板中。

JInternalFrame构造函数

JInternalFrame():建立一个不能更改大小、不可关闭、不可最大最小化、也没有标题的Internal Frame。

JInternalFrame(String title):建立一个不能更改大小、不可关闭、不可最大最小化、但具有标题的Internal Frame。

JInternalFrame(String title,boolean resizable):建立一个不可关闭、不可最大最小化、但可变更大小且具有标题的 Internal Frame。

JInternalFrame(String title,boolean resizable,boolean closable):建立一个可关闭、可更改大小、且具有标题,但不可最大化最小化的Internal Frame.

JInternalFrame(String title,boolean resizable,boolean closable,boolean maximizable):建立一个可关闭、可更改大小、 具有标题、可最大化,但不可最小化的Internal Frame.

JInternalFrame(String title,boolean resizable,boolean closable,boolean maximizable,boolean iconifiable):建立一个可关闭、可更改大小、具有标题、可最大化与最小化的Internal Frame.

JInternalFrame常用方法

setResizable(boolean b):设置是否允许自由调整大小

setClosable(boolean b):设置是否提供关闭按钮

setMaximizable(boolean b):设置是否提供“最大化”按钮

setIconifiable(boolean b):设置是否提供“最小化”按钮

setSelected(boolean b):设置窗体是否被激活

setFrameIcon(Icon icon):设置窗体标题栏显示的图标

import java.util.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyVetoException;
import java.net.*;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.tree.*;

public class t3 extends JFrame{
	
	private URL resource = this.getClass().getResource("/back.JPG");//桌面背景
	private ImageIcon icon = new ImageIcon(resource); // 创建背景图片对象
	
	private JPanel panel = new JPanel();
	private JDesktopPane desktopPane = new JDesktopPane();//创建一个桌面面板对象
	private JLabel backLabel = new JLabel();
	private JButton buttonFirst = new JButton("按钮一");
	private JButton buttonSecond = new JButton("按钮二");
	private JButton buttonThird = new JButton("按钮三");
	
	private InternalFrame pInFrame = null;//定义一个按钮一内部窗体对象
	private InternalFrame rInFrame = null;//定义一个按钮二内部窗体对象
	private InternalFrame tInFrame = null;//定义一个按钮三内部窗体对象
	
	public t3() {
		setTitle("选项卡面板");
		setBounds(400, 400, 400, 400);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		backLabel.setIcon(icon); //令标签组件显示背景图片
		backLabel.setBounds(0, 0, icon.getIconWidth(), icon.getIconHeight());//设置标签组件的显示位置及大小
		
		desktopPane.setDragMode(JDesktopPane.LIVE_DRAG_MODE);//设置内部窗体的拖动模式为拖动时连续重绘
		desktopPane.add(backLabel, new Integer(Integer.MIN_VALUE));//将标签组件添加到指定索引位置(最小值)
		
		final FlowLayout flowLayout = new FlowLayout();//设置JPanel面板的布局格式
		flowLayout.setAlignment(FlowLayout.LEFT);
		panel.setLayout(flowLayout);
		
		//添加按钮事件监听器,并将按钮添加到面板上
		buttonFirst.addActionListener(new BAListener(pInFrame, "按钮一"));
		buttonSecond.addActionListener(new BAListener(rInFrame, "按钮二"));
		buttonThird.addActionListener(new BAListener(tInFrame, "按钮三"));
		panel.add(buttonFirst);
		panel.add(buttonSecond);
		panel.add(buttonThird);
		
		add(desktopPane);
		add(panel, BorderLayout.NORTH);
		setVisible(true);
	}

	//内部类事件监听器
	private class BAListener implements ActionListener {
		
		InternalFrame inFrame;
		String title;
		
		//获取窗体对象以及窗体的标题
		public BAListener(InternalFrame inFrame, String title) {//
			this.inFrame = inFrame;
			this.title = title;
		}
		
		//事件处理
		public void actionPerformed(ActionEvent e) {
			
			if (inFrame == null || inFrame.isClosed()) {
				
				JInternalFrame[] allFrames = desktopPane.getAllFrames();//获得桌面面板中的所拥有内部窗体的数量
				
				int titleBarHight = 30 * allFrames.length;//设置窗体标题的宽度
				int x = 10 + titleBarHight;
				int y = x;// 设置窗体的显示位置
				int width = 250;
				int height = 180;// 设置窗体的大小
				
				inFrame = new InternalFrame(title);// 创建指定标题的内部窗体
				inFrame.setBounds(x, y, width, height);// 设置窗体的显示位置及大小
				inFrame.setVisible(true);// 设置窗体可见
				desktopPane.add(inFrame);// 将窗体添加到桌面面板中
			}
			
			try {
				
				inFrame.setSelected(true);//设置窗体被激活(选中窗体)
			
			} catch (PropertyVetoException propertyVetoE) {
				propertyVetoE.printStackTrace();
			}
			
		}
	}
	
	//对内部窗体的处理
	private class InternalFrame extends JInternalFrame {

		public InternalFrame(String title) {
			
			setTitle(title);// 设置内部窗体的标题
			setResizable(true);// 设置允许自由调整大小
			setClosable(true);// 设置提供关闭按钮
			setIconifiable(true);// 设置提供图标化按钮
			setMaximizable(true);// 设置提供最大化按钮
			
			URL resource = this.getClass().getResource("/titleImagine.png");//获得图片的路径
			ImageIcon icon = new ImageIcon(resource);//创建图片对象
			
			setFrameIcon(icon); //设置窗体图标
		}
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		t3 test = new t3();

	}

}

猜你喜欢

转载自blog.csdn.net/qq_36761831/article/details/81480688