Hello, I'm trying to learn about java's event handlers and keep getting errors with type type (static/non-static) methods I create. Some code I'm trying to write looks like:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class Main extends JFrame implements ActionListener{
static private int[] intArray = new int[10000];
static private int numOfInts = 0;
static private int avg = 0;
public static void main(String[] args) {
//create main frame
JFrame frame = new JFrame();
frame.setTitle("Section V, question 2");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(350, 250);
frame.setLayout(new GridLayout(4, 1));
frame.setVisible(true);
//create instruction label and add to frame
Label instructions = new Label("Follow the instructions on the exam to use this program");
frame.add(instructions);
//create textfield for index entry and add to frame
JTextField indexEntry = new JTextField();
frame.add(indexEntry);
//create button for average and add to frame
JButton avgBtn = new JButton("Click for Average");
frame.add(avgBtn);
avgBtn.addActionListener(avgBtn);
//create panel to display results and add to frame
JPanel resultsPanel = new JPanel();
resultsPanel.setBackground(Color.BLUE);
frame.add(resultsPanel);
//read in from file
readFromFile();
//compute average
computeAverage();
System.out.println(avg);
}
static private void readFromFile(){
try{
// Open the file that is the first
// command line parameter
FileInputStream fstream = new FileInputStream("numbers.dat");
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
//Read File Line By Line
int i = 0;
while ((strLine = br.readLine()) != null) {
// Print the content on the console
System.out.println (strLine);
intArray[i] = Integer.parseInt(strLine);
numOfInts++;
i++;
}
//Close the input stream
in.close();
System.out.println ("numOfInts = " + numOfInts);
}
catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
static private void computeAverage(){
int sum = 0;
for(int i = 0; i < numOfInts; i++)
sum += intArray[i];
avg = sum/numOfInts;
//return avg;
}
public void actionPerformed(ActionEvent e){
if(e.getSource() == avgBtn){
computeAverage();
}
}
}
Which is supposed to setup a GUI read in some ints from a file and then compute their average when a button is pressed. However, I keep getting problems with the static/non-static stuff and the event handers. My current error are:
Main.java:35: addActionListener(java.awt.event.ActionListener) in javax.swing.AbstractButton cannot be applied to (javax.swing.JButton)
avgBtn.addActionListener(avgBtn);
Main.java:91: cannot find symbol
symbol : variable avgBtn
location: class Main
if(e.getSource() == avgBtn){
I understand that the compiler can't find avgBtn because its defined within another function (Main()), but can anyone shed some light on how to attach an event handler to it? tried 'this' to no avail also...Thanks in advance, and if you see anything else wrong I'd love to hear how I can make it better.