中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

Java6實(shí)現(xiàn)調(diào)用操作平臺(tái)桌面系統(tǒng)

2018-07-20    來源:open-open

容器云強(qiáng)勢(shì)上線!快速搭建集群,上萬Linux鏡像隨意使用
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實(shí)現(xiàn)調(diào)用操作平臺(tái)桌面系統(tǒng)
 * Desktop類將獲得操作平臺(tái)的桌面系統(tǒng),以便使用系統(tǒng)默認(rèn)瀏覽器、編輯器、郵件、打印等
 * 一堆按鈕擺在一起不大好看,懶的布局了,大家能看明白就成,打開文件、編輯文件和打印文件需要先按“瀏覽”按鈕,選擇一個(gè)文件后才行。
 *
 * @author 五斗米 <如轉(zhuǎn)載請(qǐng)保留作者和出處>
 * @blog <a href="http://blog.csdn.net/mq612">http://blog.csdn.net/mq612
 */
public class DesktopDemo extends JFrame {
    private JPanel pane = null;
    private JLabel label = null; // 顯示信息的標(biāo)簽
    private JButton [] button = null; // 啟動(dòng)平臺(tái)默認(rèn)程序的按鈕
    private Desktop desktop = null; // 本操作平臺(tái)的桌面系統(tǒng)實(shí)例
    private JTextField text = null; // 顯示文件地址的TextField
    private JButton b = null; // 瀏覽文件的按鈕
    private JFileChooser fc = null; // 需要瀏覽文件
    private File file = null; // 文件

    public DesktopDemo() {
        super("Java1.6.0實(shí)現(xiàn)調(diào)用操作平臺(tái)桌面系統(tǒng)");
        try {
            // 將LookAndFeel設(shè)置成Windows樣式
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        fc = new JFileChooser();
        pane = new JPanel();
        label = new JLabel("本操作平臺(tái)不支持桌面系統(tǒng)"); // 默認(rèn)標(biāo)簽文字為不支持
        pane.add(label);
        button = new JButton[5];
        button[0] = new JButton("默認(rèn)瀏覽器");
        button[1] = new JButton("默認(rèn)郵件");
        button[2] = new JButton("默認(rèn)程序打開文件");
        button[3] = new JButton("默認(rèn)程序編輯文件");
        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(); // 返回本操作平臺(tái)的桌面系統(tǒng)
        if(desktop.isDesktopSupported()){ // 如果該操作平臺(tái)支持桌面系統(tǒng)
            this.desktop();
        }
        this.getContentPane().add(pane);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(400, 200);
        this.setVisible(true);
    }
    /**
     * 桌面系統(tǒng)
     */
    private void desktop(){
        label.setText("本操作平臺(tái)支持桌面系統(tǒng)");
        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() { // 打開平臺(tái)默認(rèn)的瀏覽器
            public void actionPerformed(ActionEvent e) {
                try {
                    desktop.browse(new URI("<a >http://blog.csdn.net/mq612")); // 打開平臺(tái)默認(rèn)的瀏覽器
                } catch (URISyntaxException ex) {
                    ex.printStackTrace();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        });
        button[1].addActionListener(new ActionListener() { // 打開平臺(tái)默認(rèn)的郵件
            public void actionPerformed(ActionEvent e) {
                try {
                    /**
                     * 打開平臺(tái)默認(rèn)的郵件,有兩個(gè)方法
                     * mail() // 單獨(dú)打開默認(rèn)的郵件
                     * 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() { // 使用平臺(tái)默認(rèn)程序打開文件
            public void actionPerformed(ActionEvent e) {
                try {
                    desktop.open(file);
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        });
        button[3].addActionListener(new ActionListener() { // 使用平臺(tái)默認(rèn)程序編輯文件
            public void actionPerformed(ActionEvent e) {
                try {
                    desktop.edit(file);
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        });
        button[4].addActionListener(new ActionListener() { 
        // 使用平臺(tái)默認(rèn)打印程序打印文件,此操作會(huì)先用默認(rèn)的程序打開相應(yīng)文件后再打印。
            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();
    }

}

Java1.6.0實(shí)現(xiàn)調(diào)用操作平臺(tái)桌面系統(tǒng) * Desktop類將獲得操作平臺(tái)的桌面系統(tǒng),以便使用系統(tǒng)默認(rèn)瀏覽器、編輯器、郵件、打印等 * 一堆按鈕擺在一起不大好看,懶的布局了,大家能看明白就成,打開文件、編輯文件和打印文件需要先按“瀏覽”按鈕

標(biāo)簽: seo

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請(qǐng)聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請(qǐng)與原作者聯(lián)系。

上一篇:利用htmlunit下載網(wǎng)頁上的文件

下一篇:HtmlUnit示例代碼