Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/378.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java GUI框架和按钮_Java_Eclipse_Swing_Exception_Actionlistener - Fatal编程技术网

Java GUI框架和按钮

Java GUI框架和按钮,java,eclipse,swing,exception,actionlistener,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"); JBu

我试图创建一个框架与三个按钮的狗猫和鸟。当点击按钮时,我想让它们的声音返回。我似乎被错误缠住了,但似乎无法找出它们

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
这里有一个错误:

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);
这里有一个错误:

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);
  • BirdListenerClass Listener 3=新的BirdListenerClass()
    Listener3
    不是有效的变量名,我想您的意思是
    Listener3
  • Java是区分大小写的,所以
    @override
    不是有效的注释,我想你的意思是
    @override
  • jbtBird.addActionListener(listener3)
    正在抱怨,因为您已用大写字母
    L
    声明了
    Listener3
    ,请参阅前面的评论,尝试将
    Listener3
    更改为
    Listener3
  • <>代码> DogListenerClass > <代码> > <代码> CatListenerClass < /代码>和<代码> BirdListenerClass >代码>在 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");

        }
    }

}
  • BirdListenerClass Listener 3=新的BirdListenerClass()
    Listener3
    不是有效的变量名,我想您的意思是
    Listener3
  • Java是区分大小写的,所以
    @override
    不是有效的注释,我想你的意思是
    @override
  • jbtBird.addActionListener(listener3)
    正在抱怨,因为您已用大写字母
    L
    声明了
    Listener3
    ,请参阅前面的评论,尝试将
    Listener3
    更改为
    Listener3
  • <>代码> DogListenerClass > <代码> > <代码> CatListenerClass < /代码>和<代码> BirdListenerClass >代码>在 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");

        }
    }

}