我遵循了很多教程,但也许我遗漏了一些东西,因为 url 未加载到 javafx webview 中,并且正在显示白色面板,而不是网页。我尝试使用http://google.com和其他网页,但都没有显示。下面的 JPanel 出现在 JTabbedPane 中。我认为白色面板是下面填充的 JFXPanel。
这是代码:
public class RtcOverview extends JPanel {
private final JFXPanel jfxPanel = new JFXPanel();
private WebEngine engine;
String url = "http://google.com";
public RtcOverview() {
super();
initComponents();
this.add(jfxPanel);
}
private void initComponents() {
Platform.runLater(new Runnable() {
@Override
public void run() {
WebView view = new WebView();
engine = view.getEngine();
engine.load(url);
Scene scene = new Scene(view);
jfxPanel.setScene(scene);
}
});
}
Run Code Online (Sandbox Code Playgroud)

我刚开始使用 Maven 并想更改我当前的 JavaFX8 FXML 应用程序以使用 Maven。作为测试,我试图从运行在 Glassfish 3 上的网络服务中检索国家/地区列表。
当我运行程序时,这发生在我的 FXML 控制器中initialize():
CountryClientSSL cc = new CountryClientSSL();
cc.setUsernamePassword("username", "password");
ObservableList<Country> olCountries = FXCollections.observableArrayList(cc.findAll());
olCountries.stream().forEach((country) -> {
System.out.println(country.getName());
});
cc.close();
Run Code Online (Sandbox Code Playgroud)
该findAll()方法:
public List<Country> findAll() throws ClientErrorException {
WebTarget resource = webTarget;
resource = resource.path("countries");
System.out.println(resource.getUri().toString());
return resource.request(javax.ws.rs.core.MediaType.APPLICATION_XML).get(new GenericType<List<Country>>(){});
}
Run Code Online (Sandbox Code Playgroud)
如果我在浏览器中对其进行测试,该 URI 看起来不错并且可以使用凭据,但是会引发以下错误:
...
Caused by: java.lang.NoSuchMethodError: javax.ws.rs.core.MultivaluedMap.addAll(Ljava/lang/Object;[Ljava/lang/Object;)V
at org.glassfish.jersey.client.ClientRequest.accept(ClientRequest.java:335)
at org.glassfish.jersey.client.JerseyWebTarget.request(JerseyWebTarget.java:221)
at org.glassfish.jersey.client.JerseyWebTarget.request(JerseyWebTarget.java:59)
at DA.CountryClientSSL.findAll(CountryClientSSL.java:85)
...
Run Code Online (Sandbox Code Playgroud)
我的 pom.xml 文件具有 Jersey 依赖项:
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.0.1</version>
</dependency> …Run Code Online (Sandbox Code Playgroud) 使用 JavaFX 的GridPane容器,如何设置按钮(或其他任何东西)以水平扩展直到它填满所有指定的单元格?
我使用以下行将按钮添加到我的 GridPane:
grid.add(button, 3, 0, 3, 1);
Run Code Online (Sandbox Code Playgroud)
但是按钮的宽度仅与其文本一样宽,大约为 2.5 个单元格。我希望它完全占据所有 3 个指定的单元格。
我试图设置我的选择框的默认选定项目,但是它没有按预期工作......
<ChoiceBox fx:id="d" value="- Select choice -">
<String fx:value="hellow"/>
</ChoiceBox>
Run Code Online (Sandbox Code Playgroud) 我正在使用 javaFX,我需要将自定义组件放入我的场景中。因此,我有包含我的组件(例如 DocumentModule)的网格窗格的“main_pane.fxml”。
<GridPane xmlns:fx="http://javafx.com/fxml" fx:controller="GUI.MainPane" gridLinesVisible="true" >
<padding>
<Insets bottom="0" top="0" left="0" right="0" />
</padding>
.
.
.
<DocumentModule fx:id="documentModule"
minWidth="200" minHeight="400"
GridPane.columnIndex="0" GridPane.rowIndex="1">
.
.
.
</DocumentModule>
.
.
.
Run Code Online (Sandbox Code Playgroud)
它们中的每一个都在单独的 fxml 文件中定义。
<GridPane xmlns:fx="http://javafx.com/fxml" fx:controller="GUI.DocumentModule" >
<padding>
<Insets bottom="0" top="0" left="0" right="0" />
</padding>
<ToolBar fx:id="toolBar" GridPane.rowIndex = "0" GridPane.columnIndex = "0" orientation="HORIZONTAL" >
.
.
.
</ToolBar>
<ScrollPane fx:id="scrollPane" hbarPolicy="AS_NEEDED" vbarPolicy="AS_NEEDED" GridPane.rowIndex = "1" GridPane.columnIndex = "0">
<DocumentView fx:id="documentView"/>
</ScrollPane>
</GridPane>
Run Code Online (Sandbox Code Playgroud)
问题是 DocumentModule 在构造后没有初始化。它的构造函数被调用,但不是它的 …
我写了一些代码,一切正常,但是当我在另一台计算机上打开相同的代码时,出现以下错误:
Cannot refer to the non-final local variable usernameTextField defined in an enclosing scope
Cannot refer to the non-final local variable portTextField defined in an enclosing scope
Cannot refer to the non-final local variable usernameTextField defined in an enclosing scope
Cannot refer to the non-final local variable portTextField defined in an enclosing scope
Run Code Online (Sandbox Code Playgroud)
给出此错误的代码:
private static GridPane initGUI(){
GridPane root = new GridPane();
TextField usernameTextField = new TextField();
TextField portTextField = new TextField();
Button button = new Button("Login!");
root.add(new Label("Username:"),0,0); …Run Code Online (Sandbox Code Playgroud) 我想打开一个 pdf 文件并在单击按钮时将其显示在新窗口上,我试了一下,但它不起作用:
Button btn = new Button();
File file=new File("Desktop/Test.pdf");
btn.setText("Open");
btn.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
try {
desktop.open(file);
} catch (IOException ex) {
Logger.getLogger(Exemple.class.getName())
.log(Level.SEVERE, null, ex);
}
}
});
Run Code Online (Sandbox Code Playgroud) 标题说明了一切,我猜。
如何将.jpg(或任何其他支持的图像格式)图像设置为场景背景?
我有点通过使用实现了这个HBox,一Image和ImageView,如下所示:
String url = ...
HBox box= new HBox();
Image x = new Image(url);
ImageView iv = new ImageView(x);
box.getChildren().add(iv);
box.setVisible(true);
Run Code Online (Sandbox Code Playgroud)
然后我首先将该框添加到场景中,然后再添加其他所有内容。
我不是在抱怨那段代码——它适用于我的目的——但是有没有一种正确的方法来设置背景?