package Rong;
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;
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实现调用操作平台桌面系统");
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://www.baidu.com")); // 打开平台默认的浏览器
} catch (URISyntaxException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
}
});
button[1].addActionListener(new ActionListener() { // 打开平台默认的邮件
public void actionPerformed(ActionEvent e) {
try {
desktop.mail(new URI("mailto:[email protected]"));
} 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();
}
}