单选按钮以在Java中更改背景颜色

huangapple go评论91阅读模式
英文:

RadioButton to change background color in Java

问题

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
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 BackgroundFill(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)));        
      }

   }
}
英文:

guys, I'm just trying to create 3 Radio Buttons that will change my background color in JavaFX. And I got stuck, I got this error "Symbols not found" in the updateBackGround() method, specifically the "pane1.setBackground(new...."

Java could not find the "pane1" symbol. Please help me. Thank you so much. Below is my code.

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)));        
}
}
}
`
</details>
# 答案1
**得分**: 3
你有一个名为`pane1()`的方法,它返回一个`Pane`对象,在该方法内部有一个名为`pane1`的局部变量,它是一个`GridPane`。
之后,在`updateBackground()`方法中,你引用了`pane1`,没有使用括号,看起来像是对类成员或局部变量的引用,但是在`updateBackground`中既没有类成员也没有名为`pane1`的局部变量。
`updateBackground`方法无法看到在`pane1()`方法内部声明的`pane1`变量。它也无法看到在`start()`方法中创建的`myPane1`,只能通过从`_setScene(scene1)_`中检索它才能访问到,该方法是在`scene1`中设置的。
这主要是关于Java的作用域规则,与JavaFX无关。
<details>
<summary>英文:</summary>
You have both a method `pane1()` which returns a `Pane`, and within that method you have a local variable `pane1` which is a `GridPane`.  
Later, in `updateBackground()` you reference `pane1` which, with no parentheses, looks like a reference to a class member or to a local variable — but there is neither a class member nor a variable within updateBackground named pane1.
updateBackground doesn&#39;t see the `pane1` variable declared within the `pane1()` method. It also doesn&#39;t see the `myPane1` Pane created within `start()`, which will only be accessible by retrieving it from `scene1` that is set within _setScene(scene1)_
This is mainly a matter of the scoping rules of Java, and is not specific to JavaFX.
</details>

huangapple
  • 本文由 发表于 2020年4月7日 07:42:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/61070621.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定