import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import javax.swing.*;public class LaughTrack extends JFrame{ public LaughTrack() {
super("LaughTrack");
setSize(190,80);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container content=getContentPane();
FlowLayout flo =new FlowLayout();
content.setLayout(flo);
LaughButton haha =new LaughButton();
content.add(haha);
setContentPane(content);
setVisible(true);
}
public static void main(String[] arguments){
LaughTrack lt=new LaughTrack();
}
}class LaughButton extends JButton implements Runnable,ActionListener{
AudioClip[] laugh =new AudioClip[4];
Thread runner;
LaughButton(){
super("Start Laughing");
addActionListener(this);
for(int i=0;i<laugh.length;i++){
try{
URL laughIn=new URL("file:laugh"+i+".wma");
laugh[i]=JApplet.newAudioClip(laughIn);
}catch(MalformedURLException e){}
}
}
public void actionPerformed(ActionEvent event){
String command =event.getActionCommand();
if (command=="Start Laughing"); startLaughing();
if(command=="Stop Laughing");
stopLaughing();
}
void startLaughing(){
if(runner==null){
runner=new Thread(this);
runner.start();
this.setText("Stop Laughing");
}
} void stopLaughing(){
if (runner!=null){
for(int i=0;i<laugh.length;i++)
if(laugh[i]!=null)
laugh[i].stop();
runner=null;
this.setText("Start Laughing");
}
}
public void run(){
for(int i=0;i<laugh.length;i++)
if(laugh[i]!=null)
laugh[i].loop();
Thread thisThread=Thread.currentThread();
while(runner==thisThread){
try{
Thread.sleep(5000);
}catch (InterruptedException e){}
}
}
}
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import javax.swing.*;public class LaughTrack extends JFrame{ public LaughTrack() {
super("LaughTrack");
setSize(190,80);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container content=getContentPane();
FlowLayout flo =new FlowLayout();
content.setLayout(flo);
LaughButton haha =new LaughButton();
content.add(haha);
setContentPane(content);
setVisible(true);
}
public static void main(String[] arguments){
LaughTrack lt=new LaughTrack();
}
}class LaughButton extends JButton implements Runnable,ActionListener{
AudioClip[] laugh =new AudioClip[4];
Thread runner;
LaughButton(){
super("Start Laughing");
addActionListener(this);
for(int i=0;i<laugh.length;i++){
try{
URL laughIn=new URL("file:laugh"+i+".wma");
laugh[i]=JApplet.newAudioClip(laughIn);
}catch(MalformedURLException e){}
}
}
public void actionPerformed(ActionEvent event){
String command =event.getActionCommand();
if (command=="Start Laughing"); startLaughing();
if(command=="Stop Laughing");
stopLaughing();
}
void startLaughing(){
if(runner==null){
runner=new Thread(this);
runner.start();
this.setText("Stop Laughing");
}
} void stopLaughing(){
if (runner!=null){
for(int i=0;i<laugh.length;i++)
if(laugh[i]!=null)
laugh[i].stop();
runner=null;
this.setText("Start Laughing");
}
}
public void run(){
for(int i=0;i<laugh.length;i++)
if(laugh[i]!=null)
laugh[i].loop();
Thread thisThread=Thread.currentThread();
while(runner==thisThread){
try{
Thread.sleep(5000);
}catch (InterruptedException e){}
}
}
}
public void actionPerformed(ActionEvent event){
String command =event.getActionCommand();
if (command=="Start Laughing")
startLaughing();
if(command=="Stop Laughing")
stopLaughing();
}把if (command=="Start Laughing") 和if(command=="Stop Laughing")后的分号去掉就行了。
public void actionPerformed(ActionEvent event){
String command =event.getSourse();
if (command==haha)
startLaughing();
...
}
String command =event.getActionCommand();
应该是Object command =event.getActionCommand();
public void actionPerformed(ActionEvent event){
String command =event.getActionCommand();
if (command=="Start Laughing");//这里最好不要这样写.再说,你这行代码没有任何意义. startLaughing();
if(command=="Stop Laughing");//这里最好不要这样写.再说,你这行代码没有任何意义.
stopLaughing();
}这样的代码最好写成下面这个样子. public void actionPerformed(ActionEvent event){
String command =event.getActionCommand();
if ("Start Laughing".equals(command)) {//这里为什么要写这样,你可以看一下Java中==与equals()区别
startLaughing();
} else if ("Stop Laughing".equals(command)) {
stopLaughing();
}
}
Java中==与equals()区别:
在比较对象时候,常常用到 "==" 和 "equals(Object)"。它们常常让初学者感到疑惑。下面先看一个例子
public class Example1
{
public static void main(String[] args)
{
String s1=new String("abc");
String s2=new String("abc");
// s1=s2;
System.out.println("用 == 比较结果");
System.out.println(s1==s2); //false
}
}
既然两个 String 对象内容同为 "abc" 为什么先打出 false 呢。那是由于 "==" 比较的是两个对象的引用 (references),并不是他们的内容,怎么才能比较内容是否相等呢?去掉 s1=s2 一句的注释符结果就不同了,因为他们的引用相同了。
我们要使用 equals(Object) 方法,由于 equals(Object) 方法为 Object 类中定义的方法,所用按照默认方式定义过的类均是其子类。 也就是说 Object 类为所有类的超类 (super class,也叫父类、基类等),在Object 中的 equals(Object) 方法其标准形式为public boolean equals(Object obj) 返回类型为 boolean ,即 true/false 与 "==" 返回类型一样。Object 类中定义的 equals(Object) 方法是直接使用 "==" 比较的两个对象,所以在没有覆盖 (override,或称改写、重写) equals(Object) 方法的情况下,equals(Object) 与 "==" 一样是比较的引用。如下例 (结果在注释中):
public class Example4
{
public static void main(String[] args)
{
Example4 e=new Example4();
Example4 e4=new Example4();
System.out.println("用 equals(Object) 比较结果");
System.out.println(e.equals(e4)); //结果为 false
System.out.println("用 == 比较结果");
System.out.println(e==e4); //结果为 false
}
}
equals(Object) 方法与 "==" 相比的特殊之处就在于它可以覆盖,所以我们可以通过覆盖的办法让它不是比较引用而是比较数据内容。当然 JDK 中也有覆盖过 equals(Object) 方法的类,如 java.lang.String,它就覆盖了从 Object 继承来的的 equals(Object) 方法,用以比较字符串内容是否相同。看看下面这个例子:
public class Example1
{
public static void main(String[] args)
{
String s1=new String("abc");
String s2=new String("abc");
System.out.println("用 == 比较结果");
System.out.println(s1==s2);//false
System.out.println("用equals(Object) 比较结果");
System.out.println(s1.equals(s2));//true }
}
例中用 equals(Object) 比较结果为 true。用 == 比较结果为 false。String.equals(Object) 方法直接比较了两个字符串的内容,如果相同则返回 true ,否则返回 false。你可以尝试把其中一个 "abc" 改成 "abcde",看看结果有何变化。
equals 方法(是String类从它的超类Object中继承的)被用来检测两个对象是否相等,即两个对象的内容是否相等。
==用于比较引用和比较基本数据类型时具有不同的功能:
比较基本数据类型,如果两个值相同,则结果为true
而在比较引用时,如果引用指向内存中的同一对象,结果为true
public void actionPerformed(ActionEvent event){
String command =event.getActionCommand();
if (command=="Start Laughing"); startLaughing();
if(command=="Stop Laughing");
stopLaughing();
}
分号去掉就可以了,其他地方没有错啊!
我帮你测试一下
if (command=="Start Laughing");
startLaughing();
if(command=="Stop Laughing");
stopLaughing();
if()条件后面若是有分号,它的执行语句就为空,既是什么也不执行。
这时你运行,它就会一直显示Start Laughing。点击按钮也就没有什么反应