java如何实现在一个窗口上鼠标左健单击时有一个图形放开时消失?求大师发程序代码? 5
2个回答
展开全部
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class MouseFrame extends JFrame{
JPanel jp = new JPanel();
JLabel jl = new JLabel();
public MouseFrame() {
this.add(jp);
jp.setLayout(null);
jp.add(jl);
jl.setBounds(20, 20, 100, 100);
addMouseAction();
this.setBounds(200, 200, 200, 200);
this.setVisible(true);
}
// 给窗体增加鼠标事件
private void addMouseAction() {
this.addMouseListener(new MouseListener() {
// 鼠标在窗体弹起
public void mouseReleased(MouseEvent e) {
jl.setIcon(null);
}
// 鼠标在窗体按下
public void mousePressed(MouseEvent e) {
try {
BufferedImage img = ImageIO.read(new File("d:\\1.jpg"));
jl.setIcon(new ImageIcon(img));
} catch (IOException e1) {
e1.printStackTrace();
}
}
// 鼠标离开窗体
public void mouseExited(MouseEvent e) {
}
// 鼠标进入窗体
public void mouseEntered(MouseEvent e) {
}
// 鼠标点击窗体
public void mouseClicked(MouseEvent e) {
}
});
}
}
鼠标在窗体按下,窗体会显示一张图片,松开鼠标,图片消失。
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class MouseFrame extends JFrame{
JPanel jp = new JPanel();
JLabel jl = new JLabel();
public MouseFrame() {
this.add(jp);
jp.setLayout(null);
jp.add(jl);
jl.setBounds(20, 20, 100, 100);
addMouseAction();
this.setBounds(200, 200, 200, 200);
this.setVisible(true);
}
// 给窗体增加鼠标事件
private void addMouseAction() {
this.addMouseListener(new MouseListener() {
// 鼠标在窗体弹起
public void mouseReleased(MouseEvent e) {
jl.setIcon(null);
}
// 鼠标在窗体按下
public void mousePressed(MouseEvent e) {
try {
BufferedImage img = ImageIO.read(new File("d:\\1.jpg"));
jl.setIcon(new ImageIcon(img));
} catch (IOException e1) {
e1.printStackTrace();
}
}
// 鼠标离开窗体
public void mouseExited(MouseEvent e) {
}
// 鼠标进入窗体
public void mouseEntered(MouseEvent e) {
}
// 鼠标点击窗体
public void mouseClicked(MouseEvent e) {
}
});
}
}
鼠标在窗体按下,窗体会显示一张图片,松开鼠标,图片消失。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询