0

我正在尝试编写战舰游戏,并使用 if 语句调用 BatShip3 类,该类将生成两个带有可单击网格的 JFrame,用于放置战舰。我已经尝试在该方法中实现我的代码两次,为每个生成新变量,但我似乎无法弄清楚出了什么问题。当我选择游戏模式时,它会创建两个窗口,给它们两个黑色背景,但第二个窗口上没有网格。任何指针?

package batship2;

import java.awt.Color;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
*
 * @author William
*/
public class BatShip3 extends JFrame implements ActionListener{


    public JButton buttons [][] = new JButton [100][100];
    public JPanel panel;
    public JButton clicks [][] = new JButton [100][100];
    public JPanel canvas;
    Container contentArea = getContentPane ();
    Container contentArea2 = getContentPane ();

    public void BatShip3(){
            JFrame window1 = new JFrame("Window 1");{
                   setSize (800, 600);
                   setVisible (true);
                   setBackground (Color.black);
                   setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

                   panel = new JPanel();
                   GridLayout experimentLayout = new GridLayout(100, 100);
                   panel.setLayout (experimentLayout);
                   panel.setBackground(Color.black);

                   for(int rows = 0; rows <100 ; rows++){
                      for(int cols = 0; cols < 100 ; cols++){
                          buttons [rows][cols] = new JButton ();
                          buttons [rows][cols].setBackground(Color.blue);
                          buttons [rows][cols].addActionListener(this);
                          panel.add(buttons [rows][cols]);
                      }     
                   }
                   contentArea.add(panel);
                   window1.setContentPane(contentArea);
    }        


      JFrame window2 = new JFrame("Window 2");{
           window2.setSize (800, 600);
           window2.setVisible (true);
           window2.setBackground (Color.black);
           window2.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

           canvas = new JPanel();
           GridLayout experimentLayout2 = new GridLayout(100, 100);
           canvas.setLayout (experimentLayout2);
           canvas.setBackground(Color.black);

             for(int rows1 = 0; rows1 <100 ; rows1++){
                 for(int cols1 = 0; cols1 < 100 ; cols1++){
                     clicks [rows1][cols1] = new JButton ();
                     clicks [rows1][cols1].setBackground(Color.blue);
                     clicks [rows1][cols1].addActionListener(this);
                     canvas.add(clicks [rows1][cols1]);
                 }     
             }
              contentArea2.add(canvas);
              window2.setContentPane(contentArea2);
              }
              }
    public void actionPerformed(ActionEvent ev){

        for(int rows = 0; rows < 100 ; rows++){
            for(int cols = 0 ; cols < 100 ; cols++){
                 if(ev.getSource() == buttons [rows][cols]){            
                      buttons[rows][cols].setBackground(Color.green);
                 }
            }
        }
        for(int rows1 = 0; rows1 < 100 ; rows1++){
            for(int cols1 = 0 ; cols1 < 100 ; cols1++){
                if(ev.getSource() == clicks [rows1][cols1]){

                   clicks[rows1][cols1].setBackground(Color.green);
                 }
            }
        }
}}
4

1 回答 1

1

定义描述战舰游戏的模型类。这是我几分钟后想到的。

package com.ggl.battleship.model;

import java.util.List;

public class BattleshipGame {
    private int numberOfPlayers;
    private int widthOfBattleshipGrid;
    private List<BattleshipGrid> grids;
}

.

package com.ggl.battleship.model;

import java.util.List;

public class BattleshipGrid {
    private List<Ship> ships;
    private Cell[][] grid;
}

.

package com.ggl.battleship.model;

import java.awt.Point;
import java.util.List;

public class Ship {
    private int length;
    private String name;
    private List<Point> cellLocations;
}

.

package com.ggl.battleship.model;

import java.awt.Point;

public class Cell {
    private boolean isHit;
    private Point coordinates;
}

您可以创建所有适当的 getter、setter 和绘图方法。

定义一个或多个创建 JFrame 和 JPanel 的视图类。JPanel 绘制模型类。

为播放器 1 创建 JFrame 视图类的一个实例,为播放器 2 创建另一个实例。

于 2013-04-27T12:09:27.393 回答