2013-08-06 70 views
1

一個外行人關於變量定義和使用的問題:如何在java的其他類中使用公共類中定義的變量?

我需要製作一個獲取用戶輸入並將其存儲在文本文件中的Java GUI。然而,這個寫作必須在一個Actionlistener類中完成(即用戶點擊按鈕並創建並存儲文本文件)。這意味着我必須在一個類(public class)中定義一個變量,並在另一個類(定義Actionlistener的類)中使用它。

我該怎麼做?全局變量是唯一的方法嗎?

在我的代碼中,我首先將'textfield'定義爲JTextField,然後我希望它被讀取(作爲'文本')並存儲('text.txt')。

import javax.swing.*; 
//... 
import java.io.BufferedWriter; 

public class Runcommand33 
{ 
    public static void main(String[] args) 
    { 
final JFrame frame = new JFrame("Change Backlight"); 
    // ... 
    // define frames, panels, buttons and positions 
    JTextField textfield = new JTextField();textfield.setBounds(35,20,160,30); 
    panel.add(textfield); 
    frame.setVisible(true); 
    button.addActionListener(new ButtonHandler()); 
    } 
} 

    class ButtonHandler implements ActionListener{ 
    public void actionPerformed(ActionEvent event){ 
    String text = textfield.getText(); 
     textfield.setText(""); 
     new BufferedWriter(new FileWriter("text.txt")).write(text).newLine().close(); 

    // Afterwards 'text' is needed to run a command 
       } 
      } 

我編譯時得到

Runcommand33.java:45: error: cannot find symbol 
       String text = textfield.getText(); 
          ^
    symbol: variable textfield 
    location: class ButtonHandler 

不用線路字符串文本=新的BufferedWriter代碼編譯。

請注意,我已經試過這 Get variable in other classes 和建議,這 How do I access a variable of one class in the function of another class? 但他們沒有工作。

有什麼建議嗎?

回答

1

讓我們來看看這個從設計的角度來看:ButtonHandler聽起來有點太普通。點擊「處理」按鈕的方式是什麼?嗯,它將文本字段的內容保存到一個文件中,所以它應該被稱爲「TextFieldSaver」(或者最好不要蹩腳)。

現在,TextFieldSaver需要有一個文本字段來保存,是嗎?因此,通過構造函數添加一個成員變量來保存文本字段,並通過在主類創建的文本字段:

button.addActionListener(new TextFieldSaver(textfield)); 

.... 

class TextFieldSaver implements ActionListener { 
    JTextField textfield; 
    public TextFieldSaver(JTextField toBeSaved) { 
     textfield = toBeSaved; 
    } 
    public void actionPerformed(ActionEvent event) { 
     String text = textfield.getText(); 
     textfield.setText(""); 
     new BufferedWriter(new FileWriter("text.txt")).write(text).newLine().close(); 
    } 
} 

這是不這樣做的唯一方法,也不一定是最好的方式,但我希望它表明如何使用專有名稱有時顯示出路。

+0

哇它的工作!非常感謝。確實有用的評論。儘管現在我更加困惑:(讓我回顧一下:我在類A中定義了一個變量。我使用這個變量設置了一個構造函數。變量在類B中獲取它的值,然後返回到類A.對?燦爛。現在解釋一下:A.ActionListener {JTextField textfield; ...}和 B. textfield = toBeSaved; 的意思是?我們必須定義多少次'textfield'?爲什麼在我的程序的原始版本中,我的B類不會識別變量'textfield'?再次感謝。 –

+0

這些應該是他們自己的問題,但是:A是接收參數的構造函數。 B是一個變量賦值。 C變量被定義爲局部變量,因此它的可見性僅限於它聲明的塊。這是非常基本的東西,讀一本關於Java編程的書可能是一個好主意。 – Joni

1

如何使用匿名內部類,使textfield變量final

button.addActionListener(new ActionListener() { 
    public void actionPerformed(ActionEvent event){ 
     String text = textfield.getText(); 
     textfield.setText(""); 
     new BufferedWriter(new FileWriter("text.txt")).write(text).newLine().close(); 

     // Afterwards 'text' is needed to run a command    
    } 
}); 

注意,你需要聲明textfieldfinal

final JTextField textfield = new JTextField(); 
0

沒有全局變量在Java中。每個班級可以有一些公共領域。和其他類可以訪問它們

你可以使用它們像這樣:

class A{ 
    public String text; 
} 

class B{ 
    public static void main(String []args){ 
     A a= new A(); 
     System.out.println(a.text); 
    } 
} 
+0

那麼爲什麼我的錯誤信息呢? –

+0

,因爲'textfield'是局部變量,您只能在該函數中使用它,而不是其他類,而不是其他類。 –

相關問題