当前位置: 技术问答>java相关
请帮忙诊断这段程序
来源: 互联网 发布时间:2017-04-13
本文导语: // app16_1, 简单的事件处理范例(已加入事件处理) import java.awt.*; import java.awt.event.*; import javax.swing.*; class app16_1 implements MouseListener { static Frame frm=new Frame(); static Butt...
// app16_1, 简单的事件处理范例(已加入事件处理)
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class app16_1 implements MouseListener
{
static Frame frm=new Frame();
static Button btn=null;
public static void main(String args[])
{
btn=new Button("Click Me");
btn.addMouseListener(frm); // 把frm向btn爹注册
frm.setLayout(null);
frm.setTitle("Action Event");
frm.setBounds(45,65,200,150);
btn.setBounds(50,70,30,30);
frm.add(btn);
frm.setVisible(true);
frm.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}});
}
public void mouseEntered(MouseEvent e) // 鼠标的指针进入btn上方
{
}
public void mouseClicked(MouseEvent e) // 按下并放开鼠标按钮
{
frm.setBackground(Color.red);
}
public void mouseExited(MouseEvent e) // 鼠标的指针移开btn上方
{
}
public void mousePressed(MouseEvent e) // 按下鼠标的按钮
{
}
public void mouseReleased(MouseEvent e) // 放开鼠标按钮
{
}
}
C:JCreator LEMyProjectsSSapp16_1.java:17: addMouseListener(java.awt.event.MouseListener) in java.awt.Component cannot be applied to (java.awt.Frame)
btn.addMouseListener(frm);
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class app16_1 implements MouseListener
{
static Frame frm=new Frame();
static Button btn=null;
public static void main(String args[])
{
btn=new Button("Click Me");
btn.addMouseListener(frm); // 把frm向btn爹注册
frm.setLayout(null);
frm.setTitle("Action Event");
frm.setBounds(45,65,200,150);
btn.setBounds(50,70,30,30);
frm.add(btn);
frm.setVisible(true);
frm.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}});
}
public void mouseEntered(MouseEvent e) // 鼠标的指针进入btn上方
{
}
public void mouseClicked(MouseEvent e) // 按下并放开鼠标按钮
{
frm.setBackground(Color.red);
}
public void mouseExited(MouseEvent e) // 鼠标的指针移开btn上方
{
}
public void mousePressed(MouseEvent e) // 按下鼠标的按钮
{
}
public void mouseReleased(MouseEvent e) // 放开鼠标按钮
{
}
}
C:JCreator LEMyProjectsSSapp16_1.java:17: addMouseListener(java.awt.event.MouseListener) in java.awt.Component cannot be applied to (java.awt.Frame)
btn.addMouseListener(frm);
|
addMouseListener(java.awt.event.MouseListener) in java.awt.Component cannot be applied to (java.awt.Frame)
Frame 并没有实现MouseListener接口,怎么会监听鼠标事件呢?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class app16_1 implements MouseListener
{
static Frame frm=new Frame();
static Button btn=null;
public static void main(String args[])
{
new app16_1();
}
public app16_1()
{
btn=new Button("Click Me");
btn.addMouseListener(this); // 把frm向btn爹注册
frm.setLayout(null);
frm.setTitle("Action Event");
frm.setBounds(45,65,200,150);
btn.setBounds(50,70,30,30);
frm.add(btn);
frm.setVisible(true);
frm.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}});
}
public void mouseEntered(MouseEvent e) // 鼠标的指针进入btn上方
{
}
public void mouseClicked(MouseEvent e) // 按下并放开鼠标按钮
{
frm.setBackground(Color.red);
}
public void mouseExited(MouseEvent e) // 鼠标的指针移开btn上方
{
}
public void mousePressed(MouseEvent e) // 按下鼠标的按钮
{
}
public void mouseReleased(MouseEvent e) // 放开鼠标按钮
{
}
}
Frame 并没有实现MouseListener接口,怎么会监听鼠标事件呢?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class app16_1 implements MouseListener
{
static Frame frm=new Frame();
static Button btn=null;
public static void main(String args[])
{
new app16_1();
}
public app16_1()
{
btn=new Button("Click Me");
btn.addMouseListener(this); // 把frm向btn爹注册
frm.setLayout(null);
frm.setTitle("Action Event");
frm.setBounds(45,65,200,150);
btn.setBounds(50,70,30,30);
frm.add(btn);
frm.setVisible(true);
frm.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}});
}
public void mouseEntered(MouseEvent e) // 鼠标的指针进入btn上方
{
}
public void mouseClicked(MouseEvent e) // 按下并放开鼠标按钮
{
frm.setBackground(Color.red);
}
public void mouseExited(MouseEvent e) // 鼠标的指针移开btn上方
{
}
public void mousePressed(MouseEvent e) // 按下鼠标的按钮
{
}
public void mouseReleased(MouseEvent e) // 放开鼠标按钮
{
}
}
|
button上不能俘获鼠标事件
|
用JFrame,JButton试试