こんにちは皆さん。私は通常自分自身を見つけ、修正ミスしようが、今回は私が本当のために捕まってしまいました。私の割り当ては、ローン電卓を書くことでした。すべてのコードが動作し、うまく私は新しいウィンドウでポップアップ表示ラインダイアグラム/グラフを作成する必要がポイントに着くまでコンパイルしました。
どこかFXMLファイルをロードするか、メインコントローラに追加のコントローラを接続する際の問題の嘘。
私は別のアプローチを試してみましたが、解決のために別のフォーラムで確認したが、私のコードに1を実装していませんでした。誰も私に解決策を提案してもらえますか?
ここでプログラムを起動し、私の主です。
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("Scene.fxml"));
primaryStage.setTitle("Loan calculator");
primaryStage.setScene(new Scene(root, 770, 410));
primaryStage.setResizable(false);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
これは私のメイン・コントローラです。リトルノート。私は私のメインの一つに私の第二のコントローラの方法を「初期化」を使用方法が正しくありませんが、私は別のアプローチを試してみましたが、彼らは私に任意のより良い結果を得られなかったことを理解し
public class Controller implements Initializable {
public static int years = 0;
public static int months = 0;
private double desiredLoan = 1; //should be set to zero,but for testing is set differently
private boolean graph = true; //true - linear, false - annuity
@FXML
private Button Button_3 = new Button();
private LineGraphController lineGraphController = new LineGraphController("Linear");
private AnnuityGraphController annuityGraphController = new AnnuityGraphController("Annuity");
/**Some code to count my data*/
@Override /** This method is used to access my UI elements and access other controllers*/
public void initialize(URL url, ResourceBundle resourceBundle) {
Button_3.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
try {
if (desiredLoan == 0 && months == 0 && years == 0) {
throw new RuntimeException();
}
else {
if (whatGraph() == true) { //make linear graph
lineGraphController.initialize(url, resourceBundle);
}
else {//make annuity graph
annuityGraphController.initialize(url, resourceBundle);
}
}
}
catch (RuntimeException error) {
error.printStackTrace();
}
}
});
}
/** Getters and setters */
public boolean whatGraph() {
return graph;
}
public void setGraph(boolean graph) {
this.graph = graph;
}
}
私のメインコントローラ:
折れ線グラフコントローラ
/** This controller is used to load additional fxml file*/
public class LineGraphController implements Initializable {
@FXML
public LineChart<?, ?> LineGraph;
private String title;
public LineGraphController(String title) {
this.title = title;
}
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("LineGraph.fxml"));
Parent lineGraph = null;
try {
lineGraph = (Parent)fxmlLoader.load();
} catch (IOException e) {
e.printStackTrace();
}
Stage window = new Stage();
window.initModality(Modality.APPLICATION_MODAL);
window.setTitle(title);
window.setResizable(false);
window.setMinWidth(600);
window.setMinHeight(400);
window.setScene(new Scene(lineGraph));
window.showAndWait();
}
}
年金グラフコントローラ
/** This controller is used to load additional fxml file*/
public class AnnuityGraphController implements Initializable {
@FXML
public LineChart<?, ?> AnnuityGraph;
private String title;
public AnnuityGraphController(String title) {
this.title = title;
}
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("AnnuityGraph.fxml"));
Parent lineGraph = null;
try {
lineGraph = (Parent)fxmlLoader.load();
} catch (IOException e) {
e.printStackTrace();
}
Stage window = new Stage();
window.initModality(Modality.APPLICATION_MODAL);
window.setTitle(title);
window.setResizable(false);
window.setMinWidth(600);
window.setMinHeight(400);
window.setScene(new Scene(lineGraph));
window.showAndWait();
}
}
私のメインFXMLファイル。
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" style="-fx-background-color: #4a4a4a;" xmlns="http://javafx.com/javafx/10.0.2" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Paskolu_Skaiciuokle.Controller">
<center>
<Button fx:id="Button_3" maxWidth="150.0" mnemonicParsing="false" prefHeight="26.0" prefWidth="100.0" style="-fx-effect: dropshadow(gaussian, rgba(0, 0, 0, 0.3), 3, 0,5, 5, 5);" text="Show graph" BorderPane.alignment="CENTER">
<font>
<Font name="Times New Roman" size="12.0" />
</font>
</Button>
</center>
</BorderPane>
コントローラのための私の追加FXMLファイル:
折れ線グラフFXML
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="670.0" prefWidth="800.0" xmlns="http://javafx.com/javafx/10.0.2" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Loan_calculator.LineGraphController">
<!-- some code -->
</AnchorPane>
年金グラフFXML
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="670.0" prefWidth="800.0" xmlns="http://javafx.com/javafx/10.0.2" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Loan_calculator.AnnuityGraphController">
<-- some code -->
</AnchorPane>
助けを事前にありがとうございます。
psのこれらは私が解決策を探してみましたここでリンクされている、コードこれにさまざまな方法がたくさんありましたが、ちょうど私が私のコードに実装することができたものを見つけることができませんでした...または多分私はどれだけの知識が不足していますやれ。いずれにせよ、私は誰かが私を助けるか、この問題を解決する方法について説明することができます願っています。リンク:
• パラメータのJavaFX FXML渡し
• 異なるFXMLファイルで複数のJavaFXのコントローラを作成するために、どのように?
• コントローラと複数のFXML、共有オブジェクト
私の主な問題は、私のメインコントローラからの追加のコントローラにアクセスしています。(コントローラのすべては、自分自身のFXMLファイルにリンクされています)。
私は非常によくあなたの問題を理解していないが、私は答えることを試みます。私はあなたがメインコントローラから他のコントローラにアクセスしたいと思いますが、最も簡単な方法は次のとおりです。
FXMLLoader mainLoader = new FXMLLoader(getClass().getResource("MainController.fxml"));
Parent main = mainLoader.load();
MainController mainController = mainLoader.getController();
FXMLLoader otherLoader = new FXMLLoader(getClass().getResource("OtherController.fxml"));
Parent other = otherLoader.load();
// set other controller in main controller
mainController.setOtherController(otherLoader.getController());
あなたが使用している場合はJavaFXの-ウィーバーとスプリングブーツを、DIは、それがより簡単になります。
@Component
@FxmlView
class MainController {
@Autowired
private FxControllerAndView<OtherController, VBox> otherControllerAndView;
// otherControllerAndView.getController() to access other controller
}