标签:
package
peidui;
import
java.awt.event.*;
import
java.awt.*;
import
javax.swing.*;
import
javax.swing.border.LineBorder;
public
class
peidui
extends
JFrame
implements
MouseMotionListener,MouseListener{
JLabel j1,j2,j3,j4,j5,j6;
JPanel jp1,jp2;
Point presspoint;
public
static
void
main(String[] args) {
peidui pd=
new
peidui();
}
public
peidui(){
j1=
new
JLabel(
new
ImageIcon(getClass().getResource(
"bike.jpg"
)));
j2=
new
JLabel(
new
ImageIcon(getClass().getResource(
"coffe.jpg"
)));
j3=
new
JLabel(
new
ImageIcon(getClass().getResource(
"dress.jpg"
)));
j4=
new
JLabel(
"咖啡"
,JLabel.CENTER);
j5=
new
JLabel(
"衣服"
,JLabel.CENTER);
j6=
new
JLabel(
"自行车"
,JLabel.CENTER);
jp1=
new
JPanel();
jp2=
new
JPanel();
j1.setPreferredSize(
new
Dimension(
60
,
60
));
j1.setBorder(
new
LineBorder(Color.BLACK));
j2.setPreferredSize(
new
Dimension(
60
,
60
));
j2.setBorder(
new
LineBorder(Color.BLACK));
j3.setPreferredSize(
new
Dimension(
60
,
60
));
j3.setBorder(
new
LineBorder(Color.BLACK));
j4.setBackground(Color.green);
j4.setOpaque(
true
);
j4.setPreferredSize(
new
Dimension(
80
,
80
));
j5.setBackground(Color.green);
j5.setOpaque(
true
);
j5.setPreferredSize(
new
Dimension(
80
,
80
));
j6.setBackground(Color.green);
j6.setOpaque(
true
);
j6.setPreferredSize(
new
Dimension(
80
,
80
));
j1.addMouseListener(
this
);
//注册监听
j1.addMouseMotionListener(
this
);
//注册监听
j2.addMouseListener(
this
);
j2.addMouseMotionListener(
this
);
j3.addMouseListener(
this
);
j3.addMouseMotionListener(
this
);
jp1.add(j1);
jp1.add(j2);
jp1.add(j3);
this
.setGlassPane(jp1);
//加玻璃板把jp1放到玻璃板上
this
.getGlassPane().setVisible(
true
);
//玻璃板的显示即jp1的显示
jp1.setOpaque(
false
);
//面板透明,玻璃板不透明窗体
jp2.add(j4);
jp2.add(j5);
jp2.add(j6);
this
.add(jp2,BorderLayout.SOUTH);
this
.setTitle(
"配对游戏"
);
this
.setLocation(
500
,
200
);
this
.setSize(
300
,
300
);
this
.setVisible(
true
);
this
.setResizable(
true
);
this
.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@Override
public
void
mouseClicked(MouseEvent arg0) {
// 单击
}
@Override
public
void
mouseEntered(MouseEvent arg0) {
// 进入
}
@Override
public
void
mouseExited(MouseEvent arg0) {
// 退出
}
@Override
public
void
mousePressed(MouseEvent arg0) {
// 一直按住
presspoint=arg0.getPoint();
}
@Override
public
void
mouseReleased(MouseEvent arg0) {
// 释放
if
(check()){
this
.getGlassPane().setVisible(
false
);
j6.setBackground(Color.red);
j6.setText(
"配对成功"
);
j5.setBackground(Color.red);
j5.setText(
"配对成功"
);
j4.setBackground(Color.red);
j4.setText(
"配对成功"
);
}
}
@Override
public
void
mouseDragged(MouseEvent arg0) {
//拖拽
Point curentPoint=arg0.getPoint();
JLabel imgsource=(JLabel)arg0.getSource();
Point labPoint=imgsource.getLocation();
imgsource.setLocation(labPoint.x+(curentPoint.x-presspoint.x), labPoint.y+(curentPoint.y-presspoint.y) );
}
@Override
public
void
mouseMoved(MouseEvent arg0) {
// 移动
}
public
boolean
check(){
boolean
asc=
true
;
Point a=j1.getLocationOnScreen();
Point b=j6.getLocationOnScreen();
if
(a.x<b.x||a.y<b.y||a.x>b.x+
80
||a.y>b.y+
80
)
{
asc=
false
;
j6.setBackground(Color.green);
}
Point c=j3.getLocationOnScreen();
Point d=j5.getLocationOnScreen();
if
(c.x<d.x||c.y<d.y||c.x>d.x+
80
||c.y>d.y+
80
)
{
asc=
false
;
j5.setBackground(Color.green);
}
Point e=j2.getLocationOnScreen();
Point f=j4.getLocationOnScreen();
if
(e.x<f.x||e.y<f.y||e.x>f.x+
80
||e.y>f.y+
80
)
{
asc=
false
;
j4.setBackground(Color.green);
}
return
asc;
}
}
标签:
原文地址:http://www.cnblogs.com/BK-12345/p/5202690.html