GUI框架和按钮

时间:2015-08-13 06:18:07

标签: java eclipse swing exception actionlistener

我正在尝试为狗猫和鸟创建一个带有三个按钮的框架。点击按钮我想让他们的声音返回。我似乎陷入了错误,但似乎无法弄明白。

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

public class Lab2 extends JFrame {
    public Lab2() {

        //Create three buttons.
        JButton jbtDog = new JButton("Dog");
        JButton jbtCat = new JButton("Cat");
        JButton jbtBird = new JButton("bird");


        //Create a panel and add the buttons.
        JPanel p1 = new JPanel();
        p1.add(jbtDog);
        p1.add(jbtCat);
        p1.add(jbtBird);


        //Add the panel to the frame.
        add(p1); 

        //Create the listeners.
        DogListenerClass listener1 = new DogListenerClass();
        CatListenerClass listener2 = new CatListenerClass();
        BirdListenerClass Listener 3 = new BirdListenerClass();


        //Register the listeners.
        jbtDog.addActionListener(listener1);
        jbtCat.addActionListener(listener2);
        jbtBird.addActionListener(listener3);
    }

    public static void main(String[] args) {
        JFrame frame = new Lab2();
        frame.setTitle("Lab 2");
        frame.setSize(200, 150);
        frame.setLocation(200, 100);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

class DogListenerClass implements ActionListener {
    @Override 
    public void actionPerformed(ActionEvent e) {
        System.out.println("BARK BARK!");
    }
}

class CatListenerClass implements ActionListener {
    @Override 
    public void actionPerformed(ActionEvent e) {
        System.out.println("meow");
    }
}



class BirdListenerClass implements ActionListener{
    @override
    public void actionPerformed(ActionEvent e) {
        System.out.println("chirp chirp");

    }   
}




my errors

Description Resource    Path    Location    Type
Syntax error on tokens, delete these tokens yButton.java    /ICS141/src line 3  Java Problem

Syntax error on token "JFrame", ( expected after this token yButton.java    /ICS141/src line 3  Java Problem

Syntax error, insert ")" to complete Arguments  yButton.java    /ICS141/src line 5  Java Problem

Build path specifies execution environment CDC-1.1/Foundation-1.1. There are no JREs installed in the workspace that are strictly compatible with this environment.     ICS141      Build path  JRE System Library Problem
Syntax error, insert ")" to complete ClassInstanceCreationExpression    yButton.java    /ICS141/src line 5  Java Problem

Syntax error, insert "]" to complete ArrayAccess    yButton.java    /ICS141/src line 2  Java Problem

Build path specifies execution environment CDC-1.1/Foundation-1.1. There are no JREs installed in the workspace that are strictly compatible with this environment.     two     Build path  JRE System Library Problem
Syntax error on token "]", invalid (    yButton.java    /ICS141/src line 2  Java Problem

Syntax error, insert "enum Identifier" to complete EnumHeader   yButton.java    /ICS141/src line 2  Java Problem
Build path specifies execution environment CDC-1.1/Foundation-1.1. There are no JREs installed in the workspace that are strictly compatible with this environment.     Eclipse     Build path  JRE System Library Problem
Syntax error, insert ")" to complete SingleMemberAnnotation yButton.java    /ICS141/src line 2  Java Problem

Syntax error on tokens, AnnotationName expected instead yButton.java    /ICS141/src line 2  Java Problem

2 个答案:

答案 0 :(得分:2)

这里有一个错误:

class BirdListenerClass implements ActionListener{
   // Case sensitive, start uppercase for Override
   @Override
   public void actionPerformed(ActionEvent e) {
       System.out.println("chirp chirp");

   }   
}

下一个就在这里

// You can´t seperate the variable name with a space like you did
BirdListenerClass listener3 = new BirdListenerClass();

这里的最后一个

// If you misspel the variable name (case sensitive aswell) then you can´t find it
jbtBird.addActionListener(listener3);

答案 1 :(得分:2)

  • BirdListenerClass Listener 3 = new BirdListenerClass(); Listener 3不是有效的变量名称,我认为您的意思是Listener3
  • Java区分大小写,因此@override不是有效的注释,我认为您的意思是@Override
  • jbtBird.addActionListener(listener3);有抱怨,因为您已使用大写Listener3声明L,请参阅之前的评论,尝试将Listener3转换为listener3
  • 我不确定DogListenerClassCatListenerClassBirdListenerClass是否在Lab2源文件的外部,但是如果它们在同一个文件中,可以考虑通过将它们放在{...}类的Lab2大括号内来使它们成为内部类......

例如......

public class Lab2 extends JFrame {
    //...
    class DogListenerClass implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("BARK BARK!");
        }
    }

    class CatListenerClass implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("meow");
        }
    }

    class BirdListenerClass implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("chirp chirp");

        }
    }

}