1. Main.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | package application; import javafx.application.Application; import javafx.stage.Stage; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.scene.layout.AnchorPane; import javafx.fxml.FXMLLoader; public class Main extends Application { @Override public void start(Stage primaryStage) { try { Parent root = FXMLLoader.load(getClass().getResource("Main.fxml")); Scene scene = new Scene(root); scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm()); primaryStage.setScene(scene); primaryStage.show(); } catch(Exception e) { e.printStackTrace(); } } public static void main(String[] args) { launch(args); } } | cs |
2. Main.fxml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | <?xml version="1.0" encoding="UTF-8"?> <?import javafx.scene.control.Label?> <?import javafx.scene.control.RadioButton?> <?import javafx.scene.control.ToggleGroup?> <?import javafx.scene.layout.AnchorPane?> <AnchorPane prefHeight="500.0" prefWidth="500.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.60" fx:controller="application.MainController"> <children> <RadioButton fx:id="rb1" layoutX="70.0" layoutY="75.0" mnemonicParsing="false" onAction="#radioSelect" selected="true" text="Male"> <toggleGroup> <ToggleGroup fx:id="gender" /> </toggleGroup> </RadioButton> <RadioButton fx:id="rb2" layoutX="70.0" layoutY="157.0" mnemonicParsing="false" onAction="#radioSelect" text="Female" toggleGroup="$gender" /> <Label fx:id="lbl" layoutX="109.0" layoutY="263.0" prefHeight="19.0" prefWidth="294.0" /> </children> </AnchorPane> | cs |
3. MainController.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | package application; import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.scene.control.Label; import javafx.scene.control.RadioButton; public class MainController { @FXML private RadioButton rb1; @FXML private RadioButton rb2; @FXML private Label lbl; public void radioSelect(ActionEvent event){ String message =""; if(rb1.isSelected()){ message += rb1.getText() + "\n"; } if(rb2.isSelected()){ message += rb2.getText() + "\n"; } lbl.setText(message); } } | cs |
'Java > JavaFx' 카테고리의 다른 글
JavaFX 24. DatePicker (0) | 2016.08.25 |
---|---|
JavaFX 23. TableView (1) | 2016.08.25 |
JavaFX 21. CheckBox (0) | 2016.08.25 |
JavaFX 20. Menu, MenuBar, MenuItem and SeparatorMenuItem (0) | 2016.08.25 |
JavaFX 19. Bidirectional Binding and using Slider (0) | 2016.08.25 |