我正在框架内的JavaFX浏览器中制作一个带有Url加载的摇摆应用程序。我必须将该浏览器框架分为两部分,因为在框架的左侧将显示内容,在右侧将显示图像。
import java.awt.BorderLayout;
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.ObservableList;
import javafx.concurrent.Worker;
import javafx.concurrent.Worker.State;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(final Stage stage) {
stage.setWidth(400);
stage.setHeight(500);
Scene scene = new Scene(new Group());
//Group root = new Group();
// Scene scene = new Scene(root,80,20);
final WebView browser = new WebView();
final WebEngine webEngine = browser.getEngine();
ScrollPane scrollPane = new ScrollPane();
scrollPane.setContent(browser);
webEngine.getLoadWorker().stateProperty()
.addListener(new ChangeListener<State>() {
@Override
public void changed(ObservableValue ov, State oldState, State newState) {
if (newState == Worker.State.SUCCEEDED) {
stage.setTitle(webEngine.getLocation());
stage.setTitle(webEngine.getLocation());
String trgurl=webEngine.getLocation();
System.out.println(trgurl);
if(trgurl.matches("(.*)img=(.*)")){
int n = trgurl.indexOf("&img=");
System.out.println(n+7);
int len = trgurl.length();
System.out.println("string length is:
"+trgurl.length());
System.out.println(trgurl.substring(n+7,len));
String find = "file://"+trgurl.substring(n+7,len);
System.out.println(find);
try {
new File(find).toURI().toURL();
}
catch(Exception e){
}
}
}
}
});
webEngine.load("http://www.example.com");
scene.setRoot(scrollPane);
stage.setScene(scene);
stage.show();
stage.setResizable(true);
}
private static void extracted(String[] args) {
launch(args);
}
}
当我在滚动窗格的左侧加载此 url 时,内容正在加载,但在右侧没有加载图像,除此之外该程序没有错误。 如果我尝试将浏览器添加到内部框架,怎么可能
像这样,您可以将单个 url 分为两个浏览器,即在一个浏览器加载图像,在另一个浏览器加载内容 在此处输入代码/*
package jdesktop;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.concurrent.Worker;
import javafx.event.ActionEvent;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
public class NewClass extends Application {
private static Object webEngine;
@Override
public void start(final Stage stage) {
final WebView browser = new WebView();
final WebEngine webEngine = browser.getEngine();
final WebView browser1 = new WebView();
final WebEngine webEngine1 = browser1.getEngine();
webEngine.getLoadWorker().stateProperty()
.addListener(new ChangeListener<Worker.State>() {
@Override
public void changed(ObservableValue ov, Worker.State oldState, Worker.State newState) {
if (newState == Worker.State.SUCCEEDED) {
stage.setTitle(webEngine.getLocation());
String trgurl=webEngine.getLocation();
System.out.println(trgurl);
if(trgurl.matches("(.*)img=(.*)")){
int n = trgurl.indexOf("&img=");
//String str1 = Integer.toString(n);
System.out.println(n+5);
int len = trgurl.length();
System.out.println("string length is: "+trgurl.length());
System.out.println(trgurl.substring(n+5,len));
String find = "file:"+trgurl.substring(n+5,len);
System.out.println(find);
webEngine1.load(find);// this is loading image
}
}
}
});
String url = "http://192.168.2.18/a3/host/pmms.ankiti.com/?";
// Load a page from remote url.
webEngine.load(url);
buttonURL.setOnAction((ActionEvent event) -> {
String url1 = "http://192.168.2.18/a3/host/pmms.ankiti.com/?";
// Load a page from remote url.
webEngine.load(url1);
});
HBox root = new HBox();
root.setPadding(new Insets(3));
root.setSpacing(3);
root.getChildren().addAll( browser1,browser);
Scene scene = new Scene(root);
stage.setScene(scene);
stage.setWidth(450);
stage.setHeight(300);
stage.show();
}
public static void main(String[] args) {
launch(args);
try {
String webEngineLocation = webEngine.getLocation();
URL url = new URL(webEngineLocation);
} catch (MalformedURLException e) {
}
}
}