当前位置: 技术问答>java相关
***一个奇怪的错误!!请帮忙!!谢谢
来源: 互联网 发布时间:2017-04-05
本文导语: 我做了一个小java程序,可是编译时出现如下错误: addMouseListener(java.awt.event.MouseListener) in java.awt.Component cannot be applied to (labelFrame) addMouseListener(this); 1 error; 我的源代码如下: import java.awt.*; import java.awt.eve...
我做了一个小java程序,可是编译时出现如下错误:
addMouseListener(java.awt.event.MouseListener) in java.awt.Component cannot be applied to (labelFrame)
addMouseListener(this);
1 error;
我的源代码如下:
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
public class frame extends Applet implements ActionListener
{
Button b1,b2;
labelFrame window;
public void init()
{
b1=new Button("Display the window");
add(b1);
b1.addActionListener(this);
b2=new Button("Hide the window.");
add(b2);
b2.addActionListener(this);
window=new labelFrame("Java window.");
window.setSize(300,200);
}
public void actionPerformed(ActionEvent event)
{
if (event.getSource()==b1){
window.setVisible(true);
}
if (event.getSource()==b2){
window.setVisible(false);
}
}
}
class labelFrame extends Frame
{
Label label;
labelFrame(String title)
{
super(title);
setLayout(new FlowLayout());
label=new Label("Hello from Java! This is a frame window.");
add(label);
addMouseListener(this);
}
public void mousePressed(MouseEvent e)
{
if ((e.getModifiers() & InputEvent.BUTTON1_MASK)==InputEvent.BUTTON1_MASK){
label.setText("Left ouse button down at "+e.getX()+","+e.getY());
}
else
{label.setText("Right mouse button down at "+e.getX()+","+e.getY());
}
}
public void mouseClicked(MouseEvent e)
{
label.setText("You clicked the mouse at "+e.getX()+","+e.getY());
}
public void mouseReleased(MouseEvent e)
{
label.setText("the mouse button went up.");
}
public void mouseEntered(MouseEvent e)
{
label.setText("The mouse entered.");
}
public void mouseExited(MouseEvent e)
{
label.setText("The mouse exited.");
}
}
addMouseListener(java.awt.event.MouseListener) in java.awt.Component cannot be applied to (labelFrame)
addMouseListener(this);
1 error;
我的源代码如下:
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
public class frame extends Applet implements ActionListener
{
Button b1,b2;
labelFrame window;
public void init()
{
b1=new Button("Display the window");
add(b1);
b1.addActionListener(this);
b2=new Button("Hide the window.");
add(b2);
b2.addActionListener(this);
window=new labelFrame("Java window.");
window.setSize(300,200);
}
public void actionPerformed(ActionEvent event)
{
if (event.getSource()==b1){
window.setVisible(true);
}
if (event.getSource()==b2){
window.setVisible(false);
}
}
}
class labelFrame extends Frame
{
Label label;
labelFrame(String title)
{
super(title);
setLayout(new FlowLayout());
label=new Label("Hello from Java! This is a frame window.");
add(label);
addMouseListener(this);
}
public void mousePressed(MouseEvent e)
{
if ((e.getModifiers() & InputEvent.BUTTON1_MASK)==InputEvent.BUTTON1_MASK){
label.setText("Left ouse button down at "+e.getX()+","+e.getY());
}
else
{label.setText("Right mouse button down at "+e.getX()+","+e.getY());
}
}
public void mouseClicked(MouseEvent e)
{
label.setText("You clicked the mouse at "+e.getX()+","+e.getY());
}
public void mouseReleased(MouseEvent e)
{
label.setText("the mouse button went up.");
}
public void mouseEntered(MouseEvent e)
{
label.setText("The mouse entered.");
}
public void mouseExited(MouseEvent e)
{
label.setText("The mouse exited.");
}
}
|
改成如下:(仔细看)
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
public class frame extends Applet implements ActionListener
{
Button b1,b2;
labelFrame window;
public void init()
{
b1=new Button("Display the window");
add(b1);
b1.addActionListener(this);
b2=new Button("Hide the window.");
add(b2);
b2.addActionListener(this);
window=new labelFrame("Java window.");
window.setSize(300,200);
}
public void actionPerformed(ActionEvent event)
{
if (event.getSource()==b1){
window.setVisible(true);
}
if (event.getSource()==b2){
window.setVisible(false);
}
}
}
class labelFrame extends Frame implements MouseListener
{
Label label;
labelFrame(String title)
{
super(title);
setLayout(new FlowLayout());
label=new Label("Hello from Java! This is a frame window.");
add(label);
addMouseListener(this);
}
public void mousePressed(MouseEvent e)
{
if ((e.getModifiers() & InputEvent.BUTTON1_MASK)==InputEvent.BUTTON1_MASK){
label.setText("Left ouse button down at "+e.getX()+","+e.getY());
}
else
{label.setText("Right mouse button down at "+e.getX()+","+e.getY());
}
}
public void mouseClicked(MouseEvent e)
{
label.setText("You clicked the mouse at "+e.getX()+","+e.getY());
}
public void mouseReleased(MouseEvent e)
{
label.setText("the mouse button went up.");
}
public void mouseEntered(MouseEvent e)
{
label.setText("The mouse entered.");
}
public void mouseExited(MouseEvent e)
{
label.setText("The mouse exited.");
}
}
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
public class frame extends Applet implements ActionListener
{
Button b1,b2;
labelFrame window;
public void init()
{
b1=new Button("Display the window");
add(b1);
b1.addActionListener(this);
b2=new Button("Hide the window.");
add(b2);
b2.addActionListener(this);
window=new labelFrame("Java window.");
window.setSize(300,200);
}
public void actionPerformed(ActionEvent event)
{
if (event.getSource()==b1){
window.setVisible(true);
}
if (event.getSource()==b2){
window.setVisible(false);
}
}
}
class labelFrame extends Frame implements MouseListener
{
Label label;
labelFrame(String title)
{
super(title);
setLayout(new FlowLayout());
label=new Label("Hello from Java! This is a frame window.");
add(label);
addMouseListener(this);
}
public void mousePressed(MouseEvent e)
{
if ((e.getModifiers() & InputEvent.BUTTON1_MASK)==InputEvent.BUTTON1_MASK){
label.setText("Left ouse button down at "+e.getX()+","+e.getY());
}
else
{label.setText("Right mouse button down at "+e.getX()+","+e.getY());
}
}
public void mouseClicked(MouseEvent e)
{
label.setText("You clicked the mouse at "+e.getX()+","+e.getY());
}
public void mouseReleased(MouseEvent e)
{
label.setText("the mouse button went up.");
}
public void mouseEntered(MouseEvent e)
{
label.setText("The mouse entered.");
}
public void mouseExited(MouseEvent e)
{
label.setText("The mouse exited.");
}
}
|
addMouseListener(this)
你这个方法在哪???
你这个方法在哪???