我的JavaFX窗口出现了这个问题,我添加了一个包含图形的Scrollpan。当我启动应用程序时,我需要调整窗口大小以便显示内容。
以下是模型:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<VBox xmlns:fx="http://javafx.com/fxml/1" prefHeight="544.0" prefWidth="788.0" xmlns="http://javafx.com/javafx/8.0.121"
fx:controller="Solution.SolutionController">
<MenuBar fx:id="menuBar">
<menus>
<Menu text="File">
<items>
<MenuItem text="Save"/>
</items>
</Menu>
</menus>
</MenuBar>
<VBox fx:id="contenu" alignment="CENTER">
<HBox alignment="CENTER">
<ScrollPane style="-fx-background: WHITE" fx:id="pane" focusTraversable="false" prefHeight="608.0"
prefWidth="700">
</ScrollPane>
</HBox>
<Label fx:id="counter">
</Label>
<VBox.margin>
<Insets top="17.0"/>
</VBox.margin>
</VBox>
<HBox alignment="CENTER" prefWidth="690.0">
<Button onAction="#handleGauche" text="Gauche"/>
<Button onAction="#handleDroite" text="Droite"/>
</HBox>
</VBox>
这是控制器:
package Solution;
import Main.Individual;
import javafx.embed.swing.SwingNode;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.MenuBar;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
public class SolutionController {
public VBox contenu;
public Label counter;
public MenuBar menuBar;
public ScrollPane pane;
ArrayList<Individual> graph;
JPanel panel;
int i;
public void init() {
i = 0;
panel = new JPanel();
panel.setBackground(Color.WHITE);
panel.add(graph.get(i).chopper.getContentPane());
panel.validate();
panel.repaint();
counter.setText((i+1) + "/" + graph.size());
SwingNode sn = new SwingNode();
sn.setContent(panel);
pane.setContent(sn);
}
@FXML
void handleDroite(ActionEvent event) {
i = (i + 1) % graph.size();
changeContent();
}
@FXML
void handleGauche(ActionEvent event) {
i = (graph.size() + (i - 1)) % graph.size();
changeContent();
}
public void changeContent(){
counter.setText((i+1) + "/" + graph.size());
panel.add(graph.get(i).chopper.getContentPane());
panel.repaint();
SwingNode sn = new SwingNode();
sn.setContent(panel);
pane.setContent(sn);
}
}
以下是观点:
package Solution;
import Main.Individual;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.io.IOException;
import java.util.ArrayList;
public class SolutionApplication extends Application{
ArrayList<Individual> graphI;
public SolutionApplication(ArrayList<Individual> graph) {
graphI = new ArrayList<>(graph);
}
public void start(Stage primaryStage) throws IOException {
FXMLLoader loader = new FXMLLoader(getClass().getResource("Solution.fxml"));
Parent root = loader.load();
SolutionController controller = loader.getController();
controller.graph = new ArrayList<>(graphI);
controller.init();
primaryStage.setTitle("Main Window");
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
}
我在这篇文章中偶然发现了 javafx SwingNode not working until window is resized 但他们说它是JDK的一个错误?我使用的是版本10
我想知道如何解决这个问题。
感谢阅读。