福尔摩静 2016-06-18 10:12 采纳率: 0%
浏览 1726
已结题

java连连看数字换图片后重列的问题

我有一个源代码,是只有数字的,然后我加入了调用图片并且修改了一点代码后
不知道为什么点击重列就有问题了。reload函数那里是不是缺了什么,拜托各位大神帮我解决一下了~~~小女子谢过~
下面是我的代码:
import javax.swing.*; //AWT的扩展

import java.awt.*; //抽象窗口工具包
import java.awt.event.*;
import java.io.*;
class lianliankan implements ActionListener //实现ActionListener接口
{
JFrame mainFrame; //主面板 ,JFrame定义的是一个容器,可向里面添加组件
Container thisContainer; //定义一个容器
JPanel centerPanel,southPanel,northPanel; //子面板 ,JPanel是个轻量级容器,可添加入JFrame中
JButton diamondsButton[][] = new JButton[6][5];//定义存储游戏按钮的数组
JButton exitButton,resetButton,newlyButton; //定义退出,重列,重新开始按钮
JLabel fractionLable=new JLabel("0"); //定义分数标签,并初始化为0.
JButton firstButton,secondButton; //分别记录两次先后被选中的按钮
int grid[][] = new int[8][7];//储存游戏按钮位置
static boolean pressInformation=false; // 声明了一个静态布尔类型的变量,判断是否有按钮被选中
int x0=0,y0=0,x=0,y=0,fristMsg=0,secondMsg=0,validateLV; //游戏按钮的位置坐标
int i,j,k,n;//消除方法控制

//调用图片//

private static Icon[] icons=new ImageIcon[6*6];
private static final String imgDir="c:/宠物连连看";
static{
try
{
File dir=new File(imgDir);
File[] imgFiles=dir.listFiles(new FilenameFilter(){
public boolean accept(File dir,String name){
return name.toLowerCase().endsWith(".jpg");
}
});
for(int i=0;i<10*10;i++){
icons[i]=new ImageIcon(imgFiles[i].getAbsolutePath());
}
}catch(Exception e){
e.printStackTrace();
}
}

public void init(){ //init方法
mainFrame=new JFrame("连连看"); //定义主面板为命名为“连连看”的JFrame容器
thisContainer = mainFrame.getContentPane(); //初始化mainFrame.
thisContainer.setLayout(new BorderLayout()); //定义布局为东西南北中的形式
centerPanel=new JPanel(); //初始化centerPanel
southPanel=new JPanel(); //初始化southPanel
northPanel=new JPanel(); //初始化northPanel
thisContainer.add(centerPanel,"Center"); //将centerPanel加入mainFrame中的Center位置
thisContainer.add(southPanel,"South"); //将southPanel加入mainFrame中的South位置
thisContainer.add(northPanel,"North"); //将northPanel加入mainFrame中的North位置
centerPanel.setLayout(new GridLayout(6,5)); //将centerPanel初始化为6*5的网格布局
for(int cols = 0;cols < 6;cols++){ //依次对第0列到第5列进行操作
for(int rows = 0;rows < 5;rows++ ){ //依次对第0行到第4行进行操作
diamondsButton[cols][rows]=new JButton(String.valueOf(grid[cols+1][rows+1]));//新建按钮
diamondsButton[cols][rows]=new JButton(icons[grid[cols+1][rows+1]-1]);
diamondsButton[cols][rows].addActionListener(this);//向此按键添加动作监听以接收来自它的动作
centerPanel.add(diamondsButton[cols][rows]);//将按键添加到centerPanel中
}
}
exitButton=new JButton("退出"); //新建“退出”按钮
exitButton.addActionListener(this); //向“退出”按钮添加事件监听
exitButton.setPreferredSize(new Dimension(150, 50));
exitButton.setFont(new Font("黑体", Font.PLAIN, 25));
//exitButton.setForeground(Color.red);
//exitButton.setBackground(Color.white);
resetButton=new JButton("重列"); //新建“重列”按钮
resetButton.addActionListener(this); //向“重列”按钮添加事件监听
resetButton.setPreferredSize(new Dimension(150, 50));
resetButton.setFont(new Font("黑体", Font.PLAIN, 25));
newlyButton=new JButton("再来一局"); //新建“再来一局”按钮
newlyButton.addActionListener(this); //向“再来一局”按钮添加事件监听
newlyButton.setPreferredSize(new Dimension(150, 50));
newlyButton.setFont(new Font("黑体", Font.PLAIN, 25));
southPanel.add(exitButton); //将“退出”按钮添加到southPanel
southPanel.add(resetButton); //将“重列”按钮添加到southPanel
southPanel.add(newlyButton); //将“再来一局”按钮添加到southPanel
fractionLable.setText(String.valueOf(Integer.parseInt(fractionLable.getText())));
northPanel.add(fractionLable); //将“分数”标签加入northPanel
fractionLable.setPreferredSize(new Dimension(50,50));
fractionLable.setFont(new Font("黑体", Font.PLAIN, 25));
mainFrame.setBounds(280,100,1000,950); // x:X轴上的起点 ,y:Y轴上的起点 , width:长度, height:宽度
mainFrame.setVisible(true); //框架可见
}
public void randomBuild() {
int randoms,cols,rows;
for(int twins=1;twins<=15;twins++) {
randoms=(int)(Math.random()*25+1); //在1-25之间随机产生一个数字
for(int alike=1;alike<=2;alike++) {
cols=(int)(Math.random()*6+1); //选中一个网格
rows=(int)(Math.random()*5+1);
while(grid[cols][rows]!=0) {
cols=(int)(Math.random()*6+1); //如果该格已经存在数字,则重新选择网格
rows=(int)(Math.random()*5+1);
}
this.grid[cols][rows]=randoms; //将随机产生的数字放入网格中
}
}
}
public void fraction(){
fractionLable.setText(String.valueOf(Integer.parseInt(fractionLable.getText())+100));//在原有数字上加100分
}
public void reload() {
int save[] = new int[30];
int n=0,cols,rows;
int grid[][]= new int[8][7];
for(int i=0;i<=6;i++) {
for(int j=0;j<=5;j++) {
if(this.grid[i][j]!=0) {
save[n]=this.grid[i][j]; //将现在任然存在的数字存入save中
n++;
}
}
}
n=n-1;
this.grid=grid;
while(n>=0) {
cols=(int)(Math.random()*6+1);
rows=(int)(Math.random()*5+1); //重新选择格子
while(grid[cols][rows]!=0) {
cols=(int)(Math.random()*6+1);
rows=(int)(Math.random()*5+1); //如果格子非空,则重新选择格子
}
this.grid[cols][rows]=save[n];//将save中的数字放入所选择的的格子中
n--;
}
mainFrame.setVisible(false);
pressInformation=false; //这里一定要将按钮点击信息归为初始
init(); //调用init
for(int i = 0;i < 6;i++){
for(int j = 0;j < 5;j++ ){
if(grid[i+1][j+1]==0) //如果格子的值为0
diamondsButton[i][j].setVisible(false); //则按键不可见
}
}
}
public void estimateEven(int placeX,int placeY,JButton bz)
{
if(pressInformation==false) //尚未有按键被选中
{
x=placeX;

y=placeY;
secondMsg=grid[x][y]; //将该键的值存储到secondMsg
secondButton=bz; //令secondButton为按下的键
pressInformation=true; //标示已经有键被选中
}
else { //已经有按键被选中

x0=x; //将second中的值转存到first
y0=y;
fristMsg=secondMsg;
firstButton=secondButton;
x=placeX;
y=placeY; //将该键存入second
secondMsg=grid[x][y];
secondButton=bz;
if(fristMsg==secondMsg && secondButton!=firstButton){ //如果两个格子数字相等且格子不相同
xiao(); //调用xiao方法
}
}
}
public void xiao() {
if((x0==x &&(y0==y+1||y0==y-1)) || ((x0==x+1||x0==x-1)&&(y0==y))){ //判断是否相邻
remove(); //调用remove
}
else{
for (j=0;j if (grid[x0][j]==0){ //判断第一个按钮同行哪个按钮为空
if (y>j) { //如果第二个按钮的Y坐标大于空按钮的Y坐标说明第一按钮在第二按钮左边
for (i=y-1;i>=j;i-- ){ //判断第二按钮左侧直到第一按钮中间有没有按钮
if (grid[x][i]!=0) {
k=0;
break;
}
else{ k=1; } //K=1说明通过了第一次验证
}
if (k==1) {
linePassOne();
}
}
if (y for (i=y+1;i if (grid[x][i]!=0){
k=0;
break;
}
else { k=1; }
}
if (k==1){
linePassOne();
}
}
if (y==j ) {
linePassOne();
}
}
if (k==2) {
if (x0==x) {
remove();
}
if (x0 for (n=x0;n if (grid[n][j]!=0) {
k=0;
break;
}
if(grid[n][j]==0 && n==x-1) {
remove();
}
}
}
if (x0>x) {
for (n=x0;n>=x+1 ;n-- ) {
if (grid[n][j]!=0) {
k=0;
break;
}
if(grid[n][j]==0 && n==x+1) {
remove();
}
}
}
}
}
for (i=0;i if (grid[i][y0]==0) {
if (x>i) {
for (j=x-1;j>=i ;j-- ) {
if (grid[j][y]!=0) {
k=0;
break;
}
else { k=1; }
}
if (k==1) {
rowPassOne();
}
}
if (x for (j=x+1;j if (grid[j][y]!=0) {
k=0;
break;
}
else { k=1; }
}
if (k==1) {
rowPassOne();
}
}
if (x==i) {
rowPassOne();
}
}
if (k==2){
if (y0==y) {
remove();
}
if (y0 for (n=y0;n if (grid[i][n]!=0) {
k=0;
break;
}
if(grid[i][n]==0 && n==y-1) {
remove();
}
}
}
if (y0>y) {
for (n=y0;n>=y+1 ;n--) {
if (grid[i][n]!=0) {
k=0;
break;
}
if(grid[i][n]==0 && n==y+1) {
remove();
}
}
}
}
}
}
}
public void linePassOne(){
if (y0>j){ //第一按钮同行空按钮在左边
for (i=y0-1;i>=j ;i-- ){ //判断第一按钮同左侧空按钮之间有没按钮
if (grid[x0][i]!=0) {
k=0;
break;
}
else { k=2; } //K=2说明通过了第二次验证
}
}
if (y0 for (i=y0+1;i if (grid[x0][i]!=0) {
k=0;
break;
}
else{ k=2; }
}
}
}
public void rowPassOne(){
if (x0>i) {
for (j=x0-1;j>=i ;j-- ) {
if (grid[j][y0]!=0) {
k=0;
break;
}
else { k=2; }
}
}
if (x0<i) {
for (j=x0+1;j<=i ;j++ ) {
if (grid[j][y0]!=0) {
k=0;
break;
}
else { k=2; }
}
}
}
public void remove(){
firstButton.setVisible(false); //第一个按键不可见
secondButton.setVisible(false); //第二个按键不可见
fraction(); //调用fraction, 在当前分数上加100分
pressInformation=false; //使该静态变量还原为false
k=0;
grid[x0][y0]=0; //清除该游戏按钮
grid[x][y]=0; //清除该游戏按钮
}
public void actionPerformed(ActionEvent e) {

if(e.getSource()==newlyButton){ //如果newlyButton产生动作
int grid[][] = new int[8][7]; //产生新的存储按键的数组
this.grid = grid;
randomBuild(); //调用randomBuild
mainFrame.setVisible(false); //使当前mainFrame不可见
pressInformation=false; //使该静态变量还原为false
init(); //调用init
}
if(e.getSource()==exitButton) //如果exitButton产生动作
System.exit(0); //退出程序
if(e.getSource()==resetButton) //如果resetButton产生动作
reload(); //调用reload
for(int cols = 0;cols < 6;cols++){
for(int rows = 0;rows < 5;rows++ ){
if(e.getSource()==diamondsButton[cols][rows]) //如果游戏按键产生动作
estimateEven(cols+1,rows+1,diamondsButton[cols][rows]); //调用estimateEven
}
}
}
public static void main(String[] args) {
lianliankan llk = new lianliankan(); //初始化
llk.randomBuild(); //调用randomBuild
llk.init(); //调用init
}
}

图片说明

  • 写回答

2条回答 默认 最新

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog