musicButton2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
AllMusic.stop();
AllMusic.gamemusic2();
music2 = true;
music1 = false;
music3 = false;
}
});
上面是添加AcitonListener
但是添加mouseListener怎么写呢?就这个地方musicButton2.addActionListener(new ActionListener(){
----------------解决方案--------------------------------------------------------
不是一样么?..
----------------解决方案--------------------------------------------------------
public void musicChoose1()
{
musicButton1.addMouseListener(new MouseListener()
{
public void mouseClicked(MouseEvent e)
{
AllMusic.stop();
AllMusic.gamemusic1();
music1 = true;
music2 = false;
music3 = false;
}
});
musicButton1.addMouseListener(new MouseListener()
一句要报错
}
----------------解决方案--------------------------------------------------------
晕。。。
自己写错字。。。
我是瓜的。。。
----------------解决方案--------------------------------------------------------
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JButton;
public class MainClass extends JFrame{
private JButton jbutton;
public MainClass(){
jbutton=new JButton();
jbutton.addMouseListener(new MouseListener(){
public void mouseClicked(MouseEvent e){
System.out.println("click");
}
public void mouseReleased(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mousePressed(MouseEvent e){}
public void mouseExited(MouseEvent e){}
});
jbutton.setText("click");
this.getContentPane().add(jbutton);
this.setSize(200,200);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args){
new MainClass();
}
}
----------------解决方案--------------------------------------------------------
如果你不想实现MouseListener接口的所有方法..那你可以用MouseAdapter这个类.
----------------解决方案--------------------------------------------------------
purana说得有道理
不过最好还是尽量不用匿名内部类,因为写多了,不好维护
----------------解决方案--------------------------------------------------------
哦
谢谢楼上2位
----------------解决方案--------------------------------------------------------