📄 desktopdemo.java
字号:
fileHGroup.addComponent(btnFile); txtFieldsHGroup.addGroup(fileHGroup); GroupLayout.ParallelGroup btnHGroup = layout.createParallelGroup(GroupLayout.Alignment.LEADING); btnHGroup.addComponent(btnLaunchBrowser); btnHGroup.addComponent(btnLaunchEmail); btnHGroup.addComponent(btnLaunchApplication); majorHGroup.addGroup(lblHGroup); majorHGroup.addGroup(txtFieldsHGroup); majorHGroup.addGroup(btnHGroup); layout.setHorizontalGroup(majorHGroup); // Vertical group GroupLayout.SequentialGroup majorVGroup = layout.createSequentialGroup(); GroupLayout.ParallelGroup uriVGroup = layout.createParallelGroup(GroupLayout.Alignment.BASELINE); uriVGroup.addComponent(lblBrowserUri); uriVGroup.addComponent(txtBrowserURI); uriVGroup.addComponent(btnLaunchBrowser); GroupLayout.ParallelGroup mailVGroup = layout.createParallelGroup(GroupLayout.Alignment.BASELINE); mailVGroup.addComponent(lblMailRecipient); mailVGroup.addComponent(txtMailTo); mailVGroup.addComponent(btnLaunchEmail); GroupLayout.ParallelGroup rbVGroup = layout.createParallelGroup(GroupLayout.Alignment.BASELINE); rbVGroup.addComponent(rbOpen); rbVGroup.addComponent(rbEdit); rbVGroup.addComponent(rbPrint); GroupLayout.ParallelGroup fileVGroup = layout.createParallelGroup(GroupLayout.Alignment.BASELINE); fileVGroup.addComponent(lblFile); fileVGroup.addComponent(btnLaunchApplication); fileVGroup.addComponent(txtFile); fileVGroup.addComponent(btnFile); majorVGroup.addGroup(uriVGroup); majorVGroup.addGroup(mailVGroup); majorVGroup.addGroup(rbVGroup); majorVGroup.addGroup(fileVGroup); layout.setVerticalGroup(majorVGroup); pack(); } /** * Load the "desktop" icon into our frame window. */ private void loadFrameIcon() { URL imgUrl = null; ImageIcon imgIcon = null; imgUrl = DesktopDemo.class.getResource("images/desk32.gif"); imgIcon = new ImageIcon(imgUrl); Image img = imgIcon.getImage(); this.setIconImage(img); } /* * Set the Desktop.Action to PRINT before invoking * the default application. */ private void onPrintAction(ActionEvent evt) { action = Desktop.Action.PRINT; } /** * Set the Desktop.Action to EDIT before invoking * the default application. */ private void onEditAction(ActionEvent evt) { action = Desktop.Action.EDIT; } /** * Set the Desktop.Action to OPEN before invoking * the default application. */ private void onOpenAction(ActionEvent evt) { action = Desktop.Action.OPEN; } /** * Launch the default application associated with a specific * filename using the preset Desktop.Action. * */ private void onLaunchDefaultApplication(ActionEvent evt) { String fileName = txtFile.getText(); File file = new File(fileName); try { switch(action) { case OPEN: desktop.open(file); break; case EDIT: desktop.edit(file); break; case PRINT: desktop.print(file); break; } } catch (IOException ioe) { //ioe.printStackTrace(); System.out.println("Cannot perform the given operation to the " + file + " file"); } } /** * Launch the default email client using the "mailto" * protocol and the text supplied by the user. * */ private void onLaunchMail(ActionEvent evt) { String mailTo = txtMailTo.getText(); URI uriMailTo = null; try { if (mailTo.length() > 0) { uriMailTo = new URI("mailto", mailTo, null); desktop.mail(uriMailTo); } else { desktop.mail(); } } catch(IOException ioe) { ioe.printStackTrace(); } catch(URISyntaxException use) { use.printStackTrace(); } } /** * Launch the default browser with the text provided by the * user. * */ private void onLaunchBrowser(ActionEvent evt) { URI uri = null; try { uri = new URI(txtBrowserURI.getText()); desktop.browse(uri); } catch(IOException ioe) { System.out.println("The system cannot find the " + uri + " file specified"); //ioe.printStackTrace(); } catch(URISyntaxException use) { System.out.println("Illegal character in path"); //use.printStackTrace(); } } private void onChooseFile(ActionEvent evt) { if (evt.getSource() == btnFile) { int returnVal = fc.showOpenDialog(DesktopDemo.this); if (returnVal == JFileChooser.APPROVE_OPTION){ file = fc.getSelectedFile(); txtFile.setText(file.getAbsolutePath()); } } } /** * Enable actions that are supported on this host. * The actions are: open browser, open email client, and * open, edit, and print files using their associated application */ private void enableSupportedActions() { if (desktop.isSupported(Desktop.Action.BROWSE)) { txtBrowserURI.setEnabled(true); btnLaunchBrowser.setEnabled(true); } if (desktop.isSupported(Desktop.Action.MAIL)) { txtMailTo.setEnabled(true); btnLaunchEmail.setEnabled(true); } if (desktop.isSupported(Desktop.Action.OPEN)) { rbOpen.setEnabled(true); } if (desktop.isSupported(Desktop.Action.EDIT)) { rbEdit.setEnabled(true); } if (desktop.isSupported(Desktop.Action.PRINT)) { rbPrint.setEnabled(true); } if (rbEdit.isEnabled() || rbOpen.isEnabled() || rbPrint.isEnabled()) { txtFile.setEnabled(true); btnLaunchApplication.setEnabled(true); btnFile.setEnabled(true); } } /** * Disable all graphical components until we know * whether their functionality is supported. */ private void disableActions() { txtBrowserURI.setEnabled(false); btnLaunchBrowser.setEnabled(false); txtMailTo.setEnabled(false); btnLaunchEmail.setEnabled(false); rbEdit.setEnabled(false); rbOpen.setEnabled(false); rbPrint.setEnabled(false); txtFile.setEnabled(false); btnLaunchApplication.setEnabled(false); btnFile.setEnabled(false); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -