如何让Jframe与鼠标点击一起工作?

时间:2014-02-01 18:27:42

标签: java swing jframe mouselistener gridworld

我正在gridworld中制作一个连接四游戏,如果你熟悉它,但我的老师告诉我们绝对没有关于鼠标点击的信息,连接四个部分需要放在我点击的列中,但我不能弄清楚要添加一个鼠标监听器的内容,所以我做了一个Jframe来做它,但我不能让它工作。

所以我需要知道如何使鼠标点击工作以打印点击的x和y坐标。

public class ConnectFourWorld extends World<Piece> implements MouseListener
{
   private String whosTurn;
   private boolean gameOver;
   private String winner;
   Piece X = new Piece("ex", Color.WHITE, Color.RED);
   Piece O = new Piece("oh", Color.YELLOW, Color.BLUE);
   Location column1 = new Location(5, 0);
   Location column2 = new Location(5, 1);
   Location column3 = new Location(5, 2);
   Location column4 = new Location(5, 3);
   Location column5 = new Location(5, 4);
   Location column6 = new Location(5, 5);
   Location column7 = new Location(5, 6);

   public ConnectFourWorld()
   {
    super(new BoundedGrid<Piece>(6,7));
    JFrame frame = new JFrame();
    frame.setSize(new Dimension(350, 300));
    frame.addMouseListener((MouseListener) this);
    frame.pack();
    frame.setVisible(true);
    winner="no winner";
    whosTurn="X";
    gameOver=false;
      setMessage("Welcome to Connect Four World!  - -  Click a spot - "+whosTurn+"'s                     turn.");           
   }

   public boolean locationClicked(Location loc)
   {
      Grid<Piece> grid = getGrid();
      if(grid == null)
        return false;
  ArrayList<Location> reset = new ArrayList<Location>();
  reset  = grid.getOccupiedLocations();


  if(grid.get(loc)!=null)
    return false;

  if(gameOver == true)
  {
      for(int i=0; i<reset.size(); i++){
          grid.remove(reset.get(i));
      }

     gameOver = false;
     winner = "no winner";
       setMessage("Click a spot - " + whosTurn + "'s turn.");
    return true;
  }
  // Create a new location variable = add a piece
  Location place = new Location(0,0);
  place = addPiece(loc);
  if(place.equals(null))
      return false;
  //if location variable is = to null return false
  //call the get winner method and step method
  getWinner(loc);
  step();


  return true;  
   }

   public Location addPiece(Location loc)
   {
    Grid<Piece> grid = getGrid();
   if( grid == null)
  return null;  



 int col = loc.getCol();
 int row = grid.getNumRows() - 1;
 Location spot = new Location(row, col);
 ArrayList<Location> occ = new ArrayList<Location>();
 boolean taken = false;
 occ = grid.getOccupiedLocations();
 for (int i=0; i<occ.size(); i++){
     if(spot.equals(occ.get(i)))
         taken=true;
 }
 //loop that runs as long as location is valid and location is not null
    // move up in the row and add the new Location to that row and col
 while((!spot.equals(null))&&taken==false){
     for (int i=0; i<occ.size(); i++){
         if(spot.equals(occ.get(i)))
             taken=true;
     }
     row++;
 }
 //add a piece to that location and return the location
 if(whosTurn.equals("X"))
     grid.put(spot, X);
 else if(whosTurn.equals("O"))
     grid.put(spot, O);
 return spot;
   }

   //This method is done
public void step()
   {  
    Grid<Piece> grid = getGrid();
    if (grid == null)
       return;
      if(!winner.equals("no winner"))
        {
          setMessage("And the winner is..... " + winner + "\n Click anywhere on board to play again.");
               gameOver = true;

   }
}

    //This one is done   
   public boolean isWorldFull()
   {
    Grid<Piece> grid = getGrid();
       if(grid == null)
       return false;

    ArrayList <Location> list = grid.getOccupiedLocations();
     return (grid.getNumCols() * grid.getNumRows() == list.size());
  }


   public void resetWorld()
   {

   }

   public String getWinner(Location loc)
   {
    return "";
   }
@Override
public void mouseClicked(MouseEvent e) {
    int x=e.getX();
        int y=e.getY();
    System.out.println(x+","+y);
...

我有一个单独的主要类,它将运行此

2 个答案:

答案 0 :(得分:0)

添加鼠标监听器

        addMouseListener(new MouseListener() {

        @Override
        public void mouseReleased(MouseEvent arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void mousePressed(MouseEvent arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void mouseExited(MouseEvent arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void mouseEntered(MouseEvent arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void mouseClicked(MouseEvent arg0) {
            System.out.println(arg0.getLocationOnScreen().x);
            System.out.println(arg0.getLocationOnScreen().y);
        }
    });

答案 1 :(得分:0)

您必须覆盖GridWorld GUIController类。在构造函数中,它们是一个名为display.addMouseListener()的方法。几乎只是在他们的代码中编写代码。我确实相信它们存储了您在变量loc中单击的位置。一旦你知道就很容易。要获取对象,只需执行parentFrame.getWorld()。getGrid()。get(loc)...非常直接...希望有所帮助!