有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何让用户和计算机轮流点击?

这是测试文件

while(count<52){

    System.out.println("the times:"+panel.getCount());

    if(panel.getCount()==3){
    for(int i=0;i<2;i++){
    try{
        int temp=number.nextInt(51);
        panel.simulationClick(temp);
    }
    catch(AWTException e){
        e.printStackTrace();
    }
    }}
    }

这是一种让计算机点击按钮的方法

public void simulationClick(int dex) throws AWTException
{

    Point p1 = bt[dex].getLocation();

    System.out.println(p1.x + "," + p1.y);

    Point p = MouseInfo.getPointerInfo().getLocation();
    int x = p.x;
    int y = p.y;
    robot = new Robot();
    System.out.println(x + "," + y);
    robot.mouseMove(p1.x+100, p1.y+100);
    robot.mousePress(InputEvent.BUTTON1_MASK);
    robot.mouseRelease(InputEvent.BUTTON1_MASK);
    robot.mouseMove(x, y);

}

我希望用户和simulationClick轮流进行,这意味着该方法在用户单击按钮后工作。然后这个方法又起作用了!我该怎么做?谢谢大家!

这是所有的代码,如果你需要的话

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.AWTException;
import java.awt.BorderLayout;
 import javax.swing.*;
import java.util.Random;
public class cardsTest extends JFrame  {



JButton k=new JButton();
static Cards panel=new Cards();
public static void main(String[] args){

    JFrame frame=new JFrame();
    Random number=new Random();


    restart but=new restart(frame,panel);
    frame.add(panel,BorderLayout.CENTER);
    frame.add(but,BorderLayout.NORTH);

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(1400,700);
    frame.setVisible(true);

    int count=0;



    System.out.println("the times:"+panel.getCount());


    for(int i=0;i<2;i++){
    try{
        int temp=number.nextInt(51);
        panel.simulationClick(temp);
    }
    catch(AWTException e){
        e.printStackTrace();
    }
    }




}


}


import java.awt.*;
import java.awt.event.*;
import java.util.Collections;
import javax.swing.*;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;
import java.util.Random;
public class Cards extends JPanel {
private FlowLayout grid1;
private JTextField text1;
private JTextField text2;
private JTextField text3;
 private Robot robot;
 private JButton k;
 ArrayList<Integer> rn= new ArrayList<Integer>();


String score1,score2,turn;
int count=0;
int countS=0;
int record=0;
int p1=0;
int p2=0;
Timer time=new Timer();

JButton []bt=new JButton[52];
ImageIcon tail=new ImageIcon(getClass().getResource("b1fv.png"));

ImageIcon ori;
public Cards(){
    for(int i=0;i<52;i++){
        rn.add(i);
    }
    Random number=new Random();

    grid1=new FlowLayout();
    setLayout(grid1);
    for(int i=0;i<bt.length;i++){
        ImageIcon c=new ImageIcon(getClass().getResource(i+1+".png"));

        bt[i]=new JButton(c);
        bt[i].addActionListener(new ACListener(i));
        bt[i].addMouseListener(new MouseAdapter()
        {
            public void mouseClicked(MouseEvent event)
            {
                if (event.getButton() == MouseEvent.BUTTON1)
                    System.out.println(bt[1].getText() + "鼠标左键点击");
                if (event.getButton() == MouseEvent.BUTTON3)
                    System.out.println(bt[1].getText() + "鼠标右键点击");
            }
        });
        bt[i].setSize(5,5);
        //flip(i);
        flip(i);

    }



    Collections.shuffle(rn);

    for(int i=0;i<52;i++){

        add( bt[rn.get(i)]);
    }


    text1=new JTextField(score1);
    text1.setText("Player 1: 0");
    text2=new JTextField(score2);
    text2.setText("Player 2: 0");
    text3=new JTextField();
    text3.setText("Player 1's turn.\n"+"This is turn 0.");


    add(text1);
    add(text2);
    add(text3);








    addMouseMotionListener(new MouseMotionAdapter()
    {
        public void mouseMoved(MouseEvent event)
        {
        }
    });


}



private class ACListener implements ActionListener {
    private  final int index;


    public ACListener(int index) {
        this.index = index;

    }




    public void actionPerformed(ActionEvent e) {
          System.out.println("Mouse entered for rating " + index);
        ori=new ImageIcon(getClass().getResource(index+1+".png"));

        ori.setDescription("ori");
        tail.setDescription("tail");

        if (((ImageIcon) bt[index].getIcon()).getDescription()=="ori")
            bt[index].setIcon(tail);
         else
            bt[index].setIcon(ori);
       count++;

       System.out.printf("Action Performed %d times \n",count);

       TimerTask taskFlip1=new TimerTask(){
        public void run(){
            flip(index);
        }
       };

       TimerTask taskFlip2=new TimerTask(){
            public void run(){
                flip(record);
            }
          };

       if(count==1){
           record=index;
           countS++;

       }
       String turnS=Integer.toString(countS);
       text3.setText("Player 1's turn.\n"+"This is turn "+turnS);
       if(count==2){
           int match1=record/4;
           int match2=index/4;
          if(match1==match2&&record!=index){
              p1++;
              score1=Integer.toString(p1);
              text1.setText("Player 1: "+score1);
              text3.setText("Player 2's turn.\n"+"This is turn "+turnS);
              bt[index].setEnabled(false);
              bt[record].setEnabled(false);
          }
          if(record==index){
              text3.setText("Player 1's turn.\n"+"This is turn "+turnS);
          }
          if(match1!=match2){
           time.schedule(taskFlip1,1000);
           time.schedule(taskFlip2,1000);}
          text3.setText("Player 2's turn.\n"+"This is turn "+turnS);


       }

       if(count==3){
           record=index;
       }
       if(count==4){
           int match1=record/4;
           int match2=index/4;
          if(match1==match2&&record!=index){
              p2++;
              score2=Integer.toString(p2);
              text2.setText("Player 2: "+score2);
              text3.setText("Player 1's turn.\n"+"This is turn "+turnS);
             bt[index].setEnabled(false);
              bt[record].setEnabled(false);
          }
          if(record==index){
              text3.setText("Player 1's turn.\n"+"This is turn "+turnS);
          }
          if(match1!=match2){
           time.schedule(taskFlip1,1000);
           time.schedule(taskFlip2,1000);}
          text3.setText("Player 1's turn.\n"+"This is turn "+turnS);
           count=0;
       }

    }

}

public void flip(int i){
    ori=new ImageIcon(getClass().getResource(i+1+".png"));
    ori.setDescription("ori");
    tail.setDescription("tail");

    if (((ImageIcon) bt[i].getIcon()).getDescription()=="ori")
        bt[i].setIcon(tail);
     else
        bt[i].setIcon(ori);
}

public void simulationClick(int dex) throws AWTException
{

    Point p1 = bt[dex].getLocation();

    System.out.println(p1.x + "," + p1.y);

    Point p = MouseInfo.getPointerInfo().getLocation();
    int x = p.x;
    int y = p.y;
    robot = new Robot();
    System.out.println(x + "," + y);
    robot.mouseMove(p1.x+100, p1.y+100);
    robot.mousePress(InputEvent.BUTTON1_MASK);
    robot.mouseRelease(InputEvent.BUTTON1_MASK);
    robot.mouseMove(x, y);

}

public  int getCount(){
    return count;
}
}



import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
public class restart extends JPanel {
private JButton start;
private int count=0;
public restart(JFrame fr,JPanel pa){
    AListener act=new AListener(fr,pa);
    start=new JButton("Restart");
    start.addActionListener(act);
    add(start);
}

public class AListener implements ActionListener{
    private JFrame f;
    private JPanel p;
    public AListener(JFrame f,JPanel p){
        this.f=f;
        this.p=p;
    }
public void actionPerformed(ActionEvent event){

    count++;
         f.remove(p);  
        f.setVisible(false);
        Cards panel=new Cards();
        f.add(panel,BorderLayout.CENTER);

        for(int i=0;i<10;i++){
            try
            {panel.simulationClick(i);
            }
            catch(AWTException e){
                e.printStackTrace();
            }
        }
        f.setVisible(true);

        p=panel;

    }
}
}

共 (1) 个答案

  1. # 1 楼答案

    我猜您有一个用户点击事件的处理程序。您可以在用户单击处理程序(侦听器)的末尾调用simulationClick()

    但是,如果第一步是随机的,那么在确定是否轮到simulationClick()计算机后,您必须调用它一次

    您需要用更多代码解释您的问题,以获得具体帮助

    编辑:查看代码后,调用simulationClick()的最佳位置似乎是在类ACListener的方法actionPerformed()

    我不确定你的逻辑,但如果你能在那些if条件中调用它(如果需要的话)。例如,仅当卡片匹配时才调用它,或者您也可以添加对特定动作事件的检查

    希望有帮助