java中 桌面面板和内部窗体的使用 JDesktopPane类和 JInternalFrame类

下面demo展示了桌面面板和内部窗体的使用方法,再src路径下放1.jpg和2.jpj文件就可以,下面为代码演示:

package com.test;
import java.awt.*;
import java.awt.event.*;
import java.beans.*;
import java.net.*;

import javax.swing.*;

public class Desktop JFrame {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    JDesktopPane desktopPane = null;// 定义一个桌面面板对象
    InternalFrame pInFrame = null;// 定义一个人事管理内部窗体对象
    InternalFrame rInFrame = null;// 定义一个账套管理内部窗体对象
    InternalFrame tInFrame = null;// 定义一个待遇报表内部窗体对象
    public static void main(String args[]) {
       Desktop frame = new Desktop();
        frame.setVisible(true);
    }
    public Desktop() {
        super();
        setTitle("公司人事管理系统");
        setBounds(100, 100, 570, 470);
        //使用 System exit 方法退出应用程序。仅在应用程序中使用。
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // 创建桌面面板
        desktopPane = new JDesktopPane();// 创建桌面面板对象
        // 设置内部窗体的拖动模式
        desktopPane.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);
        getContentPane().add(desktopPane, BorderLayout.CENTER);
        // 为桌面面板添加背景图片
        final JLabel backLabel = new JLabel(); // 创建一个标签组件对象
        // 获得背景图片的路径
        URL resource = this.getClass().getResource("/1.jpg");
        ImageIcon icon = new ImageIcon(resource); // 创建背景图片对象
        backLabel.setIcon(icon); // 令标签组件显示背景图片
        // 设置组件的显示位置及大小
        backLabel.setBounds(0,0, icon.getIconWidth(), icon.getIconHeight());
        // 将标签组件添加到指定索引位置
        desktopPane.add(backLabel, new Integer(Integer.MIN_VALUE));
    
        // 添加按钮
        final JPanel panel = new JPanel();
        //具有指定的对齐方式以及指定的水平和垂直间隙
        final FlowLayout flowLayout = new FlowLayout();
        flowLayout.setAlignment(FlowLayout.LEFT);//左对齐
        panel.setLayout(flowLayout);
        getContentPane().add(panel, BorderLayout.NORTH);
        
        final JButton personnelButton = new JButton();
        personnelButton.setText("行政管理");
        personnelButton
                .addActionListener(new BAListener(pInFrame, "行政管理"));
        panel.add(personnelButton);
        
        final JButton reckoningButton = new JButton();
        reckoningButton.setText("财务管理");
        reckoningButton
                .addActionListener(new BAListener(rInFrame, "财务管理"));
        panel.add(reckoningButton);
        
        final JButton treatmentButton = new JButton();
        treatmentButton.setText("工资管理");
        treatmentButton
                .addActionListener(new BAListener(tInFrame, "工资"));
        panel.add(treatmentButton);
    }
    
    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, y = x;// 设置窗体的显示位置
                int width = 250, 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 {
        /**
         * 
         */
        private static final long serialVersionUID = 1L;

        public InternalFrame(String title) {
            super();
            setTitle(title);// 设置内部窗体的标题
            setResizable(true);// 设置允许自由调整大小
            setClosable(true);// 设置提供关闭按钮
            setIconifiable(true);// 设置提供图标化按钮
            setMaximizable(true);// 设置提供最大化按钮
            // 获得图片的路径
            URL resource = this.getClass().getResource("/2.jpg"); 
            ImageIcon icon = new ImageIcon(resource); // 创建图片对象
            setFrameIcon(icon); // 设置窗体图标
        }
    }
}

测试结果:

  • 4
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值