user5699208
user5699208

Reputation:

Java: addActionListener(); not working

I'm trying to create a launcher for a game, let's say PowerTown. But when i try to create a ActionListener for the button, it says non-static variable cannot be referenced from a static context. How do I fix this?

package powertown;

import java.io.*;
import java.awt.*;
import java.awt.event.ActionEvent;

import javax.swing.*;

public class PowerTown {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) 
    implements ActionListener; {
    //Start Varibles
    String path = ClassLoader.getSystemClassLoader().getResource(".").getPath();
    if (!new File(path + "PowerTownSave.txt").exists()) {
        System.out.println("Creating save file...");
        File f = new File(path + "PowerTownSave.txt");
        f.getParentFile().mkdirs();
        try {
            f.createNewFile();
            try {
                BufferedWriter out = new BufferedWriter(new FileWriter(path + "PowerTownSave.txt"));
                out.write("Coals \n");
                out.newLine();
                out.write("1 \n");
                out.newLine();
                out.write("Oils \n");
                out.newLine();
                out.write("0 \n");
                out.newLine();
                out.write("Bios \n");
                out.newLine();
                out.write("0 \n");
                out.newLine();
                out.write("Winds \n");
                out.newLine();
                out.write("0 \n");
                out.newLine();
                out.write("Sols \n");
                out.newLine();
                out.write("0 \n");
                out.newLine();
                out.write("Nukes \n");
                out.newLine();
                out.write("0 \n");
                out.newLine();
                out.write("Money \n");
                out.newLine();
                out.write("50 \n");
                out.newLine();
                out.write("Power \n");
                out.newLine();
                out.write("0 \n");
                out.newLine();
                out.write("Customers \n");
                out.newLine();
                out.write("5 \n");
                out.newLine();
                out.write("Storage \n");
                out.newLine();
                out.write("10 \n");
                out.newLine();
                out.write("AvgUse \n");
                out.newLine();
                out.write("1 \n");
                out.newLine();
                out.write("Time \n");
                out.newLine();
                out.write("0 \n");
                out.newLine();
                out.write("Reputation \n");
                out.newLine();
                out.write("0 \n");
                out.newLine();
                out.write("Protests \n");
                out.newLine();
                out.write("0 \n");
                out.newLine();
                out.write("Coal Supply \n");
                out.newLine();
                out.write("800 \n");
                out.newLine();
                out.write("Coal Demand \n");
                out.newLine();
                out.write("30 \n");
                out.newLine();
                out.write("Oil Supply \n");
                out.newLine();
                out.write("100 \n");
                out.newLine();
                out.write("Oil Demand \n");
                out.newLine();
                out.write("9 \n");
                out.newLine();
                out.write("Bio Supply \n");
                out.newLine();
                out.write("7 \n");
                out.newLine();
                out.write("Bio Demand \n");
                out.newLine();
                out.write("3 \n");
                out.newLine();
                out.write("Nukes Supply \n");
                out.newLine();
                out.write("5 \n");
                out.newLine();
                out.write("Nukes Demand \n");
                out.newLine();
                out.write("3 \n");
                out.newLine();
                out.write("Disaster probrobility \n");
                out.newLine();
                out.write("0");
                out.newLine();
                out.close();
            } catch (IOException e) {
                System.out.println("Failed to create save file, check your permisions. Are you admin? Are you root?");
            }
        } catch (IOException e) {
            System.out.println("Failed to create save file, check your permisions. Are you admin? Are you root?");
            return;
        }
    } else {
        System.out.println("Save file found!");
    }
    System.out.println("Current readout of file will proceed, if file is empty, go to " + path + " , and then delete that file. After that, rerun the program.");
    try (BufferedReader br = new BufferedReader(new FileReader(path + "PowerTownSave.txt"))) {
        StringBuilder sb = new StringBuilder();
        String line = br.readLine();

        while (line != null) {
            sb.append(line);
            sb.append(System.lineSeparator());
            line = br.readLine();
        }
        String fileReadout = sb.toString();
        System.out.println(fileReadout);
    } catch (IOException e) {

    }
    System.out.println("Startup save pickup has finished.");
    System.out.println("Attemptng to create a launch window... Please wait.");
    JFrame frame = new JFrame("PwrTwn Launcher");
    System.out.println("Created new frame!");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    System.out.println("Set to terminate when closed...");
    JButton startButton = new JButton("Launch");
    startButton.addActionListener(this);
    System.out.println("Created new JButton!");
    startButton.setVerticalTextPosition(AbstractButton.BOTTOM);
    startButton.setHorizontalTextPosition(AbstractButton.CENTER);
    System.out.println("Set alignment for JButton!");
    frame.setPreferredSize(new Dimension(300, 150));
    System.out.println("Set Frame size!");
    startButton.setSize(new Dimension(60, 20));
    System.out.println("Set JButton size!");
    frame.getContentPane().add(startButton, BorderLayout.CENTER);
    System.out.println("Packaging...");
    frame.pack();
    frame.setVisible(true);
    System.out.println("Launch window created!");
}

public void actionPerformed(ActionEvent e) {
//some witty stuff goes here
    }
 }

Upvotes: 1

Views: 604

Answers (2)

YoungHobbit
YoungHobbit

Reputation: 13402

You need to import import java.awt.event.ActionListener;

public static void main(String[] args) implements ActionListener; { 

Methods don't implement interface, class does. So fix it like:

public class PowerTown implements ActionListener {


startButton.addActionListener(this);

this can not be used in a static method. You are using this reference in the main method. Instead you can create the instance of the class and pass it. Like below.

PowerTown powerTown = new PowerTown();
startButton.addActionListener(powerTown);

Upvotes: 1

Jack
Jack

Reputation: 133669

You are trying to implement an interface on a method. This can't be done and doesn't make any sense (as a method can't contain other methods).

You must use a class as the base class for your ActionListener, eg:

class PowerTown implements ActionListener {
  @Override public void actionPerformed(ActionEvent event) {
    ...
  }

  public static void main(String[] args) {
    PowerTown listener = new PowerTown();
    startButton.addActionListener(listener);
  }
}

But this is a strange design, the best solution would be to extend directly JFrame and embed the behavior inside it:

class PowerTownFrame extends JFrame implements ActionListener {
  public PowerTownFrame() {
    JButton button = new JButton("test");
    button.addActionListener(this);
    ...
  }

  @Override public void actionPerformed(ActionEvent event) {
    ...
  }

  public static void main(String[] args) {
    PowerTownFrame frame = new PowerTownFrame();
    frame.setVisible(true);   
  }
}

Upvotes: 1

Related Questions