Java:addActionListener();不工作

时间:2015-12-25 04:55:03

标签: java actionlistener

我试图为游戏创建一个启动器,让我们说PowerTown。但是当我尝试为按钮创建一个ActionListener时,它表示无法从静态上下文引用非静态变量。我该如何解决这个问题?

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
    }
 }

2 个答案:

答案 0 :(得分:1)

您正尝试在方法上实施interface。这不可能做到并且没有任何意义(因为方法不能包含其他方法)。

您必须使用类作为ActionListener的基类,例如:

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

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

但这是一个奇怪的设计,最好的解决方案是直接扩展JFrame并将行为嵌入其中:

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);   
  }
}

答案 1 :(得分:1)

您需要导入import java.awt.event.ActionListener;

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

方法不实现接口,类做。所以解决它:

public class PowerTown implements ActionListener {

startButton.addActionListener(this);

this无法在static方法中使用。您在main方法中使用此引用。相反,您可以创建类的实例并传递它。如下所示。

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