如何使用 JavaFX 建立單選按鈕?
按鈕是一個元件,在按下時執行操作(如提交、登入等)。它通常用文字或影像標記,指定相應操作。
單選按鈕是一種形狀為圓形的按鈕。它有兩種狀態,選中和未選中。通常,單選按鈕使用切換組進行分組,其中你只能選擇其中的一個。
你可以透過例項化 javafx.scene.control.RadioButton 類在 JavaFX 中建立一個單選按鈕,它 является的子類 ToggleButton 類。每當按下或釋放單選按鈕時都會生成操作。你可以使用 setToggleGroup() 方法將單選按鈕設定到一個組中。
示例
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.RadioButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class RadioButtonExample extends Application {
@Override
public void start(Stage stage) {
//Creating toggle buttons
RadioButton button1 = new RadioButton("Java");
RadioButton button2 = new RadioButton("Python");
RadioButton button3 = new RadioButton("C++");
//Toggle button group
ToggleGroup group = new ToggleGroup();
button1.setToggleGroup(group);
button2.setToggleGroup(group);
button3.setToggleGroup(group);
//Adding the toggle button to the pane
VBox box = new VBox(5);
box.setFillWidth(false);
box.setPadding(new Insets(5, 5, 5, 50));
box.getChildren().addAll(button1, button2, button3);
//Setting the stage
Scene scene = new Scene(box, 595, 150, Color.BEIGE);
stage.setTitle("Toggled Button Example");
stage.setScene(scene);
stage.show();
}
public static void main(String args[]){
launch(args);
}
}輸出

廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP