编写一个JavaFX程序,允许用户从5个单选钮中挑选一种颜色,在一个正方形中显示相应的颜色。
想请教为什么我运行出来是白板,什么都没有
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.control.RadioButton;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.layout.StackPane;
import javafx.scene.text.Text;
import javafx.scene.text.Font;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class ColorOptions extends Application
{
public void start(Stage primaryStage)
{
ColorOptionsPane pane = new ColorOptionsPane();
pane.setAlignment(Pos.CENTER);
pane.setStyle("-fx-background-color:white");
Scene scene = new Scene(pane,500,150);
primaryStage.setTitle("Color Options");
primaryStage.setScene(scene);
primaryStage.show();
}
}
import javafx.event.ActionEvent;
import javafx.geometry.Pos;
import javafx.scene.control.RadioButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.layout.StackPane;
import javafx.scene.text.Text;
import javafx.scene.text.Font;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class ColorOptionsPane extends HBox
{
//private rectangle color;
private RadioButton redButton,greenButton,orangeButton,yellowButton,blueBUtton;
private Rectangle colorBox;
public void ColorOptionPane()
{
colorBox = new Rectangle(100, 100, Color.RED);
StackPane colorPane=new StackPane(colorBox);
colorPane.setPrefSize(300,100);
//放到一个组里
ToggleGroup group = new ToggleGroup();
//设置红色单选钮
RadioButton redButton = new RadioButton("Red");
redButton.setToggleGroup(group);
redButton.setSelected(true);
redButton.setOnAction(this::processRadioButtonAction);
//设置绿色单选钮
RadioButton greenButton = new RadioButton("Green");
greenButton.setToggleGroup(group);
greenButton.setSelected(true);
greenButton.setOnAction(this::processRadioButtonAction);
//设置橘色单选钮
RadioButton orangeButton = new RadioButton("Orange");
orangeButton.setToggleGroup(group);
orangeButton.setSelected(true);
orangeButton.setOnAction(this::processRadioButtonAction);
//设置黄色单选钮
RadioButton yellowButton = new RadioButton("Yellow");
yellowButton.setToggleGroup(group);
yellowButton.setSelected(true);
yellowButton.setOnAction(this::processRadioButtonAction);
//设置蓝色单选钮
RadioButton blueButton = new RadioButton("Blue");
blueButton.setToggleGroup(group);
blueButton.setSelected(true);
blueButton.setOnAction(this::processRadioButtonAction);
VBox options = new VBox(redButton,greenButton,orangeButton,yellowButton,blueBUtton);
options.setAlignment(Pos.CENTER_LEFT);
options.setSpacing(10);
setSpacing(20);
getChildren().addAll(options,colorBox);
}
public void processRadioButtonAction(ActionEvent event)
{
if(redButton.isSelected())
colorBox.setFill(Color.RED);
else if (greenButton.isSelected())
colorBox.setFill(Color.GREEN);
else if (orangeButton.isSelected())
colorBox.setFill(Color.ORANGE);
else if (yellowButton.isSelected())
colorBox.setFill(Color.YELLOW);
else
colorBox.setFill(Color.BLUE);
}
}
```.