用户操作
[即时聊天] [发私信] [加为好友]
米强(五斗米)ID:mq612
49140次访问,排名2225好友70人,关注者76
树欲静而风不止
mq612的文章
原创 28 篇
翻译 0 篇
转载 0 篇
评论 97 篇
米强(五斗米)的公告
米强(五斗米)在本Blog中发表的文章全部为原创作品,欢迎朋友们批评指正,如果觉得五斗米写的东西还有点用,请给点掌声,感激! 欢迎转载,请注明原作者-米强(五斗米)
最近评论
辗转天涯:我想实现一个表头提供下拉列表选择的功能,有什么思路吗?网上查了一天了,没头绪。
huangbinhua:急人所需,谢谢了
huangbinhua:急人所需,谢谢了
k394646140:能看得出来,米老师对java语言的高深理解,
虽然我不是您带的学生,偶尔听过您带的课程
很厉害功夫学到家了,也看得出来米老师对java
职业的爱好。
local:米老师 能总结些servlet与oracle 的数据操作和乱码处理吗
文章分类
收藏
    相册
    高级19班游兴庆公园
    小小米
    存档
    软件项目交易
    订阅我的博客
    XML聚合  FeedSky
    订阅到鲜果
    订阅到Google
    订阅到抓虾
    订阅到BlogLines
    订阅到Yahoo
    订阅到GouGou
    订阅到飞鸽
    订阅到Rojo
    订阅到newsgator
    订阅到netvibes

    原创 Java1.6.0实现调用操作平台桌面系统收藏

    新一篇: Java中CardLayout卡片布局管理器使用的小例子 | 旧一篇: Java1.6.0实现GUI系统托盘技术演示代码(附详细注释)

    java.awt.Desktop类可获得操作平台的桌面系统,以便使用系统默认浏览器、编辑器、邮件、打印等。

    import java.awt.Desktop;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.File;
    import java.io.IOException;
    import java.net.URI;
    import java.net.URISyntaxException;
    import javax.swing.JButton;
    import javax.swing.JFileChooser;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import javax.swing.UIManager;
    /**
     * Java1.6.0实现调用操作平台桌面系统
     * Desktop类将获得操作平台的桌面系统,以便使用系统默认浏览器、编辑器、邮件、打印等
     * 一堆按钮摆在一起不大好看,懒的布局了,大家能看明白就成,打开文件、编辑文件和打印文件需要先按“浏览”按钮,选择一个文件后才行。
     *
     * @author 五斗米 <如转载请保留作者和出处>
     * @blog http://blog.csdn.net/mq612
     */
    public class DesktopDemo extends JFrame {
        private JPanel pane = null;
        private JLabel label = null; // 显示信息的标签
        private JButton [] button = null; // 启动平台默认程序的按钮
        private Desktop desktop = null; // 本操作平台的桌面系统实例
        private JTextField text = null; // 显示文件地址的TextField
        private JButton b = null; // 浏览文件的按钮
        private JFileChooser fc = null; // 需要浏览文件
        private File file = null; // 文件
       
        public DesktopDemo() {
            super("Java1.6.0实现调用操作平台桌面系统");
            try {
                // 将LookAndFeel设置成Windows样式
                UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            fc = new JFileChooser();
            pane = new JPanel();
            label = new JLabel("本操作平台不支持桌面系统"); // 默认标签文字为不支持
            pane.add(label);
            button = new JButton[5];
            button[0] = new JButton("默认浏览器");
            button[1] = new JButton("默认邮件");
            button[2] = new JButton("默认程序打开文件");
            button[3] = new JButton("默认程序编辑文件");
            button[4] = new JButton("打印文件");
            for(int i = 0; i < button.length; i++){ // 使按钮暂不可用
                button[i].setEnabled(false);
            }
            pane.add(button[0]);
            pane.add(button[1]);
            text = new JTextField(30);
            text.setEditable(false); // 不可编辑
            b = new JButton("浏览"); // 使按钮暂不可用
            b.setEnabled(false);
            pane.add(text);
            pane.add(b);
            pane.add(button[2]);
            pane.add(button[3]);
            pane.add(button[4]);
            desktop = Desktop.getDesktop(); // 返回本操作平台的桌面系统
            if(desktop.isDesktopSupported()){ // 如果该操作平台支持桌面系统
                this.desktop();
            }
            this.getContentPane().add(pane);
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.setSize(400, 200);
            this.setVisible(true);
        }
        /**
         * 桌面系统
         */
        private void desktop(){
            label.setText("本操作平台支持桌面系统");
            for(int i = 0; i < button.length; i++){ // 使按钮可用
                button[i].setEnabled(true);
            }
            b.setEnabled(true);
            b.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    openFile();
                }
            });
            button[0].addActionListener(new ActionListener() { // 打开平台默认的浏览器
                public void actionPerformed(ActionEvent e) {
                    try {
                        desktop.browse(new URI("http://blog.csdn.net/mq612")); // 打开平台默认的浏览器
                    } catch (URISyntaxException ex) {
                        ex.printStackTrace();
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }
                }
            });
            button[1].addActionListener(new ActionListener() { // 打开平台默认的邮件
                public void actionPerformed(ActionEvent e) {
                    try {
                        /*
                         * 打开平台默认的邮件,有两个方法
                         * mail() // 单独打开默认的邮件
                         * mail(URI mailtoURI) // 带接收者地址的mail方法
                         */
                        desktop.mail(new URI("mailto:mq612@163.com"));
                    } catch (URISyntaxException ex) {
                        ex.printStackTrace();
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }
                }
            });
            button[2].addActionListener(new ActionListener() { // 使用平台默认程序打开文件
                public void actionPerformed(ActionEvent e) {
                    try {
                        desktop.open(file);
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }
                }
            });
            button[3].addActionListener(new ActionListener() { // 使用平台默认程序编辑文件
                public void actionPerformed(ActionEvent e) {
                    try {
                        desktop.edit(file);
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }
                }
            });
            button[4].addActionListener(new ActionListener() { // 使用平台默认打印程序打印文件,此操作会先用默认的程序打开相应文件后再打印。
                public void actionPerformed(ActionEvent e) {
                    try {
                        desktop.print(file);
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }
                }
            });
        }
       
        /**
         * 浏览本地文件
         */
        private void openFile(){
            fc.showOpenDialog(this);
            file = fc.getSelectedFile();
            text.setText(file.toString());
        }
       
        public static void main(String[] args) {
            new DesktopDemo();
        }
       
    }

    发表于 @ 2007年01月10日 12:28:00|评论(loading...)|编辑

    新一篇: Java中CardLayout卡片布局管理器使用的小例子 | 旧一篇: Java1.6.0实现GUI系统托盘技术演示代码(附详细注释)

    评论

    #zhaichao621 发表于2008-02-29 02:04:50  IP: 61.185.201.*
    我谨我自己支持并转载!
    #a_nuo 发表于2008-07-04 10:43:25  IP: 211.94.138.*
    太好了
    顶一下
    发表评论  


    登录
    Csdn Blog version 3.1a
    Copyright © 米强(五斗米)