ヘディングのテキストプログラミング初心者です。
vscodeでscene builderを使用してguiを作成したいと考えています。
いくつかのサイトを参考にして、javafx、scene builderの設定を行い、vscodeで使用できる設定ができていると思います。
ここで、各コントロールのイベントを設定したいと考えております。
しかし、イベント用のコードをどのように挿入したらよいのかわかりません。
未熟なコードとなり大変申し訳ございませんが、ご意見いただければ幸いです。
以下に私の現状を添付致します。
import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.stage.Stage; public class App extends Application { @Override public void start(Stage primaryStage) { try { FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("Mainframe.fxml")); Parent root = (Parent) fxmlLoader.load(); Scene scene = new Scene(root); primaryStage.setTitle("test-Window"); primaryStage.setScene(scene); primaryStage.show(); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { launch(args); } }
以下 Mainframe.java
import java.net.URL; import java.util.ResourceBundle; import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.scene.control.Button; import javafx.scene.control.TextField; import java.util.Optional; import javafx.scene.control.Alert; import javafx.scene.control.Alert.AlertType; import javafx.scene.control.ButtonType; public class Mainframe { @FXML private ResourceBundle resources; @FXML private URL location; @FXML private Button button1; @FXML private TextField textFiled1; @FXML void handleButtonAction(ActionEvent event) { Alert alert = new Alert(AlertType.CONFIRMATION); alert.setTitle("Title!"); alert.setHeaderText("Header!"); alert.setContentText("You entered: " + textFiled1.getText()); Optional<ButtonType> result = alert.showAndWait(); if (result.isPresent() && result.get() == ButtonType.OK) { System.out.println("You clicked OK"); } } @FXML void initialize() { assert button1 != null : "fx:id=\"button1\" was not injected: check your FXML file 'Mainframe.fxml'."; assert textFiled1 != null : "fx:id=\"textFiled1\" was not injected: check your FXML file 'Mainframe.fxml'."; } }
Mainframe.fxml
<?xml version="1.0" encoding="UTF-8"?> <!-- Copyright (c) 2015, 2019, Gluon and/or its affiliates. All rights reserved. Use is subject to license terms. This file is available and licensed under the following license: Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. - Neither the name of Oracle Corporation nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. --> <?import javafx.scene.control.Button?> <?import javafx.scene.control.Label?> <?import javafx.scene.control.Menu?> <?import javafx.scene.control.MenuBar?> <?import javafx.scene.control.MenuItem?> <?import javafx.scene.control.SeparatorMenuItem?> <?import javafx.scene.control.TextField?> <?import javafx.scene.layout.AnchorPane?> <?import javafx.scene.layout.VBox?> <?import javafx.scene.text.Font?> <VBox prefHeight="400.0" prefWidth="640.0" xmlns="http://javafx.com/javafx/18" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Mainframe"> <children> <MenuBar VBox.vgrow="NEVER"> <menus> <Menu mnemonicParsing="false" text="File"> <items> <MenuItem mnemonicParsing="false" text="New" /> <MenuItem mnemonicParsing="false" text="Open…" /> <Menu mnemonicParsing="false" text="Open Recent" /> <SeparatorMenuItem mnemonicParsing="false" /> <MenuItem mnemonicParsing="false" text="Close" /> <MenuItem mnemonicParsing="false" text="Save" /> <MenuItem mnemonicParsing="false" text="Save As…" /> <MenuItem mnemonicParsing="false" text="Revert" /> <SeparatorMenuItem mnemonicParsing="false" /> <MenuItem mnemonicParsing="false" text="Preferences…" /> <SeparatorMenuItem mnemonicParsing="false" /> <MenuItem mnemonicParsing="false" text="Quit" /> </items> </Menu> <Menu mnemonicParsing="false" text="Edit"> <items> <MenuItem mnemonicParsing="false" text="Undo" /> <MenuItem mnemonicParsing="false" text="Redo" /> <SeparatorMenuItem mnemonicParsing="false" /> <MenuItem mnemonicParsing="false" text="Cut" /> <MenuItem mnemonicParsing="false" text="Copy" /> <MenuItem mnemonicParsing="false" text="Paste" /> <MenuItem mnemonicParsing="false" text="Delete" /> <SeparatorMenuItem mnemonicParsing="false" /> <MenuItem mnemonicParsing="false" text="Select All" /> <MenuItem mnemonicParsing="false" text="Unselect All" /> </items> </Menu> <Menu mnemonicParsing="false" text="Help"> <items> <MenuItem mnemonicParsing="false" text="About MyHelloApp" /> </items> </Menu> </menus> </MenuBar> <AnchorPane maxHeight="-1.0" maxWidth="-1.0" prefHeight="-1.0" prefWidth="-1.0" VBox.vgrow="ALWAYS"> <children> <Label alignment="CENTER" layoutX="155.0" layoutY="177.0" style=" " textAlignment="CENTER" textFill="#9f9f9f" wrapText="false"> <font> <Font size="18.0" /> </font> </Label> <Button fx:id="button1" layoutX="14.0" layoutY="76.0" mnemonicParsing="false" onAction="#handleButtonAction" text="OK" textFill="#d7064c" /> <TextField fx:id="textFiled1" layoutX="14.0" layoutY="38.0" /> </children> </AnchorPane> </children> </VBox>

回答1件
あなたの回答
tips
プレビュー
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。
また依頼した内容が修正された場合は、修正依頼を取り消すようにしましょう。
2022/06/03 13:00
2022/06/04 12:21
2022/06/05 12:54