![](/img/trans.png)
[英]How do I get information of the variables from one class to another Action Listener class?
[英]How do I use a directory path variable from an Action Listener from one class in another?
我是編程的新手,我就像一個白痴,決定讓我的第一個項目超越我的水平。 我還沒有成功嘗試在網站上找到解決這個問題的方法,所以我問。
所以,我有一個按鈕,用於創建一個帶有用戶輸入名稱的文件,然后創建包含該文件的目錄。 它在課堂“NewProject”中。
JButton btnNewButton = new JButton("Create");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String projectName = textPane.getText();
File projectFolder = new File("C:\\Jibberish\\" + projectName);
File projectStart = new File("C:\\Jibberish\\" + projectName +
"\\" + "Project" + "\\" + "text.rtf");
現在,在另一個類“工作區”中,我有一個JTree和一個JEditorPane。 我想知道如何在“workspace”類中獲取類似“projectStart”的變量,這樣我就可以將該目錄用作JTree的模型,將文件“text.rtf”用作JEditor中的默認文本。
如果需要更多信息,我會盡力提供。 請回答,好像我什么都不知道,因為我沒有。 提前致謝。
不確定我是否正確使用你,但是為了將projectStart交給workspace.class你可以創建一個私有的類變量並為它創建一個getter方法。
private File projectStart = null;
private void buttonAction(){ //this is where your ActionListener stuff happens
JButton btnNewButton = new JButton("Create");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String projectName = textPane.getText();
File projectFolder = new File("C:\\Jibberish\\" + projectName);
projectStart = new File("C:\\Jibberish\\" + projectName +
"\\" + "Project" + "\\" + "text.rtf");
}
public File getProjectStart(){ //this method makes your projectStart-variable accessible for other classes
return projectStart;
}
在您的工作空間類中,您可以通過調用它來使用此變量,類似於:
private void foo(){
NewProject np = new NewProject();
File copyOfProjectStart = np.getProjectStart();
}
希望這對你有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.