Java 如何访问类';之外的变量;范围

Java 如何访问类';之外的变量;范围,java,button,javafx,scope,overriding,Java,Button,Javafx,Scope,Overriding,我正在尝试编写一个JavaFX数独程序,用户可以通过以下方式填充单元格: 单击九个按钮中的一个(对应于1-9) 单击其中一个单元格 代码的工作方式是,单击按钮时,对应于该按钮的int被分配给selectedNum。单击单元格时,单元格会将其文本分配给selectedNum。然而,这就是问题所在——在单元构造函数中实现setOnAction函数单元格无法访问selectedNum。当selectedNum是一个超出其类范围的变量时,如何传递它 单元类: import javafx.event.Ev

我正在尝试编写一个JavaFX数独程序,用户可以通过以下方式填充单元格:

  • 单击九个按钮中的一个(对应于1-9)
  • 单击其中一个单元格
  • 代码的工作方式是,单击按钮时,对应于该按钮的int被分配给
    selectedNum
    。单击单元格时,单元格会将其文本分配给
    selectedNum
    。然而,这就是问题所在——在
    单元
    构造函数中实现setOnAction函数<代码>单元格无法访问
    selectedNum
    。当selectedNum是一个超出其类范围的变量时,如何传递它

    单元类:

    import javafx.event.EventHandler;
    import javafx.scene.Node;
    import javafx.scene.input.MouseEvent;
    import javafx.scene.layout.StackPane;
    import javafx.scene.paint.Color;
    import javafx.scene.shape.Rectangle;
    import javafx.scene.text.Font;
    import javafx.scene.text.Text;
    
    /**
     * The Cell class inherits StackPane, representing one of the cells in Sudoku.
     * @author rkuni
     *
     */
    public class Cell extends StackPane{
        
        private Text text; //displays the number inside the cell
        private Rectangle rect; //visual element for the cell
        
        /**
         * Default Constructor for Cell
         */
        public Cell() {
            text = new Text("");
            text.setFont(Font.font(30));
            rect = new Rectangle(50, 50);
            rect.setStyle("-fx-fill: white; -fx-stroke: black; -fx-stroke-width: 1;");
            this.getChildren().add(rect);
            this.getChildren().add(text);
            
            this.setOnMouseClicked(new EventHandler<MouseEvent>() { 
                @Override
                public void handle(MouseEvent t) {
                    text.setText("" + 1); //when clicked, I want to set text to the number that is selected.
                }
            });
        }
    }
    
    导入javafx.event.EventHandler;
    导入javafx.scene.Node;
    导入javafx.scene.input.MouseEvent;
    导入javafx.scene.layout.StackPane;
    导入javafx.scene.paint.Color;
    导入javafx.scene.shape.Rectangle;
    导入javafx.scene.text.Font;
    导入javafx.scene.text.text;
    /**
    *Cell类继承StackPane,表示数独中的一个单元格。
    *@作者rkuni
    *
    */
    公共类单元格扩展StackPane{
    private Text;//显示单元格内的数字
    私有矩形rect;//单元格的可视元素
    /**
    *单元格的默认构造函数
    */
    公共单元格(){
    文本=新文本(“”);
    text.setFont(Font.Font(30));
    rect=新矩形(50,50);
    矩形设置样式(“-fx填充:白色;-fx笔划:黑色;-fx笔划宽度:1;”);
    this.getChildren().add(rect);
    this.getChildren().add(文本);
    this.setOnMouseClicked(新的EventHandler(){
    @凌驾
    公共无效句柄(MouseEvent t){
    text.setText(“+1);//单击时,我想将文本设置为所选的数字。
    }
    });
    }
    }
    
    主类

    import javafx.application.Application;
    import javafx.event.ActionEvent;
    import javafx.event.EventHandler;
    import javafx.stage.Stage;
    import javafx.scene.Group;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.layout.BorderPane;
    import javafx.scene.text.Font;
    
    public class Main extends Application {
        
        private int selectedNum = 0;
        
        @Override
        public void start(Stage primaryStage) {
            try {
                
                Group root = new Group();
                Cell cell1 = new Cell();
                cell1.setLayoutX(100);
                cell1.setLayoutY(100);
                root.getChildren().add(cell1);
                
                Button numSelection[] = new Button[9];
                for (int i = 0; i < 9; i++) {
                    final int j = i; 
                    numSelection[i] = new Button("" + (i + 1));
                    numSelection[i].setLayoutY((i / 3) * 55 + 200);
                    numSelection[i].setLayoutX((i % 3) * 55 + 630);
                    numSelection[i].setMinHeight(50);
                    numSelection[i].setMinWidth(50);
                    numSelection[i].setFont(Font.font(20));
                    root.getChildren().add(numSelection[i]);
    
                    numSelection[i].setOnAction(new EventHandler<ActionEvent>() {
                        @Override
                        public void handle(ActionEvent arg0) {
                            selectedNum = j + 1;
                        }
                    });
                }
                
                Scene scene = new Scene(root,900,500);
                primaryStage.setScene(scene);
                primaryStage.show();
                
            } catch(Exception e) {
                e.printStackTrace();
            }
        }
        
        public static void main(String[] args) {
            launch(args);
        }
    }
    
    
    导入javafx.application.application;
    导入javafx.event.ActionEvent;
    导入javafx.event.EventHandler;
    导入javafx.stage.stage;
    导入javafx.scene.Group;
    导入javafx.scene.scene;
    导入javafx.scene.control.Button;
    导入javafx.scene.layout.BorderPane;
    导入javafx.scene.text.Font;
    公共类主扩展应用程序{
    private int selectedNum=0;
    @凌驾
    公共无效开始(阶段primaryStage){
    试一试{
    组根=新组();
    Cell cell1=新的Cell();
    cell1.setLayoutX(100);
    细胞1.setLayoutY(100);
    root.getChildren().add(cell1);
    按钮数选择[]=新按钮[9];
    对于(int i=0;i<9;i++){
    最终int j=i;
    numSelection[i]=新按钮(“+(i+1));
    numSelection[i].setLayoutY((i/3)*55+200);
    numSelection[i].setLayoutX((i%3)*55+630);
    numSelection[i].setMinHeight(50);
    numSelection[i].setMinWidth(50);
    numSelection[i].setFont(Font.Font(20));
    root.getChildren().add(numSelection[i]);
    numSelection[i].setOnAction(新的EventHandler(){
    @凌驾
    公共无效句柄(ActionEvent arg0){
    selectedNum=j+1;
    }
    });
    }
    场景=新场景(根,900500);
    初级阶段。场景(场景);
    primaryStage.show();
    }捕获(例外e){
    e、 printStackTrace();
    }
    }
    公共静态void main(字符串[]args){
    发射(args);
    }
    }
    
    潜在解决方案

    import javafx.application.Application;
    import javafx.event.ActionEvent;
    import javafx.event.EventHandler;
    import javafx.stage.Stage;
    import javafx.scene.Group;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.layout.BorderPane;
    import javafx.scene.text.Font;
    
    public class Main extends Application {
        
        private int selectedNum = 0;
        
        @Override
        public void start(Stage primaryStage) {
            try {
                
                Group root = new Group();
                Cell cell1 = new Cell();
                cell1.setLayoutX(100);
                cell1.setLayoutY(100);
                root.getChildren().add(cell1);
                
                Button numSelection[] = new Button[9];
                for (int i = 0; i < 9; i++) {
                    final int j = i; 
                    numSelection[i] = new Button("" + (i + 1));
                    numSelection[i].setLayoutY((i / 3) * 55 + 200);
                    numSelection[i].setLayoutX((i % 3) * 55 + 630);
                    numSelection[i].setMinHeight(50);
                    numSelection[i].setMinWidth(50);
                    numSelection[i].setFont(Font.font(20));
                    root.getChildren().add(numSelection[i]);
    
                    numSelection[i].setOnAction(new EventHandler<ActionEvent>() {
                        @Override
                        public void handle(ActionEvent arg0) {
                            selectedNum = j + 1;
                        }
                    });
                }
                
                Scene scene = new Scene(root,900,500);
                primaryStage.setScene(scene);
                primaryStage.show();
                
            } catch(Exception e) {
                e.printStackTrace();
            }
        }
        
        public static void main(String[] args) {
            launch(args);
        }
    }
    
    
    一个潜在的解决方案是根本不需要Cell类,代码就可以工作了。问题不再存在,因为所有变量都在同一范围内。但是,我希望保留Cell类,因为它有助于更有序的代码。无论如何,这是:

    import javafx.application.Application;
    import javafx.event.ActionEvent;
    import javafx.event.EventHandler;
    import javafx.stage.Stage;
    import javafx.scene.Group;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.input.MouseEvent;
    import javafx.scene.layout.StackPane;
    import javafx.scene.paint.Color;
    import javafx.scene.shape.Rectangle;
    import javafx.scene.text.Font;
    import javafx.scene.text.Text;
    
    
    public class Main extends Application {
        
        private int selectedNum; //will take the value of the last pressed button (1-9)
        
        @Override
        public void start(Stage primaryStage) {
            try {
                Group root = new Group();
                
                //setting up a stackpane that will contain a rect and text, making for a "cell"
                StackPane stack1 = new StackPane();
                Text text1 = new Text("");
                Rectangle rect1 = new Rectangle(50,50);
                rect1.setStyle("-fx-fill: white; -fx-stroke: black; -fx-stroke-width: 1;");
                stack1.getChildren().add(text1);
                stack1.getChildren().add(rect1);
                stack1.setLayoutX(100);
                stack1.setLayoutY(100);
                stack1.setOnMouseClicked(new EventHandler<MouseEvent>() { 
                    @Override
                    public void handle(MouseEvent t) {
                        text1.setText("" + selectedNum); //when clicked, set text to the number that is selected.
                    }
                });
                
                //setting up the buttons that will designate which number will go into the cell.
                Button numButtons[] = new Button[9];
                for (int i = 0; i < 9; i++) {
                    final int j = i;
                    numButtons[i] = new Button("" + (i + 1));
                    numButtons[i].setLayoutY((i / 3) * 55 + 200);
                    numButtons[i].setLayoutX((i % 3) * 55 + 630);
                    numButtons[i].setMinHeight(50);
                    numButtons[i].setMinWidth(50);
                    numButtons[i].setFont(Font.font(20));
                    root.getChildren().add(numButtons[i]);
    
                    numButtons[i].setOnAction(new EventHandler<ActionEvent>() {
                        @Override
                        public void handle(ActionEvent arg0) {
                            selectedNum = j + 1;
                        }
                    });
                }
                
                text1.toFront();
                root.getChildren().add(stack1);
                Scene scene = new Scene(root,900,500);
                primaryStage.setScene(scene);
                primaryStage.show();
                
            } catch(Exception e) {
                e.printStackTrace();
            }
        }
        
        public static void main(String[] args) {
            launch(args);
        }
    }
    
    导入javafx.application.application;
    导入javafx.event.ActionEvent;
    导入javafx.event.EventHandler;
    导入javafx.stage.stage;
    导入javafx.scene.Group;
    导入javafx.scene.scene;
    导入javafx.scene.control.Button;
    导入javafx.scene.input.MouseEvent;
    导入javafx.scene.layout.StackPane;
    导入javafx.scene.paint.Color;
    导入javafx.scene.shape.Rectangle;
    导入javafx.scene.text.Font;
    导入javafx.scene.text.text;
    公共类主扩展应用程序{
    private int selectedNum;//将获取上次按下按钮的值(1-9)
    @凌驾
    公共无效开始(阶段primaryStage){
    试一试{
    组根=新组();
    //设置包含rect和文本的堆栈窗格,使其成为“单元格”
    StackPane stack1=新的StackPane();
    Text text1=新文本(“”);
    矩形rect1=新矩形(50,50);
    rect1.setStyle(“-fx填充:白色;-fx笔划:黑色;-fx笔划宽度:1;”);
    stack1.getChildren().add(text1);
    stack1.getChildren().add(rect1);
    stack1.setLayoutX(100);
    stack1.setLayoutY(100);
    stack1.setOnMouseClicked(新的EventHandler(){
    @凌驾
    公共无效句柄(MouseEvent t){
    text1.setText(“+selectedNum);//单击时,将文本设置为所选的数字。
    }
    });
    //设置按钮,指定哪个号码将进入单元格。
    按钮编号按钮[]=新按钮[9];
    对于(int i=0;i<9;i++){
    最终int j=i;
    numButtons[i]=新按钮(“+(i+1));
    n个按钮[i].设置布局((i/3)*55+200);
    numButtons[i].setLayoutX((i%3)*55+630);
    numButtons[i].设置最小高度(50);
    麻木
    
    public class Cell extends StackPane{
    
        private final Model model ;
        
        private Text text; //displays the number inside the cell
        private Rectangle rect; //visual element for the cell
        
        /**
         * Default Constructor for Cell
         */
        public Cell(Model model) {
            this.model = model ;
            text = new Text("");
            text.setFont(Font.font(30));
            rect = new Rectangle(50, 50);
            rect.setStyle("-fx-fill: white; -fx-stroke: black; -fx-stroke-width: 1;");
            this.getChildren().add(rect);
            this.getChildren().add(text);
            
            this.setOnMouseClicked(new EventHandler<MouseEvent>() { 
                @Override
                public void handle(MouseEvent t) {
                    text.setText("" + model.getCurrentlySelectedValue()); 
                }
            });
        }
    }
    
    public class Main extends Application {
        
        private int selectedNum = 0;
        
        @Override
        public void start(Stage primaryStage) {
    
            Model model = new Model();
    
            try {
                
                Group root = new Group();
                Cell cell1 = new Cell(model);
                cell1.setLayoutX(100);
                cell1.setLayoutY(100);
                root.getChildren().add(cell1);
                
                Button numSelection[] = new Button[9];
                for (int i = 0; i < 9; i++) {
                    final int j = i + 1; 
                    numSelection[i] = new Button("" + j);
                    numSelection[i].setLayoutY((i / 3) * 55 + 200);
                    numSelection[i].setLayoutX((i % 3) * 55 + 630);
                    numSelection[i].setMinHeight(50);
                    numSelection[i].setMinWidth(50);
                    numSelection[i].setFont(Font.font(20));
                    root.getChildren().add(numSelection[i]);
    
                    numSelection[i].setOnAction(new EventHandler<ActionEvent>() {
                        @Override
                        public void handle(ActionEvent arg0) {
                            model.setCurrentlySelectedValue(j);
                        }
                    });
                }
                
                Scene scene = new Scene(root,900,500);
                primaryStage.setScene(scene);
                primaryStage.show();
                
            } catch(Exception e) {
                e.printStackTrace();
            }
        }
        
        public static void main(String[] args) {
            launch(args);
        }
    }