Java 鼠标监听事件 mouseMoved(MouseEvent) 10
importjava.awt.Color;importjavax.swing.*;importjava.awt.event.MouseAdapter;importjava...
import java.awt.Color;
import javax.swing.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
public class BtnText1 extends JFrame implements MouseMotionListener
{
private JButton btn;
private JPanel panel=new JPanel();
public void init()
{
btn=new JButton("改变颜色的按钮");
btn.addMouseListener(new MouseAdapter(){
public void mouseEntered(MouseEvent e){
panel.setBackground(Color.blue);
}
public void mouseExited(MouseEvent e){
panel.setBackground(Color.green);
}
public void mouseClicked(MouseEvent e){
if(e.getClickCount()==2)
panel.setBackground(Color.gray);
if(e.getClickCount()==1)
panel.setBackground(Color.black);
}
});
btn.addMouseMotionListener(new MouseAdapter(){
public void mouseDragged(MouseEvent e){}
public void mouseMoved(MouseEvent e){
panel.setBackground(Color.red);
}
});
this.add(panel);
this.setBounds(110,110,200,200);
panel.add(btn);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args)
{
new BtnText1().init();
}
}
上面的代码怎么实现鼠标在按钮上上面移动面板变红 展开
import javax.swing.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
public class BtnText1 extends JFrame implements MouseMotionListener
{
private JButton btn;
private JPanel panel=new JPanel();
public void init()
{
btn=new JButton("改变颜色的按钮");
btn.addMouseListener(new MouseAdapter(){
public void mouseEntered(MouseEvent e){
panel.setBackground(Color.blue);
}
public void mouseExited(MouseEvent e){
panel.setBackground(Color.green);
}
public void mouseClicked(MouseEvent e){
if(e.getClickCount()==2)
panel.setBackground(Color.gray);
if(e.getClickCount()==1)
panel.setBackground(Color.black);
}
});
btn.addMouseMotionListener(new MouseAdapter(){
public void mouseDragged(MouseEvent e){}
public void mouseMoved(MouseEvent e){
panel.setBackground(Color.red);
}
});
this.add(panel);
this.setBounds(110,110,200,200);
panel.add(btn);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args)
{
new BtnText1().init();
}
}
上面的代码怎么实现鼠标在按钮上上面移动面板变红 展开
1个回答
展开全部
public class BtnText1 extends JFrame implements MouseMotionListener
不需要实现MouseMotionListener接口,你已经用了addMouseMotionListener方法
MouseAdapter类已经是实现了MouseMotionListener接口的。
改成
public class BtnText1 extends JFrame
可以运行成功
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询