javaFx如何連結外部網頁(從javafx內部開啟新的網頁)

來源:互聯網
上載者:User

 

 

Hmm, 2 ways to do it actually.

 

No1 : Use the Desktop API of JDK6. It's simple to use. One example is here.

So, very basic code will go like this :

 

package sample2;<br />import javafx.scene.Scene;<br />import javafx.stage.Stage;<br />import javafx.scene.control.*;<br />import java.net.*;<br />Stage {<br /> title: "HyperLink to URL"<br /> width: 240<br /> height: 320<br /> scene: Scene {<br /> content: [<br /> Hyperlink{<br /> translateY: 160<br /> translateX: 40<br /> width: 150<br /> text: bind "Visit javafx Samples! "<br /> action: function():Void{<br /> java.awt.Desktop.getDesktop().browse(new URI("http://javafx.com/samples"));<br /> }<br /> }<br /> ]<br /> }<br />} 

 

 

 

So, 2 things for running this code. First,Desktop API has been added in JDK6, so this code won't run on JDK5. 

Second, Add rt.jar(rt.jar of JDK6) file in the Libraries if you are using Netbeans

 

No2 : For only JavaFX code, we can use AppletStageExtension like this :

 

package sample1;<br />import javafx.stage.Stage;<br />import javafx.scene.Scene;<br />import javafx.scene.control.Hyperlink;<br />import javafx.stage.AppletStageExtension;<br />Stage {<br /> title: "Hyperlink to URL"<br /> width: 250<br /> height: 80<br /> scene: Scene {<br /> content: [<br /> Hyperlink {<br /> text: "JavaFX Samples !"<br /> action: function() {<br /> AppletStageExtension.showDocument("http://javafx.com/samples");<br /> }<br /> }<br /> ]<br /> }<br />}<br /> 

 

In this case, you cant send hyperlink from Desktop Application, but it will work fine for applet or Browser application. 

 

So, best is to use this and then use our normal funda : if {__PROFILE__}" != "browser") --> use the Desktop API code. What you say :).

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.