1109104 2023-05-12 12:10 采纳率: 33.3%
浏览 11

JavaFX程序单选钮中挑选颜色

编写一个JavaFX程序,允许用户从5个单选钮中挑选一种颜色,在一个正方形中显示相应的颜色。

img

想请教为什么我运行出来是白板,什么都没有

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);    
    }
}

```.
  • 写回答

2条回答 默认 最新

  • 瞬间的未来式 2023-05-12 13:54
    关注

    img


    你这语法错的有点多,涉及到构造函数,局部变量 还有继承多看看,这个类代码替换一下就可以了

    
    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 ColorOptionsPane() {
            super();
            colorBox = new Rectangle(100, 100, Color.RED);
            StackPane colorPane = new StackPane(colorBox);
            colorPane.setPrefSize(300, 100);
    
            //放到一个组里
            ToggleGroup group = new ToggleGroup();
    
            //设置红色单选钮
            redButton = new RadioButton("Red");
            redButton.setToggleGroup(group);
            redButton.setSelected(true);
            redButton.setOnAction(this::processRadioButtonAction);
            //设置绿色单选钮
            greenButton = new RadioButton("Green");
            greenButton.setToggleGroup(group);
            greenButton.setSelected(true);
            greenButton.setOnAction(this::processRadioButtonAction);
            //设置橘色单选钮
            orangeButton = new RadioButton("Orange");
            orangeButton.setToggleGroup(group);
            orangeButton.setSelected(true);
            orangeButton.setOnAction(this::processRadioButtonAction);
            //设置黄色单选钮
            yellowButton = new RadioButton("Yellow");
            yellowButton.setToggleGroup(group);
            yellowButton.setSelected(true);
            yellowButton.setOnAction(this::processRadioButtonAction);
            //设置蓝色单选钮
            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);
        }
    }
    
    
    
    评论

报告相同问题?

问题事件

  • 修改了问题 5月12日
  • 创建了问题 5月12日

悬赏问题

  • ¥15 带序列特征的多输出预测模型
  • ¥15 VB.NET读取电脑主板序列号
  • ¥15 Python 如何安装 distutils模块
  • ¥15 关于#网络#的问题:网络是从楼上引一根网线下来,接了2台傻瓜交换机,也更换了ip还是不行
  • ¥15 资源泄露软件闪退怎么解决?
  • ¥15 CCF-CSP 2023 第三题 解压缩(50%)
  • ¥30 comfyui openpose报错
  • ¥20 Wpf Datarid单元格闪烁效果的实现
  • ¥15 图像分割、图像边缘提取
  • ¥15 sqlserver执行存储过程报错