📄 desktopdemo.java
字号:
/* * Copyright (c) 1995 - 2008 Sun Microsystems, Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * - Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * - Neither the name of Sun Microsystems nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ package misc;/* * DesktopDemo.java * */import java.awt.*;import java.awt.event.*;import java.io.*;import java.net.*;import javax.swing.*;public class DesktopDemo extends JFrame { JButton btnLaunchApplication = new JButton("Launch Application"); JButton btnLaunchBrowser = new JButton("Launch Browser"); JButton btnLaunchEmail = new JButton(); JRadioButton rbEdit = new JRadioButton("Edit"); JRadioButton rbOpen = new JRadioButton("Open", true); JRadioButton rbPrint = new JRadioButton("Print"); JTextField txtBrowserURI = new JTextField(); JTextField txtMailTo = new JTextField(); JTextField txtFile = new JTextField(); ButtonGroup bgAppAction = new ButtonGroup(); JLabel lblMailRecipient = new JLabel("E-mail:"); JLabel lblBrowserUri = new JLabel("URI:"); JLabel lblFile = new JLabel("File:"); JButton btnFile = new JButton("..."); JLabel emptyLabel = new JLabel(" "); JPanel conLeft = new JPanel(); JPanel conCenter = new JPanel(); JPanel conRight = new JPanel(); JFileChooser fc = new JFileChooser(); File file; private Desktop desktop; private Desktop.Action action = Desktop.Action.OPEN; /** * Creates new form DesktopDemo */ public DesktopDemo() { // init all gui components initComponents(); // disable buttons that launch browser, email client, // disable buttons that open, edit, print files disableActions(); // before any Desktop APIs are used, first check whether the API is // supported by this particular VM on this particular host if (Desktop.isDesktopSupported()) { desktop = Desktop.getDesktop(); // now enable buttons for actions that are supported. enableSupportedActions(); } loadFrameIcon(); setResizable(false); } public static void main(String args[]) { /* Use an appropriate Look and Feel */ try { //UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); //UIManager.setLookAndFeel("com.sun.java.swing.plaf.gtk.GTKLookAndFeel"); UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel"); } catch (UnsupportedLookAndFeelException ex) { ex.printStackTrace(); } catch (IllegalAccessException ex) { ex.printStackTrace(); } catch (InstantiationException ex) { ex.printStackTrace(); } catch (ClassNotFoundException ex) { ex.printStackTrace(); } /* Turn off metal's use of bold fonts */ UIManager.put("swing.boldMetal", Boolean.FALSE); //Schedule a job for the event-dispatching thread: //creating and showing this application's GUI. SwingUtilities.invokeLater(new Runnable() { public void run() { new DesktopDemo().setVisible(true); } }); } /** Create and show components */ private void initComponents() { setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); setTitle("DesktopDemo"); txtBrowserURI.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { onLaunchBrowser(null); } }); btnLaunchBrowser.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { onLaunchBrowser(evt); } }); txtMailTo.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { onLaunchMail(null); } }); btnLaunchEmail.setText("Launch Mail"); btnLaunchEmail.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { onLaunchMail(evt); } }); txtFile.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { onLaunchDefaultApplication(null); } }); rbOpen.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { onOpenAction(evt); } }); rbEdit.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { onEditAction(evt); } }); rbPrint.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { onPrintAction(evt); } }); btnLaunchApplication.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { onLaunchDefaultApplication(evt); } }); btnFile.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { onChooseFile(evt); } }); Container conFrame = this.getContentPane(); bgAppAction.add(rbOpen); bgAppAction.add(rbEdit); bgAppAction.add(rbPrint); // Components layouting GroupLayout layout = new GroupLayout(conFrame); conFrame.setLayout(layout); layout.setAutoCreateContainerGaps(true); layout.setAutoCreateGaps(true); GroupLayout.SequentialGroup majorHGroup = layout.createSequentialGroup(); // Horizontal group GroupLayout.ParallelGroup lblHGroup = layout.createParallelGroup(GroupLayout.Alignment.LEADING); lblHGroup.addComponent(lblBrowserUri, GroupLayout.Alignment.TRAILING); lblHGroup.addComponent(lblMailRecipient, GroupLayout.Alignment.TRAILING); lblHGroup.addComponent(lblFile, GroupLayout.Alignment.TRAILING); GroupLayout.ParallelGroup txtFieldsHGroup = layout.createParallelGroup(GroupLayout.Alignment.LEADING); txtFieldsHGroup.addComponent(txtMailTo); txtFieldsHGroup.addComponent(txtBrowserURI); GroupLayout.SequentialGroup rbHGroup = layout.createSequentialGroup(); rbHGroup.addComponent(rbOpen); rbHGroup.addComponent(rbEdit); rbHGroup.addComponent(rbPrint); txtFieldsHGroup.addGroup(rbHGroup); GroupLayout.SequentialGroup fileHGroup = layout.createSequentialGroup(); fileHGroup.addComponent(txtFile);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -