RadioButton更改Java中的背景颜色

问题描述 投票:1回答:1

伙计们,我只是试图创建3个单选按钮,这些按钮将更改JavaFX中的背景颜色。而且我被卡住了,在updateBackGround()方法中,特别是在“ pane1.setBackground(new ....)”中出现了此错误“找不到符号”。

Java找不到“ pane1”符号。请帮我。非常感谢。下面是我的代码。

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import javafx.scene.paint.Color;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.CornerRadii;

public class Project2 extends Application
{
   public RadioButton RedButton;
   public RadioButton BlueButton;
   public RadioButton GreenButton;

   public static void main(String[] args) {
      launch(args);
   }

   public void start(Stage primaryStage)
   {
      Pane myPane1 = pane1();   
      Scene scene1 = new Scene(myPane1);    
      primaryStage.setScene(scene1);    
      primaryStage.setTitle("HAI VO");
      primaryStage.show();
   }

   public Pane pane1()
   {
     ToggleGroup group1 = new ToggleGroup();

     RedButton = new RadioButton("RED");
     RedButton.setToggleGroup(group1);
     RedButton.setOnAction(event -> updateBackGround());

     BlueButton = new RadioButton("BLUE");
     BlueButton.setToggleGroup(group1);
     BlueButton.setOnAction(event -> updateBackGround());

     BlueButton.setSelected(true);

     GreenButton = new RadioButton("Green");
     GreenButton.setToggleGroup(group1);
     GreenButton.setOnAction(event -> updateBackGround());

     GridPane pane1 = new GridPane();

     pane1.add(RedButton,5,0);
     pane1.add(GreenButton,10,0);
     pane1.add(BlueButton,20,0);

     pane1.setHgap(5);
     pane1.setVgap(5);

     pane1.setPadding(new Insets(20,20,20,20));

     updateBackGround();
     return pane1;
   } 

   public void updateBackGround()
   {
      if (RedButton.isSelected()) {
          pane1.setBackground(new Background(new BackgroundFil(Color.RED,CornerRadii.EMPTY, Insets.EMPTY)));
      }

      if (BlueButton.isSelected()) {
          pane1.setBackground(new Background(new BackgroundFill(Color.BLUE,CornerRadii.EMPTY, Insets.EMPTY)));
      }

      if (GreenButton.isSelected()) {
          pane1.setBackground(new Background(new BackgroundFill(Color.GREEN,CornerRadii.EMPTY, Insets.EMPTY)));        
      }

   }
}
`

伙计们,我只是试图创建3个单选按钮,这些按钮将更改JavaFX中的背景颜色。而且我陷入困境,在updateBackGround()方法中,特别是...

java javafx background-color
1个回答
3
投票

[您同时具有返回pane1()的方法Pane,并且在该方法中,您具有作为pane1的局部变量GridPane

© www.soinside.com 2019 - 2024. All rights reserved.