添加鼠标监听接口,里面方法的引数是MouseEvent可以通过他们得到鼠标的位置
class ML extend MouseAdapter{
public void mousePressed(MouseEvent me)
{
int x = me.getX();
int y = me.getY();
Point p = me.getPoint();//这个和上面的位置是相同的
}
}
class ML extend MouseAdapter{
public void mousePressed(MouseEvent me)
{
int x = me.getX();
int y = me.getY();
Point p = me.getPoint();//这个和上面的位置是相同的
}
}
解决方案 »
- 请教大家一个问题?
- Apache+tomcat迁移到apache+Weblogic整合之动静分离问题?困惑两天了~
- 注册了一个花生壳的域名,但是没有办法访问我的jsp网站
- Setting property 'source' to 'org.eclipse.jst.jee.server:*did not find a matching property.
- MyEclipse创建SOAP Web Services提示Unable to Create JAXBContext错误
- unclosed connection, forgot to call close() on your session?
- 在weblogic上部署EJB出现的问题
- 请问可否出掉static???
- servlet请求重定向中参数的传递:
- 请问运行测试代码出现这个怎么回事?以前运行还可以的
- 需要java源程序,请大家帮忙!
- 急请问:怎么把一个服务器传过来的url和参数都打印出来?....thanks........
Returns the x,y position of the event relative to the source component.
getX()
Returns the horizontal x position of the event relative to the source component.
getY()
Returns the vertical y position of the event relative to the source component.
import javax.swing.JToolBar;
import javax.swing.JButton;
import javax.swing.ImageIcon;import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.JPanel;import java.net.URL;import java.awt.*;
import java.awt.event.*;
public class BattleShip extends JFrame
implements MouseListener
{
static final int coordXCompStr = 50; //the x Coord of computer String;
static final int coordYCompStr = 60; //the y Coord of computer String; static final int coordXPlayerStr = 450; //the x Coord of Player String;
static final int coordYPlayerStr = 60; //the y Coord of Player String; static final int hmGrids = 5; //the grids number per line or per column
static final int widthGrid = 50; //the width of grid
static final int coordXCompPic = 30; //the x Coord of computer;
static final int coordYCompPic = 100; //the y Coord of computer; static final int coordXPlayerPic = 400; //the x Coord of Player;
static final int coordYPlayerPic = 100; //the y Coord of Player; static final private String OPEN = "OPEN";
static final private String SAVE = "SAVE";
static final private String SEARCH = "SEARCH"; Graphics myG; public static void main(String[] args)
{
BattleShip bs = new BattleShip();
bs.repaint();
}
public void paint(Graphics g)
{
DisplayBoard(g);
DisplayPlayerInformation(g);
myG = g;
} public void DisplayBoard(Graphics g)
{
g.setColor(Color.blue); //draw the grids of computer
//draw 5 vertical line
for (int i=0; i<=hmGrids; i++){
g.drawLine(coordXCompPic + widthGrid * i,coordYCompPic,coordXCompPic + widthGrid * i,coordYCompPic + widthGrid * hmGrids);
}
//draw 5 horizon line
for (int i=0; i<=hmGrids; i++){
g.drawLine(coordXCompPic ,coordYCompPic + widthGrid * i,coordXCompPic + widthGrid * hmGrids,coordYCompPic + widthGrid * i);
}
}
public void DisplayPlayerInformation(Graphics g)
{
g.setColor(Color.blue);
Font myFont;
g.setFont(new Font("Dialog", 3, 20));
g.drawString("computer", coordXCompStr, coordYCompStr);
}
BattleShip()
{
super("bs");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setSize(400, 400);
show();
setResizable(false);
}
public void mouseClicked(MouseEvent e)
{
myG.drawString("current mouse is in (" + e.getX() + "." + e.getY() + ")", 300, 200);
}
public void mouseEntered(MouseEvent e)
{
myG.setColor(Color.red);
myG.drawString("current mouse is in (" + e.getX() + "." + e.getY() + ")", 300, 200);
}
public void mouseExited(MouseEvent e)
{
myG.drawString("current mouse is in (" + e.getX() + "." + e.getY() + ")", 300, 200);
}
public void mousePressed(MouseEvent e)
{
myG.drawString("current mouse is in (" + e.getX() + "." + e.getY() + ")", 300, 200);
}
public void mouseReleased(MouseEvent e)
{
myG.drawString("current mouse is in (" + e.getX() + "." + e.getY() + ")", 300, 200);
}
}