Java 如何让用户和计算机轮流单击?
这是测试文件Java 如何让用户和计算机轮流单击?,java,methods,awt,awtrobot,Java,Methods,Awt,Awtrobot,这是测试文件 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(AWTExcept
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();
}
}}
}
我希望用户和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;
}
}
}
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.awt.event.MouseAdapter;
导入java.awt.event.MouseEvent;
导入java.awt.AWTException;
导入java.awt.BorderLayout;
导入javax.swing.*;
导入java.util.Random;
公共类cardsTest扩展JFrame{
JButton k=新JButton();
静态卡面板=新卡();
公共静态void main(字符串[]args){
JFrame=新JFrame();
随机数=新随机数();
重新启动,但=重新启动(机架、面板);
添加(面板、边框布局、中心);
框架。添加(但是,BorderLayout.NORTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
框架。设置尺寸(1400700);
frame.setVisible(true);
整数计数=0;
System.out.println(“时报:+panel.getCount());
对于(int i=0;i我猜您有一个用户单击事件的处理程序。您可以在用户单击处理程序(侦听器)的末尾调用simulationClick()
但是,如果第一步是随机的,您必须在确定是否轮到它的计算机后调用模拟单击()
您需要用更多代码解释您的问题,以获得具体帮助
编辑:查看代码后,调用simulationClick()
的最佳位置似乎在类ACListener
的方法actionPerformed()
中
我不确定你的逻辑,但你是否可以在那些if条件中调用它(如果需要)。例如,仅当卡片匹配时调用它,等等。或者你也可以添加对特定动作事件的检查
希望这会有所帮助。我的意思是,用户采取第一步,计算机采取下一步,然后用户采取第三步。现在,该方法不再等待用户单击。我想要一个类似循环的东西,让计算机和用户都单击按钮。我已经更新了代码。似乎您正在尝试让人们为你写代码?我们很难弄清楚你的代码需要什么,因为1.我们没有写代码,也没有和你一样的思维模式,2.这是一堆乱七八糟的代码,几乎没有人愿意通读它,3.你的问题不太清楚。但是!!是什么阻止你使用布尔值来检查Ck如果电脑有一个回合?如果有,那就轮到玩家了?从你所说的需要一个循环…你已经知道该怎么做了!!做吧:)
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;
}
}
}