java写五子棋 关于鼠标事件的问题

2024-12-16 02:57:21
推荐回答(2个)
回答1:

我也是初学者,这是我刚刚编写的五子棋源程序,希望对你有帮助,也请多多指教。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class MyFrame extends JFrame{
public static final int LEN = 15;
public static int time = 0;
JPanel panel = new JPanel();
public JButton [][] b = new JButton[LEN][LEN];
public static boolean flag =true;
public static int [] [] message=new int [LEN] [LEN];
public static Color[] [] color=new Color [LEN] [LEN];
public static String word="黑方执棋";
public void setButton(JButton[][] b){
for(int i=0;i for(int j=0;j b[i][j]=new JButton();
b[i][j].setBackground(color[i][j]);
b[i][j].addActionListener(new MyButtonListener(i,j));
}
}
public void addButton(JPanel panel,JButton[][] b){
for(int i=0;i for(int j=0;j panel.add(b[i][j]);
}
}
public static void resetColor(){
for(int i=0;i for(int j=0;j color[i][j]=Color.yellow;
}
public MyFrame(){
super("五子棋");
this.setLocation(130,0);
this.setSize(500,570);
panel.setLayout(new GridLayout(LEN,LEN));
setButton(b);
addButton(panel,b);
this.setVisible(true);
this.add(panel,BorderLayout.CENTER);
JPanel p1=new JPanel();
JLabel l1=new JLabel(word);
p1.add(l1);
this.add(p1,BorderLayout.NORTH);
JPanel p2=new JPanel();
JButton bb=new JButton("新游戏");
bb.addActionListener(new BbListener());
p2.add(bb);
this.add(p2,BorderLayout.SOUTH);

}

public static void resetMessage(){
for(int i=0;i for(int j=0;j message[i][j]=0;
}
}
class MyButtonListener implements ActionListener{
public static final int LEN=MyFrame.LEN;
int i,j;
public MyButtonListener(int i,int j){
this.i=i;
this.j=j;
}
public int whoWins(){
int COR=1;
int result = 0;
if(i>=4&&MyFrame.message[i][j]*MyFrame.message[i-1][j]*MyFrame.message[i-2][j]*MyFrame.message[i-3][j]*MyFrame.message[i-4][j]==COR||i<=(LEN-5)&&MyFrame.message[i][j]*MyFrame.message[i+1][j]*MyFrame.message[i+2][j]*MyFrame.message[i+3][j]*MyFrame.message[i+4][j]==COR||j>=4&&MyFrame.message[i][j]*MyFrame.message[i][j-1]*MyFrame.message[i][j-2]*MyFrame.message[i][j-3]*MyFrame.message[i][j-4]==COR||j<=(LEN-5)&&MyFrame.message[i][j]*MyFrame.message[i][j+1]*MyFrame.message[i][j+2]*MyFrame.message[i][j+3]*MyFrame.message[i][j+4]==COR||i>=4&&j>=4&&MyFrame.message[i][j]*MyFrame.message[i-1][j-1]*MyFrame.message[i-2][j-2]*MyFrame.message[i-3][j-3]*MyFrame.message[i-4][j-4]==COR||i>=4&&j<=(LEN-5)&&MyFrame.message[i][j]*MyFrame.message[i-1][j+1]*MyFrame.message[i-2][j+2]*MyFrame.message[i-3][j+3]*MyFrame.message[i-4][j+4]==COR||i<=(LEN-5)&&j<=(LEN-5)&&MyFrame.message[i][j]*MyFrame.message[i+1][j+1]*MyFrame.message[i+2][j+2]*MyFrame.message[i+3][j+3]*MyFrame.message[i+4][j+4]==COR||i<=(LEN-5)&&j>=4&&MyFrame.message[i][j]*MyFrame.message[i+1][j-1]*MyFrame.message[i+2][j-2]*MyFrame.message[i+3][j-3]*MyFrame.message[i+4][j-4]==COR)
result = 1;
COR=32;
if(i>=4&&MyFrame.message[i][j]*MyFrame.message[i-1][j]*MyFrame.message[i-2][j]*MyFrame.message[i-3][j]*MyFrame.message[i-4][j]==COR||i<=(LEN-5)&&MyFrame.message[i][j]*MyFrame.message[i+1][j]*MyFrame.message[i+2][j]*MyFrame.message[i+3][j]*MyFrame.message[i+4][j]==COR||j>=4&&MyFrame.message[i][j]*MyFrame.message[i][j-1]*MyFrame.message[i][j-2]*MyFrame.message[i][j-3]*MyFrame.message[i][j-4]==COR||j<=(LEN-5)&&MyFrame.message[i][j]*MyFrame.message[i][j+1]*MyFrame.message[i][j+2]*MyFrame.message[i][j+3]*MyFrame.message[i][j+4]==COR||i>=4&&j>=4&&MyFrame.message[i][j]*MyFrame.message[i-1][j-1]*MyFrame.message[i-2][j-2]*MyFrame.message[i-3][j-3]*MyFrame.message[i-4][j-4]==COR||i>=4&&j<=(LEN-5)&&MyFrame.message[i][j]*MyFrame.message[i-1][j+1]*MyFrame.message[i-2][j+2]*MyFrame.message[i-3][j+3]*MyFrame.message[i-4][j+4]==COR||i<=(LEN-5)&&j<=(LEN-5)&&MyFrame.message[i][j]*MyFrame.message[i+1][j+1]*MyFrame.message[i+2][j+2]*MyFrame.message[i+3][j+3]*MyFrame.message[i+4][j+4]==COR||i<=(LEN-5)&&j>=4&&MyFrame.message[i][j]*MyFrame.message[i+1][j-1]*MyFrame.message[i+2][j-2]*MyFrame.message[i+3][j-3]*MyFrame.message[i+4][j-4]==COR)
result = 2;
return result;
}
public void actionPerformed(ActionEvent e){
if(TestMyFrame.f.message[i][j]==0){
if(TestMyFrame.f.flag){
TestMyFrame.f.message[i][j]=1;
TestMyFrame.f.color[i][j]=Color.black;
TestMyFrame.f.word="白方执棋";
}
else{
TestMyFrame.f.message[i][j]=2;
TestMyFrame.f.color[i][j]=Color.white;
TestMyFrame.f.word="黑方执棋";
}
TestMyFrame.f.flag=!TestMyFrame.f.flag;
}

if(whoWins()==1)
{TestMyFrame.f.word="黑方胜";
int i1=0,j1=0;
for(i1=0;i1 if(whoWins()==2)
{TestMyFrame.f.word="白方胜";
int i1=0,j1=0;
for(i1=0;i1
TestMyFrame.f1=TestMyFrame.f;
TestMyFrame.f=new MyFrame();
TestMyFrame.f1.setVisible(false);
}
}

class TestMyFrame{
public static MyFrame f = new MyFrame();
public static MyFrame f1;
public static void main(String [] args){
f.resetColor();
f.resetMessage();
}
}

class BbListener implements ActionListener{
public void actionPerformed(ActionEvent e){
TestMyFrame.f.flag=true;
TestMyFrame.f.resetColor();
TestMyFrame.f.resetMessage();
MyFrame.word="黑方执棋";
TestMyFrame.f.setVisible(false);
TestMyFrame.f=new MyFrame();

}
}

回答2:

这个你到网上下一个,看看别人的,你就会写了,主要的就是鼠标拖动问题