数组中的对应元素
Corresponding element in arrays
我在想什么使用new 创建一个JLabel 对象,并将它放在数组中相应的元素中。 (grid[row][col]) 的意思。我正在创建井字游戏 program.I 我正在使用 for 循环来初始化网格。
这是我的 for 循环:
package tic.tac.toe;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TicTacToe extends JFrame implements ActionListener, MouseListener
{
Container content = this.getContentPane();
//Program Arrays
JLabel [][] grid = new JLabel[' '][' '];
char [][ ]game = new char [' '][' '];
// Graphical User-interface Fields
JButton restart = new JButton("Restart");
JPanel p = new JPanel();
JLabel status = new JLabel("Welcome to Tic-Tac-Toe");
//Primitive Fields
int numClicks = 0 ;
boolean isDone = false;
boolean isXTurn = true;
public TicTacToe()
{//GUI
this.setVisible(true);
this.setTitle("Tic-Tac-Toe");
this.setSize(900,500);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//SetLayout
//Add Status Label
content.add(status);
status.setOpaque(true);
status.setBackground(Color.yellow);
status.setForeground(Color.blue);
status.setFont(new Font("Helvetica", Font.BOLD, 12));
//Initailize the Main Panel
p.setLayout(new GridLayout(3,3));
p.setBackground(Color.black);
content.add(p, BorderLayout.CENTER);
//Initialize the grid
for(int row = 0; row<=3;)
{
for (int col = 0; col<=3;)
{
JLabel lbl = new JLabel();
grid[row][col] = lbl;
grid[row][col].addMouseListener(this);
grid[row][col].setOpaque(true);
grid[row][col].setBackground(Color.white);
grid[row][col].setFont(new Font("Helvetica", Font.BOLD, 39));
p.add(grid[row][col]);
}
}
}
@Override
public void actionPerformed(ActionEvent ae)
{
}
@Override
public void mouseClicked(MouseEvent me)
{
}
@Override
public void mousePressed(MouseEvent me)
{
}
@Override
public void mouseReleased(MouseEvent me)
{
}
@Override
public void mouseEntered(MouseEvent me)
{
}
@Override
public void mouseExited(MouseEvent me)
{
}
public static void main(String[] args) {
TicTacToe tTT = new TicTacToe();
}
}
我会给你一些提示,否则对你没有帮助我会做你的功课:
- Java 中的数组是这样定义的:
Type[] varName = new Type[NumberOfOccurences];
或更多维度 Type[][] varName = new Type[NumberOfOccurencesForDimension1][NumberOfOccurencesForDimension2];
。
- 将一个table放入一维数组中需要通过
myArray[rowIndex * colIndex + colIndex] = objectToPutIn;
计算其索引
- 将 table 放入二维数组中很简单:
myArray[rowIndex][colIndex] = objectToPutIn;
- Java 中的数组索引从 0 开始。因此一个元素的数组在索引 0 处。
我在想什么使用new 创建一个JLabel 对象,并将它放在数组中相应的元素中。 (grid[row][col]) 的意思。我正在创建井字游戏 program.I 我正在使用 for 循环来初始化网格。 这是我的 for 循环:
package tic.tac.toe;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TicTacToe extends JFrame implements ActionListener, MouseListener
{
Container content = this.getContentPane();
//Program Arrays
JLabel [][] grid = new JLabel[' '][' '];
char [][ ]game = new char [' '][' '];
// Graphical User-interface Fields
JButton restart = new JButton("Restart");
JPanel p = new JPanel();
JLabel status = new JLabel("Welcome to Tic-Tac-Toe");
//Primitive Fields
int numClicks = 0 ;
boolean isDone = false;
boolean isXTurn = true;
public TicTacToe()
{//GUI
this.setVisible(true);
this.setTitle("Tic-Tac-Toe");
this.setSize(900,500);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//SetLayout
//Add Status Label
content.add(status);
status.setOpaque(true);
status.setBackground(Color.yellow);
status.setForeground(Color.blue);
status.setFont(new Font("Helvetica", Font.BOLD, 12));
//Initailize the Main Panel
p.setLayout(new GridLayout(3,3));
p.setBackground(Color.black);
content.add(p, BorderLayout.CENTER);
//Initialize the grid
for(int row = 0; row<=3;)
{
for (int col = 0; col<=3;)
{
JLabel lbl = new JLabel();
grid[row][col] = lbl;
grid[row][col].addMouseListener(this);
grid[row][col].setOpaque(true);
grid[row][col].setBackground(Color.white);
grid[row][col].setFont(new Font("Helvetica", Font.BOLD, 39));
p.add(grid[row][col]);
}
}
}
@Override
public void actionPerformed(ActionEvent ae)
{
}
@Override
public void mouseClicked(MouseEvent me)
{
}
@Override
public void mousePressed(MouseEvent me)
{
}
@Override
public void mouseReleased(MouseEvent me)
{
}
@Override
public void mouseEntered(MouseEvent me)
{
}
@Override
public void mouseExited(MouseEvent me)
{
}
public static void main(String[] args) {
TicTacToe tTT = new TicTacToe();
}
}
我会给你一些提示,否则对你没有帮助我会做你的功课:
- Java 中的数组是这样定义的:
Type[] varName = new Type[NumberOfOccurences];
或更多维度Type[][] varName = new Type[NumberOfOccurencesForDimension1][NumberOfOccurencesForDimension2];
。 - 将一个table放入一维数组中需要通过
myArray[rowIndex * colIndex + colIndex] = objectToPutIn;
计算其索引
- 将 table 放入二维数组中很简单:
myArray[rowIndex][colIndex] = objectToPutIn;
- Java 中的数组索引从 0 开始。因此一个元素的数组在索引 0 处。