用户注册



邮箱:

密码:

用户登录


邮箱:

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

发表随想


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

点击一个按钮,在JDesktopPane中弹出JInternalFrame

2017-04-24 作者: 陆痴举报

[java]代码库

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyVetoException;
import java.net.URL;
 
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JPanel;
 
/**
 * 桌面面板和内部窗体
 * @author Administrator
 *
 */
public class JDeskPane_and_JInternalFrame extends JFrame{
    JDesktopPane desktopPane = null;
     
    //InternalFrame类----是自己定义的一个类
    InternalFrame aInFrame = null;
    InternalFrame bInFrame = null;
    InternalFrame cInFrame = null;
     
     
    public static void main(String[] args) {
        //再次显示JFrame窗体------能起到刷新的效果
        new JDeskPane_and_JInternalFrame().setVisible(true);
    }
 
    /**
     * 构造方法
     */
    public JDeskPane_and_JInternalFrame(){
        this.setTitle("JDeskPane_and_JInternalFrame");
        //显示JFrame窗体
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(800, 600);
        //设置JFrame窗体在电脑显示屏居中
        this.setLocationRelativeTo(null);
         
        //获取图片的路径
        URL resource = this.getClass().getResource("aa.jpg");
        //获取图片
        Image image = Toolkit.getDefaultToolkit().getImage(resource);
        //设置JFrame窗体的----图标
        this.setIconImage(image);
         
        desktopPane = new JDesktopPane();
        //设置JDesktopPane的背景颜色
        desktopPane.setBackground(Color.lightGray);
        this.getContentPane().add(desktopPane, BorderLayout.CENTER);
         
         
        final JPanel panel = new JPanel();
        //设置JPanel的背景颜色
        panel.setBackground(Color.gray);
        this.getContentPane().add(panel, BorderLayout.NORTH);
        JButton aButton = new JButton("AAA--Button");
        aButton.addActionListener(new ButtonListener(aInFrame, "aButton---实例化的AAA"));
        panel.add(aButton);
         
        JButton bButton = new JButton("BBB--Button");
        bButton.addActionListener(new ButtonListener(bInFrame, "bButton---实例化的BBB"));
        panel.add(bButton);
         
        JButton cButton = new JButton("CCC--Button");
        cButton.addActionListener(new ButtonListener(cInFrame, "cButton--实例化的CCC"));
        panel.add(cButton);
    }
     
     
    /**
     * 设置一个按钮事件的监听类
     *
     * @author Administrator
     *
     */
    private class ButtonListener implements ActionListener{
        InternalFrame inFrame;
        String title;
         
        /**
         *构造方法
         */
        public ButtonListener(InternalFrame inFrame, String title) {
            this.inFrame = inFrame;
            this.title = title;
        }
         
        /**
         * 鼠标的事件监听
         */
        @Override
        public void actionPerformed(ActionEvent e) {
            /**
             * 只有窗体没有实例化,或者窗体实例化后关闭了。
             * 才能打开窗体
             */
            if(inFrame == null || inFrame.isClosed()){
                //获取桌面面板中所有-----实例化的内部窗体
                JInternalFrame[] allFrames = desktopPane.getAllFrames();
                                /*根据实例化的JInternalFrame,设置连个窗体的间隔,
                                    使实例化后的窗体不叠在一起。*/
                int Hight = 30 * allFrames.length;
                int x = 30 + Hight;
                int y = x;
                int width = 400;
                int height = 300;
                 
                inFrame = new InternalFrame(title);
                //设置inFrame实例化后的内部窗体的位置、大小
                inFrame.setBounds(x, y, width, height);
                //设置inFrame内部窗体显示
                inFrame.setVisible(true);
                //将inFrame添加到JDesktopPane中
                desktopPane.add(inFrame);  
            }
             
            //设置触发按钮事件的窗体被选中。
            //那个按钮实例化的JInternalFrame,点击后这是内部窗体被选中
            try {
                inFrame.setSelected(true);
            } catch (PropertyVetoException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }
         
    }
     
     
    /**
     * 内部窗体类------JInternalFrame
     * @author Administrator
     *
     */
    private class InternalFrame extends JInternalFrame{
        public InternalFrame(String title){
            //设置内部窗体的标题
            this.setTitle(title);
            //设置内部窗体---是否可以改变大小
            this.setResizable(true);
            //设置内部窗体---是否可以关闭
            this.setClosable(true);
            //设置内部窗体---是否最大化
            this.setMaximizable(true);
            //设置内部窗体---是否可以最小化
            this.setIconifiable(true);
             
            URL resource = this.getClass().getResource("aa.jpg");
            ImageIcon imageIcon = new ImageIcon(resource);
            //设置窗体的图标
            this.setFrameIcon(imageIcon);
        }
    }
     
}

[代码运行效果截图]


点击一个按钮,在JDesktopPane中弹出JInternalFrame


网友评论    (发表评论)

共2 条评论 1/1页

发表评论:

评论须知:

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


扫码下载

加载中,请稍后...

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

加载中,请稍后...