2

私は初心者のプログラマーで、アクション リスナーと GUI で問題が発生しています。これが私のコードです:

メインクラス--

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class prog extends JFrame {

//add instance variable to hold lockd
prog app = new prog();

//create buttons
JPanel row1 = new JPanel();
JButton oneLeft = new JButton("oneLeft");
JButton oneRight = new JButton("oneRight");

JPanel row2 = new JPanel();
JButton twoLeft = new JButton("twoLeft");
JButton twoRight = new JButton("twoRight");

JPanel row3 = new JPanel();
JButton threeLeft = new JButton("threeLeft");
JButton threeRight = new JButton("threeRight");


public prog() {
    super("Prog");
    setLookAndFeel();
    setSize(400, 800);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    GridLayout layout = new GridLayout(3, 2);
    setLayout(layout);

    //add Listeners
    oneLeft.addActionListener(app);
    oneRight.addActionListener(app);
    twoLeft.addActionListener(app);
    twoRight.addActionListener(app);
    threeLeft.addActionListener(app);
    threeRight.addActionListener(app);



    setVisible(true);
}

private void setLookAndFeel() {
    try {
        UIManager.setLookAndFeel("com.sun.java.plaf.");
    } catch (Exception e) {
        //ignore error
    }
}

public static void main(String[] args) {
    prog progApp = new prog();
}
}

このクラスでアクション リスナーを使用するとエラーが発生します。タイプ AbstractButton のメソッド addActionListener(ActionListener) は、引数 (prog) には適用できません。

完全なエラー:

Description Resource    Path    Location    Type
The method addActionListener(ActionListener) in the type AbstractButton is not applicable for the arguments (prog)  prog.java   /Experiment/src line 33 Java Problem
The method addActionListener(ActionListener) in the type AbstractButton is not applicable for the arguments (prog)  prog.java   /Experiment/src line 35 Java Problem
The method addActionListener(ActionListener) in the type AbstractButton is not applicable for the arguments (prog)  prog.java   /Experiment/src line 34 Java Problem
The method addActionListener(ActionListener) in the type AbstractButton is not applicable for the arguments (prog)  prog.java   /Experiment/src line 37 Java Problem
The method addActionListener(ActionListener) in the type AbstractButton is not applicable for the arguments (prog)  prog.java   /Experiment/src line 36 Java Problem
The method addActionListener(ActionListener) in the type AbstractButton is not applicable for the arguments (prog)  prog.java   /Experiment/src line 38 Java Problem

ここに私のリスナークラスがあります:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class progEvent implements ActionListener {

prog GUI;
String newLine = System.getProperty("line.separater");


public progEvent(prog in) {
    GUI = in;
}

public void actionPerformed(ActionEvent event) {
    String command = event.getActionCommand();
    try {
        File numClicks = new File("numClicks.properties");
        FileOutputStream outStream = new FileOutputStream(numClicks);

        if (command.equals("oneLeft")) {
            write(outStream, "oneLeft has been clicked.");  
        }
        if (command.equals("oneRight")) {
            write(outStream, "oneRight has been clicked.");
        }
        if (command.equals("twoLeft")) {
            write(outStream, "twoLeft has been clicked.");
        }
        if (command.equals("twoRight")) {
            write(outStream, "twoRight has been clicked.");
        }
        if (command.equals("threeLeft")) {
            write(outStream, "threeLeft has been clicked.");
        }
        if (command.equals("threeRight")) {
            write(outStream, "threeRight has been clicked.");
        }
    } catch (IOException ioe) {
        System.out.println("The file could not be written to.");
    }
}

void write(FileOutputStream stream, String output) throws IOException {
    output = output + newLine;
    byte[] data = output.getBytes();
    stream.write(data, 0, data.length);
}
}

GUIも表示できません。私は本を​​使ってそれを学び、クラスをこの実験の一種のテンプレートとして使用しています。しかし、私は完全に立ち往生しています。ありがとう!

4

2 に答える 2

0

クラスは ActionListener を実装する必要があります。したがって、prog の定義は「prog extends JFrame implements ActionListener」である必要があります。次に、 actionperformed を実装します。例えば

public void actionPerformed(ActionEvent arg0) {
        if ("connect".equalsIgnoreCase(arg0.getActionCommand())) {
            connect();
        }

    }
于 2014-02-12T21:41:11.593 に答える