2016-04-07 62 views
0

我正在研究一个简单的对象数据库应用程序,该应用程序显示表中某个类的条目的各种属性,并允许用户通过辅助输入窗口添加新条目。JavaFX FXML应用程序中的奇怪可见性问题

主要:

public class Main extends Application { 

     Stage theStage; 
     Scene mainWindowController; 

     @Override 
     public void start(Stage primaryStage) throws Exception{ 
      theStage = primaryStage; 

      Parent root = FXMLLoader.load(getClass().getResource("MainWindow.fxml")); 
      primaryStage.setTitle("Steam Database"); 

      mainWindowController = new Scene(root, 800, 550); 

      primaryStage.setScene(mainWindowController); 
      primaryStage.show(); 
     } 

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

一种用于辅助窗口的一个控制器:

import static sample.MainWindowController.*; 

public class CreateDeveloperWindowController { 
/* 
*/ 
@FXML 
private TextField newDeveloperNameTextField; 

@FXML 
private TextField newDeveloperPassTextField; 

    private void handleCreateDeveloperButton() { 
     String proposedNewDevName = newDeveloperNameTextField.getText(); 
     String proposedNewDevPass = newDeveloperPassTextField.getText(); 
     if (proposedNewDevName.equals("") || proposedNewDevPass.equals("")) { 
      mainWindowController.textMessageDisplay.setText("Name and password must not be empty!"); 
     } else { 
      allDevelopers.add(new Developer(proposedNewDevName, proposedNewDevPass)); 
     } 

    } 
/* 
*/ 
} 

的问题是在控制器,在线路

mainWindowController.textMessageDisplay.setText("Name and password must not be empty!"); 

我越来越一个“无法解决符号”的错误,但我不明白为什么。一种解决方案是添加“主”。在它的前面并声明变量static,但这会为我想添加的其他功能创建问题。所以我的问题是:

  1. 为什么这甚至显示出来? mainWindowController变量在Main类中声明,所以它应该在应用程序中的任何位置都可见,就我所知。

  2. 我该如何解决这个问题;我如何获得该线路的工作?

+0

没有名为'mainWindowController'在'CreateDeveloperWindowController'定义的变量,所以你的错误。 –

+0

但是不应该CreateDeveloperWindowController在Main类中看到它?即使我导入它,它也会给出“不能从静态上下文中引用的非静态变量”错误,只能通过声明变量static来解决,这会导致我在问题中提到的问题 - 它不能成为静态,因为它阻碍了我要添加的其他内容 – Sargon1

+0

*“但是不应该在Main类中看到CreateDeveloperWindowController?”*不,它为什么会看到它?这根本不是Java(或者其他任何我知道的语言)的工作原理。 –

回答

相关问题