Java AWT |桌面类
Desktop 类是Java AWT 包的一部分。此类用于启动在本机桌面上注册的关联应用程序以处理 URI 或文件。
关于桌面类的要点:
- 它可以打开显示特定 URI 的默认 Web 浏览器
- 它可以使用可选的 mailto URI 启动默认邮件客户端
- 它可以启动已注册的应用程序来打开、编辑或打印特定文件。
桌面类中的不同方法
Method | Explanation |
---|---|
browse(URI u) | Launches the default browser to display a specific URI. |
edit(File f) | Launches the associated editor application and opens a file. |
getDesktop() | Returns the Desktop instance of the current browser context. |
isDesktopSupported() | returns whether this class is supported on the current platform. |
isSupported(Desktop.Action action) | returns whether an action is supported on the current platform. |
mail() | Launches the mail composing window of the user default mail client. |
mail(URI mailtoURI) | Launches the mail composing window of the user default mail client, filling the message fields specified by a mailto: URI. |
open(File f) | Launches the associated application to open the file |
print(File f) | Prints a file with the native desktop printing facility, using the associated application’s print command. |
下面的程序说明了Java AWT 中的 Desktop 类:
- 启动浏览器并打开特定 URI 的程序
// Java Program to Launch the browser // and open a specific URI import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.net.*; class desk extends JFrame implements ActionListener { // frame static JFrame f; // Main Method public static void main(String args[]) { desk d = new desk(); // create a frame f = new JFrame("desktop"); // create a panel JPanel p = new JPanel(); // create a button JButton b = new JButton("launch"); // add Action Listener b.addActionListener(d); p.add(b); f.add(p); f.show(); f.setSize(200, 200); } // if button is pressed public void actionPerformed(ActionEvent e) { try { // create a URI URI u = new URI("www.geeksforgeeks.org"); Desktop d = Desktop.getDesktop(); d.browse(u); } catch (Exception evt) { } } }
输出 :
- 将邮件发送到特定 URI 的程序
// Java Program to Launch the // mail to a specific URI import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.net.*; class desk extends JFrame implements ActionListener { // frame static JFrame f; // Main Method public static void main(String args[]) { desk d = new desk(); // create a frame f = new JFrame("desktop"); // create a panel JPanel p = new JPanel(); // create a button JButton b = new JButton("launch"); // add Action Listener b.addActionListener(d); p.add(b); f.add(p); f.show(); f.setSize(200, 200); } // if button is pressed public void actionPerformed(ActionEvent e) { try { URI u = new URI("mailto:contribute@geeksforgeeks.org"); Desktop d = Desktop.getDesktop(); d.mail(u); } catch (Exception evt) { JOptionPane.showMessageDialog(this, evt.getMessage()); } } }
输出:
- 打开文件的程序
// Java Program to open a file import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.io.*; import java.net.*; class desk extends JFrame implements ActionListener { // frame static JFrame f; // Main Method public static void main(String args[]) { desk d = new desk(); // create a frame f = new JFrame("desktop"); // create a panel JPanel p = new JPanel(); // create a button JButton b = new JButton("launch"); // add Action Listener b.addActionListener(d); p.add(b); f.add(p); f.show(); f.setSize(200, 200); } // if button is pressed public void actionPerformed(ActionEvent e) { try { // create a file File u = new File("f:\\image.png"); Desktop d = Desktop.getDesktop(); d.open(u); } catch (Exception evt) { JOptionPane.showMessageDialog(this, evt.getMessage()); } } }
输出 :
- 打开文件进行编辑的程序
// Java Program to open a file for editing import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.io.*; import java.net.*; class desk extends JFrame implements ActionListener { // frame static JFrame f; // Main Method public static void main(String args[]) { desk d = new desk(); // create a frame f = new JFrame("desktop"); // create a panel JPanel p = new JPanel(); // create a button JButton b = new JButton("launch"); // add Action Listener b.addActionListener(d); p.add(b); f.add(p); f.show(); f.setSize(200, 200); } // if button is pressed public void actionPerformed(ActionEvent e) { try { // create a file File u = new File("f:\\sample.txt"); Desktop d = Desktop.getDesktop(); d.edit(u); } catch (Exception evt) { JOptionPane.showMessageDialog(this, evt.getMessage()); } } }
输出 :
- 打开文件进行打印的程序
// Java Program to open a file for printing import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.io.*; import java.net.*; class desk extends JFrame implements ActionListener { // frame static JFrame f; // Main Method public static void main(String args[]) { desk d = new desk(); // create a frame f = new JFrame("desktop"); // create a panel JPanel p = new JPanel(); // create a button JButton b = new JButton("launch"); // add Action Listener b.addActionListener(d); p.add(b); f.add(p); f.show(); f.setSize(200, 200); } // if button is pressed public void actionPerformed(ActionEvent e) { try { // create a file File u = new File("f:\\sample.txt"); Desktop d = Desktop.getDesktop(); d.print(u); } catch (Exception evt) { JOptionPane.showMessageDialog(this, evt.getMessage()); } } }
输出 :
参考: https: Java/awt/Desktop.html