私は戦艦ゲームをプログラムしようとしています。if ステートメントを使用してクラス BatShip3 を呼び出します。これにより、戦艦配置用のクリック可能なグリッドを持つ 2 つの JFrame が生成されます。メソッドにコードを 2 回実装して、それぞれに新しい変数を生成しようとしましたが、何が問題なのかわかりません。ゲーム モードを選択すると、両方のウィンドウが作成され、両方の背景が黒になりますが、2 つ目のウィンドウにはグリッドがありません。ポインタはありますか?
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);
}
}
}
}}