当前位置: 技术问答>java相关
高手請指教,高分相送!!!
来源: 互联网 发布时间:2015-07-06
本文导语: package game; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class slnew extends WindowAdapter implements MouseListener{ JFrame f=null; JButton b1=null; JLabel label=null; public slnew(){ f=new JFrame("Mouse Event"); Container cPan...
package game;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class slnew extends WindowAdapter implements MouseListener{
JFrame f=null;
JButton b1=null;
JLabel label=null;
public slnew(){
f=new JFrame("Mouse Event");
Container cPane=f.getContentPane();
cPane.setLayout(new GridLayout(2,1));
b1=new JButton("Button");
b1.addMouseListener(this);
label=new JLabel("NoNo",JLabel.CENTER);
cPane.add(label);
cPane.add(b1);
f.pack();
f.show();
f.addWindowListener(this);
}
public void mouseClicked(MouseEvent e){
label.setText("click 按");
}
public void windowClosing(WindowEvent e){
System.exit(0);
}
public static void main(String args[]){
new slnew ();
}
}
>javac gameslnew.java
提示:game.slnew should be declared abstract;it does not mousePressed(java.awt.event.MouseEvent) in game.slnew.
WHY??
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class slnew extends WindowAdapter implements MouseListener{
JFrame f=null;
JButton b1=null;
JLabel label=null;
public slnew(){
f=new JFrame("Mouse Event");
Container cPane=f.getContentPane();
cPane.setLayout(new GridLayout(2,1));
b1=new JButton("Button");
b1.addMouseListener(this);
label=new JLabel("NoNo",JLabel.CENTER);
cPane.add(label);
cPane.add(b1);
f.pack();
f.show();
f.addWindowListener(this);
}
public void mouseClicked(MouseEvent e){
label.setText("click 按");
}
public void windowClosing(WindowEvent e){
System.exit(0);
}
public static void main(String args[]){
new slnew ();
}
}
>javac gameslnew.java
提示:game.slnew should be declared abstract;it does not mousePressed(java.awt.event.MouseEvent) in game.slnew.
WHY??
|
MouseListener是个抽象类,下面方法都要实现:
void mouseClicked(MouseEvent mouseEvent);
void mousePressed(MouseEvent mouseEvent);
void mouseReleased(MouseEvent mouseEvent);
void mouseEntered(MouseEvent mouseEvent);
void mouseExited(MouseEvent mouseEvent);
void mouseClicked(MouseEvent mouseEvent);
void mousePressed(MouseEvent mouseEvent);
void mouseReleased(MouseEvent mouseEvent);
void mouseEntered(MouseEvent mouseEvent);
void mouseExited(MouseEvent mouseEvent);
|
可以继承MouseAdapter,
如bl.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e){}
});
这样你就可以只是先你需要的方法了,甚至可以不实现!
如bl.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e){}
});
这样你就可以只是先你需要的方法了,甚至可以不实现!
|
没有实现MouseListener接口的mousePressed方法
|
你实现一个接口就必须保证实现该接口中的所有abstract方法,不然编译器会报错。提示信息告诉你你没有实现mousePressed()方法。
|
必须实现接口定义的所有方法。
|
同意
|
没有实现MouseListener接口的mousePressed方法
|
MouseListener是个抽象类,下面方法都要实现:
void mouseClicked(MouseEvent mouseEvent);
void mousePressed(MouseEvent mouseEvent);
void mouseReleased(MouseEvent mouseEvent);
void mouseEntered(MouseEvent mouseEvent);
void mouseExited(MouseEvent mouseEvent);
void mouseClicked(MouseEvent mouseEvent);
void mousePressed(MouseEvent mouseEvent);
void mouseReleased(MouseEvent mouseEvent);
void mouseEntered(MouseEvent mouseEvent);
void mouseExited(MouseEvent mouseEvent);