繁体   English   中英

从控制器类javaFX Scene Builder内部创建类的实例

[英]Create instance of a class from inside controller class javaFX Scene Builder

我是新来的,完全是自学成才。

我有一个程序,该程序具有各种功能,这些功能可以更改数组中的值,然后通过Midi输出。

一切都进行得如此顺利,直到我决定学习Scene Builder。 这是一个简化的版本,突出显示了我用一个按钮创建GUI时遇到的问题。 每次按下该按钮时,我都需要将数组中大于零的数字的值增加一。 我的初始值是

0 1 0 1

按下按钮增加

0 2 0 2

按下按钮增加

0 3 0 3

按下按钮增加

0 4 0 4等

问题在于,在控制器类中访问主程序中的void时,它需要创建一个实例,因此,一次按下按钮后它不能超过0 2 02。我需要将所有数组保留在主程序类中因为那是所有Midi东西生活和工作的地方。 我为我的非技术性语言表示歉意感谢您在此处阅读以下两个相关的代码文件

package zump;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Zump extends Application {

int[] myNoteArray = {0, 1, 0, 1};

@Override
public void start(Stage stage) throws Exception {
    Parent root = FXMLLoader.load(getClass().getResource("zumpFXML.fxml"));
    Scene scene = new Scene(root);
    stage.setScene(scene);
    stage.show();
}

public static void main(String[] args) {
    System.out.println("Main");
    launch(args);
}

void octaveUp() {
    System.out.println("Octave UP");
    for (int i = 0; i < 4; i++) {
        int fa4 = myNoteArray[i];
        if (fa4 > 0 && fa4 < 4) {
            myNoteArray[i] = fa4 + 1;
        } // eo if } 
    }// eo for
    //Print Array//
    for (int i = 0; i < 4; i++) {
        int fa6 = myNoteArray[i];
        System.out.print(fa6);
        System.out.print(" ");
    }
    System.out.println("");
}

}

这是控制器

package zump;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;

public class zumpFXMLController {

    @FXML // ResourceBundle that was given to the FXMLLoader
    private ResourceBundle resources;
    @FXML // URL location of the FXML file that was given to the FXMLLoader
    private URL location;
    @FXML // fx:id="button"
    private Button button; // Value injected by FXMLLoader

    @FXML
    void handleButtonAction(ActionEvent event) {
    }

    @FXML // This method is called by the FXMLLoader when initialization is complete
    void initialize() {
        assert button != null : "fx:id=\"button\" was not injected: check your FXML file 'zumpFXML.fxml'.";
        button.setOnAction((ActionEvent event) -> {
            System.out.println("That was easy, wasn't it?");
            Zump t1 = new Zump();
            t1.octaveUp();
            // Zump.octaveUp();
        });
    }
}

您不应该在每次按下按钮时都创建一个实例,而应该只创建一次,然后在每次按下按钮时访问该实例。

因此,实例的创建应该在事件处理程序之外,但是创建的实例应该对事件处理程序可用。

例如,一种方法是将实例的创建移到一个级别之外:

@FXML // This method is called by the FXMLLoader when initialization is complete
void initialize() {
    assert button != null : "fx:id=\"button\" was not injected: check your FXML file 'zumpFXML.fxml'.";
    final Zump t1 = new Zump();
    button.setOnAction((ActionEvent event) -> {
        System.out.println("That was easy, wasn't it?");
        t1.octaveUp();
        // Zump.octaveUp();
    });
}

final在Java 8中不是必需的,但是它仍然有助于认识到这是在匿名类/ lambda表达式内部使用的变量)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM