2016-04-07 63 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(或者其他任何我知道的語言)的工作原理。 –

回答

相關問題