簡體   English   中英

用Java將數據從一個類傳遞到另一個類

[英]Passing data from one class to another in Java

我試圖將輸出傳遞到文本區域。

我有Class Search ,它使用System.out.println()處理所有搜索並顯示輸出。

我創建了Class GUI ,以使控制台輸出(System.out.println())出現在JTextArea

我試圖使用對象將數據傳遞到文本區域,但我不知道為什么它不起作用。

Class Search有這個計算輸出的方法:

 public static void searchIndex(String searchString) throws IOException, ParseException 

Class GUI具有text1

在類GUI中我試過這個:

text1.setText(Search.searchIndex(searchString));

但它給我一個錯誤searchString cannot be resolved to a variable

有什么建議么 ?

問候。

該方法未返回:

public static void searchIndex(String searchString) throws IOException, ParseException {

需要將void更改為String並返回結果:

public static String searchIndex(String searchString) throws IOException, ParseException {
    //do search
    return resultString; 
}

為了以下工作:

text1.setText(Search.searchIndex(searchString));

searchString無法解析為變量

上述錯誤是由於您嘗試使用變量“searchString”而未聲明它而引起的。

String searchString = "Some string that is in the search index";
text1.setText(Search.searchIndex(searchString));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM