package a;
import javax.swing.*;import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
/**
* @author qz
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class MainFrame implements MouseMotionListener { private JFrame frm;
private JPanel jp,jp1,jp2,jp3,jp4;
ImageIcon img1,img2,img3,img4;
JLabel jl1,jl2,jl3,jl4;
Point pt11,pt12,pt21,pt22,pt31,pt32,pt41,pt42;
public static void main(String[] args) {
MainFrame mf=new MainFrame();
}
public MainFrame(){
frm=new JFrame();
frm.setDefaultCloseOperation(2);
//frm.setSize(400,300);
jp=new JPanel(new GridLayout(2,2));
img1=new ImageIcon("1.gif");img2=new ImageIcon("2.gif");
img3=new ImageIcon("3.gif");img4=new ImageIcon("4.gif");
jl1=new JLabel();jl2=new JLabel();jl3=new JLabel();jl4=new JLabel();
jl1.setIcon(img1);jl2.setIcon(img2);jl3.setIcon(img3);jl4.setIcon(img4);
jp.add(jl1);jp.add(jl2);jp.add(jl3);jp.add(jl4);
jp.addMouseMotionListener(this);
frm.add(jp);
frm.pack();
frm.setVisible(true);
} /* (non-Javadoc)
* @see java.awt.event.MouseMotionListener#mouseDragged(java.awt.event.MouseEvent)
*/
public void mouseDragged(MouseEvent e) {
// TODO Auto-generated method stub
} /* (non-Javadoc)
* @see java.awt.event.MouseMotionListener#mouseMoved(java.awt.event.MouseEvent)
*/
public void mouseMoved(MouseEvent e) {
// TODO Auto-generated method stub
Point tmp=e.getPoint();
calPoint();
calin(tmp);
}
public void calPoint(){
int a,b,c,d;
pt11=new Point();pt12=new Point();pt21=new Point(); pt22=new Point();
pt31=new Point();pt32=new Point();pt41=new Point();pt42=new Point();
a=jl1.getX();b=jl1.getWidth();c=jl1.getY(); d=jl1.getHeight();
pt11.x=a;pt11.y=c;pt12.x=b;pt12.y=d;
pt21.x=a+b;pt21.y=c;pt22.x=b+b;pt22.y=d;
pt31.x=a;pt31.y=c+d;pt32.x=b;pt32.y=d+d;
pt41.x=a+b;pt41.y=c+d;pt42.x=b+b;pt42.y=d+d;
}
public void calin(Point tmp){
if(tmp.x>pt11.x && tmp.y>pt11.y && tmp.x<pt12.x && tmp.y<pt12.y){
jl1.setVisible(true);
jl2.setVisible(false);
jl3.setVisible(false);
jl4.setVisible(false);
}
if(tmp.x>pt21.x && tmp.y>pt21.y && tmp.x<pt22.x && tmp.y<pt22.y){
jl2.setVisible(true);
jl1.setVisible(false);
jl3.setVisible(false);
jl4.setVisible(false);
}
if(tmp.x>pt31.x && tmp.y>pt31.y && tmp.x<pt32.x && tmp.y<pt32.y){
jl3.setVisible(true);
jl1.setVisible(false);
jl2.setVisible(false);
jl4.setVisible(false);
}
if(tmp.x>pt41.x && tmp.y>pt41.y && tmp.x<pt42.x && tmp.y<pt42.y){
jl4.setVisible(true);
jl1.setVisible(false);
jl3.setVisible(false);
jl2.setVisible(false);
}
}
}
import javax.swing.*;import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
/**
* @author qz
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class MainFrame implements MouseMotionListener { private JFrame frm;
private JPanel jp,jp1,jp2,jp3,jp4;
ImageIcon img1,img2,img3,img4;
JLabel jl1,jl2,jl3,jl4;
Point pt11,pt12,pt21,pt22,pt31,pt32,pt41,pt42;
public static void main(String[] args) {
MainFrame mf=new MainFrame();
}
public MainFrame(){
frm=new JFrame();
frm.setDefaultCloseOperation(2);
//frm.setSize(400,300);
jp=new JPanel(new GridLayout(2,2));
img1=new ImageIcon("1.gif");img2=new ImageIcon("2.gif");
img3=new ImageIcon("3.gif");img4=new ImageIcon("4.gif");
jl1=new JLabel();jl2=new JLabel();jl3=new JLabel();jl4=new JLabel();
jl1.setIcon(img1);jl2.setIcon(img2);jl3.setIcon(img3);jl4.setIcon(img4);
jp.add(jl1);jp.add(jl2);jp.add(jl3);jp.add(jl4);
jp.addMouseMotionListener(this);
frm.add(jp);
frm.pack();
frm.setVisible(true);
} /* (non-Javadoc)
* @see java.awt.event.MouseMotionListener#mouseDragged(java.awt.event.MouseEvent)
*/
public void mouseDragged(MouseEvent e) {
// TODO Auto-generated method stub
} /* (non-Javadoc)
* @see java.awt.event.MouseMotionListener#mouseMoved(java.awt.event.MouseEvent)
*/
public void mouseMoved(MouseEvent e) {
// TODO Auto-generated method stub
Point tmp=e.getPoint();
calPoint();
calin(tmp);
}
public void calPoint(){
int a,b,c,d;
pt11=new Point();pt12=new Point();pt21=new Point(); pt22=new Point();
pt31=new Point();pt32=new Point();pt41=new Point();pt42=new Point();
a=jl1.getX();b=jl1.getWidth();c=jl1.getY(); d=jl1.getHeight();
pt11.x=a;pt11.y=c;pt12.x=b;pt12.y=d;
pt21.x=a+b;pt21.y=c;pt22.x=b+b;pt22.y=d;
pt31.x=a;pt31.y=c+d;pt32.x=b;pt32.y=d+d;
pt41.x=a+b;pt41.y=c+d;pt42.x=b+b;pt42.y=d+d;
}
public void calin(Point tmp){
if(tmp.x>pt11.x && tmp.y>pt11.y && tmp.x<pt12.x && tmp.y<pt12.y){
jl1.setVisible(true);
jl2.setVisible(false);
jl3.setVisible(false);
jl4.setVisible(false);
}
if(tmp.x>pt21.x && tmp.y>pt21.y && tmp.x<pt22.x && tmp.y<pt22.y){
jl2.setVisible(true);
jl1.setVisible(false);
jl3.setVisible(false);
jl4.setVisible(false);
}
if(tmp.x>pt31.x && tmp.y>pt31.y && tmp.x<pt32.x && tmp.y<pt32.y){
jl3.setVisible(true);
jl1.setVisible(false);
jl2.setVisible(false);
jl4.setVisible(false);
}
if(tmp.x>pt41.x && tmp.y>pt41.y && tmp.x<pt42.x && tmp.y<pt42.y){
jl4.setVisible(true);
jl1.setVisible(false);
jl3.setVisible(false);
jl2.setVisible(false);
}
}
}
解决方案 »
- glassfish下获取连接池问题
- SSH框架整合问题
- 关于WebLogic Portal的Shell的问题??
- Spring 的 Controller接口在那个架包里?
- 初学spring怎么得到连接啊??我连接的oracle数据库!
- struts中的赞不赞成用一个actionform接收两个表单数据?
- hibernate的问题, Cat is not mapped!!!!!!!!!!!
- 一个关于struts中logic iterate标签的问题
- forward 和redirect的区别??
- 急征:B/S C/S,两层 三层 多层,分布式 非分布式,面向对象 面向过程,MVC 设计模式
- 免费体验J2EE技术
- ireport高手请出来吧,搞不出来,小弟确实不想混了,严重打击我信心
if (e.getSource() != mp1)
{
}
源代码具体如下:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MyApplet extends JFrame
{
Icon img1=new ImageIcon("1.jpg");
Icon img2=new ImageIcon("2.jpg");
Icon img3=new ImageIcon("3.jpg");
Icon img4=new ImageIcon("4.jpg");
JButton mp1 = new JButton(); JButton mp2 = new JButton(); JButton mp3 = new JButton(); JButton mp4 = new JButton(); MyMouseEvent me = new MyMouseEvent(); MyApplet()
{
this.getContentPane().setLayout(new GridLayout(2, 2));
this.getContentPane().add(mp1);
this.getContentPane().add(mp2);
this.getContentPane().add(mp3);
this.getContentPane().add(mp4);
mp1.addMouseMotionListener(me);
mp2.addMouseMotionListener(me);
mp3.addMouseMotionListener(me);
mp4.addMouseMotionListener(me);
} class MyMouseEvent extends MouseMotionAdapter
{ /*
* (非 Javadoc)
*
* @see java.awt.event.MouseMotionAdapter#mouseMoved(java.awt.event.MouseEvent)
*/
public void mouseMoved(MouseEvent e)
{
if (e.getSource().equals(mp1))
{
mp1.setIcon(img1);
}
else if (e.getSource().equals(mp2))
{
mp2.setIcon(img2);
}
else if (e.getSource().equals(mp3))
{
mp3.setIcon(img3);
}
else if (e.getSource().equals(mp4))
{
mp4.setIcon(img4);
}
if (e.getSource() != mp1)
{
}
if (e.getSource() != mp2)
{
}
if(e.getSource() != mp3)
{
}
if (e.getSource() != mp4)
{
} } } public static void main(String[] args) {
MyApplet ma = new MyApplet();
ma.setSize(300, 300);
ma.show(); }
}