0
在嘗試調用實現actionListener
的類中的方法時遇到問題。被調用的方法DataCompiler
需要使用wordCount
類中返回的整數wordCountWhole
。問題是我無法將所需參數傳遞給actionListener method
。調用ActionListener中的參數的方法
import javax.swing.*;
import java.awt.*;
import java.awt.List;
import java.awt.event.*;
import java.beans.PropertyChangeListener;
import java.text.BreakIterator;
import java.util.*;
import java.util.stream.IntStream;
public class GUI extends JFrame {
public JTextArea textInput;
public JButton dataButton;
public String str;
public GUI() {
super("Text Miner");
pack();
setLayout(null);
dataButton = new JButton("View Data"); //Button to take user to data table
dataButton.setSize(new Dimension(120, 50));
dataButton.setLocation(5, 5);
Handler event = new Handler(); //Adds an action listener to each button
dataButton.addActionListener(event);
add(dataButton);
public class wordCount {
public int miner() {
//This returns an integer called wordCountWhole
}
}
public class Handler implements Action { //All the possible actions for when an action is observed
public void action(ActionEvent event, int wordCountWhole) {
if (event.getSource() == graphButton) {
Graphs g = new Graphs();
g.Graphs();
} else if (event.getSource() == dataButton) {
DataCompiler dc = new DataCompiler();
dc.Data(wordCountWhole);
} else if (event.getSource() == enterButton) {
wordCount wc = new wordCount();
sentenceCount sc = new sentenceCount();
wc.miner();
sc.miner();
}
}
}
}
而這裏的DataCompiler類的代碼:
public class DataCompiler{
public void Data(int wordCountWhole){
int m = wordCountWhole;
System.out.println(m);
}
}
非常感謝您的回覆!我遵循你的建議,一切似乎都很好,直到運行該程序,然後它會拋出一個錯誤,告訴我「構造函數GUI.Handler()未定義」,所以我不確定我做錯了什麼。我只用Java編寫了大約兩個月的編程,所以我仍然在處理它。你想讓我發佈修改後的代碼嗎? – MDP503
我的答案的後半部分解釋說你不應該使用構造函數,因爲你不應該把整數傳遞給監聽器。 –
也許我應該更好地解釋一下,我需要這個整數來做一些數據分析,而DataCompiler類將比較不同的值並將它們打印到屏幕上,而不僅僅是原始整數。如果沒有將這個整數傳遞給actionlistener,我無法調用DataCompiler方法。對不起〜 – MDP503