java浏览器开发,其实很简单, 使用JavaFX的WebView组件即可.(支持html5)也可以在swing里使用JFXPanel然后添加WebView
效果图如下
参考代码如下(java8+win10 x64 编写,测试)
import javafx.application.Application
import javafx.concurrent.Worker.State
import javafx.geometry.Insets
import javafx.scene.Scene
import javafx.scene.control.*
import javafx.scene.layout.*
import javafx.scene.web.*
import javafx.stage.Stage
public class WebViewTest extends Application {
public static void main(String[] args) {
launch(args)
}
@Override
public void start(Stage primaryStage) throws Exception {
//顶部
HBox hbox=new HBox(8)
Label labeAddr=new Label("地址")
TextField tfAddr = new TextField()
tfAddr.setMinWidth(500)
tfAddr.setText("http://www.baidu.com")
Button btnGo = new Button("Go转到")
Button btnGoBack=new Button("<-")
Button btnH=new Button("->")
TextField tfSerch = new TextField()
Button btnSerch= new Button("捜")
hbox.getChildren().addAll(labeAddr,tfAddr,btnGoBack,btnH,btnGo,tfSerch,btnSerch)
hbox.setPadding(new Insets(5))
//中间
WebView wv = new WebView()
WebEngine we = wv.getEngine()
we.getLoadWorker().stateProperty().addListener((ob, ov, nv) ->{
if(nv==State.SCHEDULED) {
tfAddr.setText(we.getLocation())//
}
})
//底部
Label labelBottom = new Label()
labelBottom.textProperty().bind(we.getLoadWorker().messageProperty())//底下的信息显示
BorderPane root = new BorderPane()
root.setTop(hbox)
root.setCenter(wv)
root.setBottom(labelBottom)
Scene scene = new Scene(root, 900, 600)
primaryStage.setTitle("JavaFX 雪飞潇潇 web浏览器")
primaryStage.setScene(scene)
primaryStage.show()
btnGo.setOnAction(e ->{
we.load(tfAddr.getText().trim())
})
btnGoBack.setOnAction(e->{
we.executeScript("history.back()")//执行JavaScript,后退
})
btnH.setOnAction(e->{
we.executeScript("history.forward()")//执行JavaScript,前进
})
//搜索
btnSerch.setOnAction(e->{
we.load("https://www.baidu.com/s?wd="+tfSerch.getText())
})
}
}
和其他的UI控件一样,HTMLEditor也必须添加到场景中,你可以直接向下面这样直接添加到场景中,也可以添加到布局容器中。import javafx.application.Application
import javafx.scene.Scene
import javafx.scene.web.HTMLEditor
import javafx.stage.Stage
public class HTMLEditorSample extends Application {
@Override
public void start(Stage stage) {
stage.setTitle("HTMLEditor Sample")
stage.setWidth(650650)
stage.setHeight(300)
final HTMLEditor htmlEditor = new HTMLEditor()
htmlEditor.setPrefHeight(245)
Scene scene = new Scene(htmlEditor)
stage.setScene(scene)
stage.show()
}
public static void main(String[] args) {
launch(args)
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)