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 >代码>在
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 >代码>在
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");
}
}
}