下面列出了java.awt.Desktop#getDesktop ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void jLabel_webMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel_webMouseClicked
// TODO add your handling code here:
try {
URI uri = new URI("http://www.meteothink.org");
Desktop desktop = null;
if (Desktop.isDesktopSupported()) {
desktop = Desktop.getDesktop();
}
if (desktop != null) {
desktop.browse(uri);
}
} catch (URISyntaxException ex) {
Logger.getLogger(FrmAbout.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ioe) {
}
}
private void jLabel_webMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel_webMouseClicked
// TODO add your handling code here:
try {
URI uri = new URI("http://www.meteothink.org");
Desktop desktop = null;
if (Desktop.isDesktopSupported()) {
desktop = Desktop.getDesktop();
}
if (desktop != null) {
desktop.browse(uri);
}
} catch (URISyntaxException ex) {
Logger.getLogger(FrmAbout.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ioe) {
}
}
public static boolean open(String url) {
Desktop desktop = Desktop.isDesktopSupported() ? Desktop.getDesktop() : null;
if (desktop != null && desktop.isSupported(Desktop.Action.BROWSE)) {
try {
desktop.browse(new URL(url).toURI());
return true;
}
catch (Exception e) {
e.printStackTrace();
}
}
JFrame frame = new JFrame("Help");
JLabel lbl = new JLabel(url);
frame.add(lbl);
frame.pack();
frame.setVisible(true);
return false;
}
public static void open(final String filename, final Program program) {
File file = new File(filename);
LOG.info("Opening: {}", file.getName());
if (isSupported(Desktop.Action.OPEN)) {
Desktop desktop = Desktop.getDesktop();
try {
desktop.open(file);
return;
} catch (IOException ex) {
LOG.warn(" Opening Failed: {}", ex.getMessage());
}
} else {
LOG.warn(" Opening failed");
}
JOptionPane.showMessageDialog(program.getMainWindow().getFrame(), "Could not open " + file.getName(), "Open File", JOptionPane.PLAIN_MESSAGE);
}
@FXML
public void startTwitterAuth() {
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setOAuthConsumerKey(txtTwitterConsumerKey.getText());
cb.setOAuthConsumerSecret(txtTwitterConsumerSecret.getText());
cb.setOAuthAccessToken(null);
cb.setOAuthAccessTokenSecret(null);
TwitterFactory twitterfactory = new TwitterFactory(cb.build());
Twitter twitter = twitterfactory.getInstance();
try {
requestToken = twitter.getOAuthRequestToken();
Desktop desktop = Desktop.getDesktop();
URI uri = new URI(requestToken.getAuthorizationURL());
desktop.browse(uri);
player.setTwitterConsumerKey(txtTwitterConsumerKey.getText());
player.setTwitterConsumerSecret(txtTwitterConsumerSecret.getText());
player.setTwitterAccessToken("");
player.setTwitterAccessTokenSecret("");
txtTwitterPIN.setDisable(false);
twitterPINButton.setDisable(false);
txtTwitterAuthenticated.setVisible(false);
} catch (Exception e) {
e.printStackTrace();
}
}
private static boolean attemptDesktopOpen(String directory)
{
if (!Desktop.isDesktopSupported())
{
return false;
}
final Desktop desktop = Desktop.getDesktop();
if (!desktop.isSupported(Desktop.Action.OPEN))
{
return false;
}
try
{
desktop.open(new File(directory));
return true;
}
catch (IOException ex)
{
log.warn("Failed to open Desktop#open {}", directory, ex);
return false;
}
}
/**
* to visit a URL address
*/
public static void visitAddress(String adress) {
try {
URI uri = new URI(adress);
Desktop desktop = null;
if (Desktop.isDesktopSupported()) {
desktop = Desktop.getDesktop();
}
if (desktop != null) desktop.browse(uri);
} catch (IOException ioe) {
ioe.printStackTrace();
} catch (URISyntaxException use) {
use.printStackTrace();
}
}
@Override
protected void call() throws Exception {
Desktop desktop = Desktop.getDesktop();
String message = "mailto:[email protected]?subject=TinkerTime%20Support%20Request";
URI uri = URI.create(message);
desktop.mail(uri);
}
@CalledOnlyBy(AmidstThread.EDT)
private void openURL(URI uri) throws IOException, UnsupportedOperationException {
if (Desktop.isDesktopSupported()) {
Desktop desktop = Desktop.getDesktop();
if (desktop.isSupported(Desktop.Action.BROWSE)) {
desktop.browse(uri);
} else {
throw new UnsupportedOperationException("Unable to open browser page.");
}
} else {
throw new UnsupportedOperationException("Unable to open browser.");
}
}
private void openButtonMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_openButtonMouseReleased
Desktop dt = Desktop.getDesktop();
if(dt != null) {
try {
dt.browse(new URI("http://www.hel.fi/palvelukartta"));
}
catch(Exception e) {
e.printStackTrace();
}
}
}
/**
* Launches a file browser or opens a file with java Desktop.open() if is
* supported
*
* @param file
*/
private void launchFile(File file) {
if (!Desktop.isDesktopSupported())
return;
Desktop dt = Desktop.getDesktop();
try {
dt.open(file);
} catch (IOException ex) {
launchFile(file.getPath());
}
}
public static void openBrowse(String url) throws Exception {
Desktop desktop = Desktop.getDesktop();
boolean flag = Desktop.isDesktopSupported() && desktop.isSupported(Desktop.Action.BROWSE);
if (flag) {
try {
URI uri = new URI(url);
desktop.browse(uri);
} catch (Exception e) {
throw new Exception("can't open browse", e);
}
} else {
throw new Exception("don't support browse");
}
}
private static boolean isSupported(final Desktop.Action action) {
if (Desktop.isDesktopSupported()) {
Desktop desktop = Desktop.getDesktop();
if (desktop.isSupported(action)) {
return true;
}
}
return false;
}
private static void openWebPage(URI uri) {
Desktop desktop = Desktop.isDesktopSupported() ? Desktop.getDesktop() : null;
if (desktop != null && desktop.isSupported(Desktop.Action.BROWSE)) {
try {
desktop.browse(uri);
} catch (IOException e) {
throw new IllegalStateException("Problem opening " + uri.toString(), e);
}
}
}
public static void openFileWithDesktop(File file) {
try {
Desktop desktop = Desktop.getDesktop();
desktop.open(file);
} catch (IOException ex) {
Logger.getLogger(LibraryAssistantUtil.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static boolean openWebpage(URI uri) {
Desktop desktop = Desktop.isDesktopSupported() ? Desktop.getDesktop() : null;
if (desktop != null && desktop.isSupported(Desktop.Action.BROWSE)) {
try {
desktop.browse(uri);
return true;
} catch (Exception e) {
e.printStackTrace();
}
}
return false;
}
public static void openWebpage(URI uri) {
Desktop desktop = Desktop.isDesktopSupported() ? Desktop.getDesktop() : null;
if (desktop != null && desktop.isSupported(Desktop.Action.BROWSE)) {
try {
desktop.browse(uri);
} catch (Exception e) {
e.printStackTrace();
}
}
}
static void uninstall() {
Desktop app = Desktop.getDesktop();
app.setAboutHandler(null);
app.setOpenFileHandler(null);
app.setPreferencesHandler(null);
app.setQuitHandler(null);
}
@Test
public void notSupportedDesktopInOpenInBrowser() throws URISyntaxException {
URI uri = new URI("http://example.com");
when(Desktop.isDesktopSupported()).thenReturn(false);
ExternalsUtil.openInBrowser(uri);
verifyStatic(never());
Desktop.getDesktop();
}
private void scriptLabelMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_scriptLabelMouseReleased
try {
Desktop desktop = Desktop.getDesktop();
desktop.browse(new URI("http://wandora.org/wiki/Query_language"));
}
catch(Exception e) {
e.printStackTrace();
}
}