这是我使用JPanel创建国际象棋棋盘的代码。我希望能够使用boardArray并使用asign一个String值来表示Panel的名称。我怎么做?例如:
boardArray[1][5] = "TwoSix";
TwoSix应该是我在for循环中创建的字符串值,但它代表我创建的Panel。
import java.awt.Component;
import java.awt.Container;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.Color;
public class ChessBoardPractice extends JFrame {
private JPanel contentPane;
Component boardArray[][] = new Component[8][8];
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ChessBoardPractice frame = new ChessBoardPractice();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public ChessBoardPractice() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 500, 500);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JPanel OneOne = new JPanel();
OneOne.setBounds(20, 20, 30, 30);
OneOne.setBackground(Color.WHITE);
OneOne.setForeground(Color.BLACK);
//contentPane.add(OneOne);
JPanel TwoTwo = new JPanel();
TwoTwo.setLayout(null);
TwoTwo.setForeground(Color.BLACK);
TwoTwo.setBackground(Color.WHITE);
TwoTwo.setBounds(50, 50, 30, 30);
//contentPane.add(TwoTwo);
JPanel ThreeThree = new JPanel();
ThreeThree.setLayout(null);
ThreeThree.setForeground(Color.BLACK);
ThreeThree.setBackground(Color.WHITE);
ThreeThree.setBounds(80, 80, 30, 30);
//contentPane.add(ThreeThree);
JPanel FourFour = new JPanel();
FourFour.setLayout(null);
FourFour.setForeground(Color.BLACK);
FourFour.setBackground(Color.WHITE);
FourFour.setBounds(110, 110, 30, 30);
//contentPane.add(FourFour);
JPanel FiveFive = new JPanel();
FiveFive.setLayout(null);
FiveFive.setForeground(Color.BLACK);
FiveFive.setBackground(Color.WHITE);
FiveFive.setBounds(140, 140, 30, 30);
//contentPane.add(FiveFive);
JPanel SixSix = new JPanel();
SixSix.setLayout(null);
SixSix.setForeground(Color.BLACK);
SixSix.setBackground(Color.WHITE);
SixSix.setBounds(170, 170, 30, 30);
//contentPane.add(SixSix);
JPanel SevenSeven = new JPanel();
SevenSeven.setLayout(null);
SevenSeven.setForeground(Color.BLACK);
SevenSeven.setBackground(Color.WHITE);
SevenSeven.setBounds(200, 200, 30, 30);
//contentPane.add(SevenSeven);
JPanel EightEight = new JPanel();
EightEight.setLayout(null);
EightEight.setForeground(Color.BLACK);
EightEight.setBackground(Color.WHITE);
EightEight.setBounds(230, 230, 30, 30);
//contentPane.add(EightEight);
JPanel OneThree = new JPanel();
OneThree.setLayout(null);
OneThree.setForeground(Color.BLACK);
OneThree.setBackground(Color.WHITE);
OneThree.setBounds(80, 20, 30, 30);
//contentPane.add(OneThree);
JPanel TwoFour = new JPanel();
TwoFour.setLayout(null);
TwoFour.setForeground(Color.BLACK);
TwoFour.setBackground(Color.WHITE);
TwoFour.setBounds(110, 50, 30, 30);
//contentPane.add(TwoFour);
JPanel ThreeFive = new JPanel();
ThreeFive.setLayout(null);
ThreeFive.setForeground(Color.BLACK);
ThreeFive.setBackground(Color.WHITE);
ThreeFive.setBounds(140, 80, 30, 30);
//contentPane.add(ThreeFive);
JPanel FourSix = new JPanel();
FourSix.setLayout(null);
FourSix.setForeground(Color.BLACK);
FourSix.setBackground(Color.WHITE);
FourSix.setBounds(170, 110, 30, 30);
//contentPane.add(FourSix);
JPanel FiveSeven = new JPanel();
FiveSeven.setLayout(null);
FiveSeven.setForeground(Color.BLACK);
FiveSeven.setBackground(Color.WHITE);
FiveSeven.setBounds(200, 140, 30, 30);
//contentPane.add(FiveSeven);
JPanel SixEight = new JPanel();
SixEight.setLayout(null);
SixEight.setForeground(Color.BLACK);
SixEight.setBackground(Color.WHITE);
SixEight.setBounds(230, 170, 30, 30);
//contentPane.add(SixEight);
JPanel OneFive = new JPanel();
OneFive.setLayout(null);
OneFive.setForeground(Color.BLACK);
OneFive.setBackground(Color.WHITE);
OneFive.setBounds(140, 20, 30, 30);
//contentPane.add(OneFive);
JPanel TwoSix = new JPanel();
TwoSix.setLayout(null);
TwoSix.setForeground(Color.BLACK);
TwoSix.setBackground(Color.WHITE);
TwoSix.setBounds(170, 50, 30, 30);
//contentPane.add(TwoSix);
JPanel ThreeSeven = new JPanel();
ThreeSeven.setLayout(null);
ThreeSeven.setForeground(Color.BLACK);
ThreeSeven.setBackground(Color.WHITE);
ThreeSeven.setBounds(200, 80, 30, 30);
//contentPane.add(ThreeSeven);
JPanel FourEight = new JPanel();
FourEight.setLayout(null);
FourEight.setForeground(Color.BLACK);
FourEight.setBackground(Color.WHITE);
FourEight.setBounds(230, 110, 30, 30);
//contentPane.add(FourEight);
JPanel OneSeven = new JPanel();
OneSeven.setLayout(null);
OneSeven.setForeground(Color.BLACK);
OneSeven.setBackground(Color.WHITE);
OneSeven.setBounds(200, 20, 30, 30);
//contentPane.add(OneSeven);
JPanel TwoEight = new JPanel();
TwoEight.setLayout(null);
TwoEight.setForeground(Color.BLACK);
TwoEight.setBackground(Color.WHITE);
TwoEight.setBounds(230, 50, 30, 30);
//contentPane.add(TwoEight);
JPanel ThreeOne = new JPanel();
ThreeOne.setLayout(null);
ThreeOne.setForeground(Color.BLACK);
ThreeOne.setBackground(Color.WHITE);
ThreeOne.setBounds(20, 80, 30, 30);
//contentPane.add(ThreeOne);
JPanel FourTwo = new JPanel();
FourTwo.setLayout(null);
FourTwo.setForeground(Color.BLACK);
FourTwo.setBackground(Color.WHITE);
FourTwo.setBounds(50, 110, 30, 30);
//contentPane.add(FourTwo);
JPanel FiveThree = new JPanel();
FiveThree.setLayout(null);
FiveThree.setForeground(Color.BLACK);
FiveThree.setBackground(Color.WHITE);
FiveThree.setBounds(80, 140, 30, 30);
//contentPane.add(FiveThree);
JPanel SixFour = new JPanel();
SixFour.setLayout(null);
SixFour.setForeground(Color.BLACK);
SixFour.setBackground(Color.WHITE);
SixFour.setBounds(110, 170, 30, 30);
//contentPane.add(SixFour);
JPanel SevenFive = new JPanel();
SevenFive.setLayout(null);
SevenFive.setForeground(Color.BLACK);
SevenFive.setBackground(Color.WHITE);
SevenFive.setBounds(140, 200, 30, 30);
//contentPane.add(SevenFive);
JPanel EightSix = new JPanel();
EightSix.setLayout(null);
EightSix.setForeground(Color.BLACK);
EightSix.setBackground(Color.WHITE);
EightSix.setBounds(170, 230, 30, 30);
//contentPane.add(EightSix);
JPanel FiveOne = new JPanel();
FiveOne.setLayout(null);
FiveOne.setForeground(Color.BLACK);
FiveOne.setBackground(Color.WHITE);
FiveOne.setBounds(20, 140, 30, 30);
//contentPane.add(FiveOne);
JPanel SixTwo = new JPanel();
SixTwo.setLayout(null);
SixTwo.setForeground(Color.BLACK);
SixTwo.setBackground(Color.WHITE);
SixTwo.setBounds(50, 170, 30, 30);
//contentPane.add(SixTwo);
JPanel SevenThree = new JPanel();
SevenThree.setLayout(null);
SevenThree.setForeground(Color.BLACK);
SevenThree.setBackground(Color.WHITE);
SevenThree.setBounds(80, 200, 30, 30);
//contentPane.add(SevenThree);
JPanel EightFour = new JPanel();
EightFour.setLayout(null);
EightFour.setForeground(Color.BLACK);
EightFour.setBackground(Color.WHITE);
EightFour.setBounds(110, 230, 30, 30);
//contentPane.add(EightFour);
JPanel SevenOne = new JPanel();
SevenOne.setLayout(null);
SevenOne.setForeground(Color.BLACK);
SevenOne.setBackground(Color.WHITE);
SevenOne.setBounds(20, 200, 30, 30);
//contentPane.add(SevenOne);
JPanel EightTwo = new JPanel();
EightTwo.setLayout(null);
EightTwo.setForeground(Color.BLACK);
EightTwo.setBackground(Color.WHITE);
EightTwo.setBounds(50, 230, 30, 30);
//contentPane.add(EightTwo);
JPanel OneTwo = new JPanel();
OneTwo.setLayout(null);
OneTwo.setForeground(Color.BLACK);
OneTwo.setBackground(Color.BLACK);
OneTwo.setBounds(50, 20, 30, 30);
//contentPane.add(OneTwo);
JPanel OneFour = new JPanel();
OneFour.setLayout(null);
OneFour.setForeground(Color.BLACK);
OneFour.setBackground(Color.BLACK);
OneFour.setBounds(110, 20, 30, 30);
//contentPane.add(OneFour);
JPanel OneSix = new JPanel();
OneSix.setLayout(null);
OneSix.setForeground(Color.BLACK);
OneSix.setBackground(Color.BLACK);
OneSix.setBounds(170, 20, 30, 30);
//contentPane.add(OneSix);
JPanel OneEight = new JPanel();
OneEight.setLayout(null);
OneEight.setForeground(Color.BLACK);
OneEight.setBackground(Color.BLACK);
OneEight.setBounds(230, 20, 30, 30);
//contentPane.add(OneEight);
JPanel TwoOne = new JPanel();
TwoOne.setBounds(20, 50, 30, 30);
TwoOne.setLayout(null);
TwoOne.setForeground(Color.BLACK);
TwoOne.setBackground(Color.BLACK);
//contentPane.add(TwoOne);
JPanel TwoThree = new JPanel();
TwoThree.setBounds(80, 50, 30, 30);
TwoThree.setLayout(null);
TwoThree.setForeground(Color.BLACK);
TwoThree.setBackground(Color.BLACK);
//contentPane.add(TwoThree);
JPanel TwoFive = new JPanel();
TwoFive.setBounds(140, 50, 30, 30);
TwoFive.setLayout(null);
TwoFive.setForeground(Color.BLACK);
TwoFive.setBackground(Color.BLACK);
//contentPane.add(TwoFive);
JPanel TwoSeven = new JPanel();
TwoSeven.setBounds(200, 50, 30, 30);
TwoSeven.setLayout(null);
TwoSeven.setForeground(Color.BLACK);
TwoSeven.setBackground(Color.BLACK);
//contentPane.add(TwoSeven);
JPanel ThreeSix = new JPanel();
ThreeSix.setLayout(null);
ThreeSix.setForeground(Color.BLACK);
ThreeSix.setBackground(Color.BLACK);
ThreeSix.setBounds(170, 80, 30, 30);
//contentPane.add(ThreeSix);
JPanel ThreeEight = new JPanel();
ThreeEight.setLayout(null);
ThreeEight.setForeground(Color.BLACK);
ThreeEight.setBackground(Color.BLACK);
ThreeEight.setBounds(230, 80, 30, 30);
//contentPane.add(ThreeEight);
JPanel ThreeFour = new JPanel();
ThreeFour.setLayout(null);
ThreeFour.setForeground(Color.BLACK);
ThreeFour.setBackground(Color.BLACK);
ThreeFour.setBounds(110, 80, 30, 30);
//contentPane.add(ThreeFour);
JPanel ThreeTwo = new JPanel();
ThreeTwo.setLayout(null);
ThreeTwo.setForeground(Color.BLACK);
ThreeTwo.setBackground(Color.BLACK);
ThreeTwo.setBounds(50, 80, 30, 30);
//contentPane.add(ThreeTwo);
JPanel FourOne = new JPanel();
FourOne.setLayout(null);
FourOne.setForeground(Color.BLACK);
FourOne.setBackground(Color.BLACK);
FourOne.setBounds(20, 110, 30, 30);
//contentPane.add(FourOne);
JPanel FourThree = new JPanel();
FourThree.setLayout(null);
FourThree.setForeground(Color.BLACK);
FourThree.setBackground(Color.BLACK);
FourThree.setBounds(80, 110, 30, 30);
//contentPane.add(FourThree);
JPanel FourFive = new JPanel();
FourFive.setLayout(null);
FourFive.setForeground(Color.BLACK);
FourFive.setBackground(Color.BLACK);
FourFive.setBounds(140, 110, 30, 30);
//contentPane.add(FourFive);
JPanel FourSeven = new JPanel();
FourSeven.setLayout(null);
FourSeven.setForeground(Color.BLACK);
FourSeven.setBackground(Color.BLACK);
FourSeven.setBounds(200, 110, 30, 30);
//contentPane.add(FourSeven);
JPanel FiveEight = new JPanel();
FiveEight.setLayout(null);
FiveEight.setForeground(Color.BLACK);
FiveEight.setBackground(Color.BLACK);
FiveEight.setBounds(230, 140, 30, 30);
//contentPane.add(FiveEight);
JPanel FiveTwo = new JPanel();
FiveTwo.setLayout(null);
FiveTwo.setForeground(Color.BLACK);
FiveTwo.setBackground(Color.BLACK);
FiveTwo.setBounds(50, 140, 30, 30);
//contentPane.add(FiveTwo);
JPanel FiveFour = new JPanel();
FiveFour.setLayout(null);
FiveFour.setForeground(Color.BLACK);
FiveFour.setBackground(Color.BLACK);
FiveFour.setBounds(110, 140, 30, 30);
//contentPane.add(FiveFour);
JPanel FiveSix = new JPanel();
FiveSix.setLayout(null);
FiveSix.setForeground(Color.BLACK);
FiveSix.setBackground(Color.BLACK);
FiveSix.setBounds(170, 140, 30, 30);
//contentPane.add(FiveSix);
JPanel SixOne = new JPanel();
SixOne.setLayout(null);
SixOne.setForeground(Color.BLACK);
SixOne.setBackground(Color.BLACK);
SixOne.setBounds(20, 170, 30, 30);
//contentPane.add(SixOne);
JPanel SixThree = new JPanel();
SixThree.setLayout(null);
SixThree.setForeground(Color.BLACK);
SixThree.setBackground(Color.BLACK);
SixThree.setBounds(80, 170, 30, 30);
//contentPane.add(SixThree);
JPanel SixFive = new JPanel();
SixFive.setLayout(null);
SixFive.setForeground(Color.BLACK);
SixFive.setBackground(Color.BLACK);
SixFive.setBounds(140, 170, 30, 30);
//contentPane.add(SixFive);
JPanel SixSeven = new JPanel();
SixSeven.setLayout(null);
SixSeven.setForeground(Color.BLACK);
SixSeven.setBackground(Color.BLACK);
SixSeven.setBounds(200, 170, 30, 30);
//contentPane.add(SixSeven);
JPanel SevenTwo = new JPanel();
SevenTwo.setLayout(null);
SevenTwo.setForeground(Color.BLACK);
SevenTwo.setBackground(Color.BLACK);
SevenTwo.setBounds(50, 200, 30, 30);
//contentPane.add(SevenTwo);
JPanel SevenFour = new JPanel();
SevenFour.setLayout(null);
SevenFour.setForeground(Color.BLACK);
SevenFour.setBackground(Color.BLACK);
SevenFour.setBounds(110, 200, 30, 30);
//contentPane.add(SevenFour);
JPanel SevenSix = new JPanel();
SevenSix.setLayout(null);
SevenSix.setForeground(Color.BLACK);
SevenSix.setBackground(Color.BLACK);
SevenSix.setBounds(170, 200, 30, 30);
//contentPane.add(SevenSix);
JPanel SevenEight = new JPanel();
SevenEight.setLayout(null);
SevenEight.setForeground(Color.BLACK);
SevenEight.setBackground(Color.BLACK);
SevenEight.setBounds(230, 200, 30, 30);
//contentPane.add(SevenEight);
JPanel EightOne = new JPanel();
EightOne.setLayout(null);
EightOne.setForeground(Color.BLACK);
EightOne.setBackground(Color.BLACK);
EightOne.setBounds(20, 230, 30, 30);
//contentPane.add(EightOne);
JPanel EightThree = new JPanel();
EightThree.setLayout(null);
EightThree.setForeground(Color.BLACK);
EightThree.setBackground(Color.BLACK);
EightThree.setBounds(80, 230, 30, 30);
//contentPane.add(EightThree);
JPanel EightFive = new JPanel();
EightFive.setLayout(null);
EightFive.setForeground(Color.BLACK);
EightFive.setBackground(Color.BLACK);
EightFive.setBounds(140, 230, 30, 30);
//contentPane.add(EightFive);
JPanel EightSeven = new JPanel();
EightSeven.setLayout(null);
EightSeven.setForeground(Color.BLACK);
EightSeven.setBackground(Color.BLACK);
EightSeven.setBounds(200, 230, 30, 30);
//contentPane.add(EightSeven);
boardArray[0][0] = OneOne;
boardArray[0][1] = OneTwo;
boardArray[0][2] = OneThree;
boardArray[0][3] = OneFour;
boardArray[0][4] = OneFive;
boardArray[0][5] = OneSix;
boardArray[0][6] = OneSeven;
boardArray[0][7] = OneEight;
boardArray[1][0] = TwoOne;
boardArray[1][1] = TwoTwo;
boardArray[1][2] = TwoThree;
boardArray[1][3] = TwoFour;
boardArray[1][4] = TwoFive;
boardArray[1][5] = TwoSix;
boardArray[1][6] = TwoSeven;
boardArray[1][7] = TwoEight;
boardArray[2][0] = ThreeOne;
boardArray[2][1] = ThreeTwo;
boardArray[2][2] = ThreeThree;
boardArray[2][3] = ThreeFour;
boardArray[2][4] = ThreeFive;
boardArray[2][5] = ThreeSix;
boardArray[2][6] = ThreeSeven;
boardArray[2][7] = ThreeEight;
boardArray[3][0] = FourOne;
boardArray[3][1] = FourTwo;
boardArray[3][2] = FourThree;
boardArray[3][3] = FourFour;
boardArray[3][4] = FourFive;
boardArray[3][5] = FourSix;
boardArray[3][6] = FourSeven;
boardArray[3][7] = FourEight;
boardArray[4][0] = FiveOne;
boardArray[4][1] = FiveTwo;
boardArray[4][2] = FiveThree;
boardArray[4][3] = FiveFour;
boardArray[4][4] = FiveFive;
boardArray[4][5] = FiveSix;
boardArray[4][6] = FiveSeven;
boardArray[4][7] = FiveEight;
boardArray[5][0] = SixOne;
boardArray[5][1] = SixTwo;
boardArray[5][2] = SixThree;
boardArray[5][3] = SixFour;
boardArray[5][4] = SixFive;
boardArray[5][5] = SixSix;
boardArray[5][6] = SixSeven;
boardArray[5][7] = SixEight;
boardArray[6][0] = SevenOne;
boardArray[6][1] = SevenTwo;
boardArray[6][2] = SevenThree;
boardArray[6][3] = SevenFour;
boardArray[6][4] = SevenFive;
boardArray[6][5] = SevenSix;
boardArray[6][6] = SevenSeven;
boardArray[6][7] = SevenEight;
boardArray[7][0] = EightOne;
boardArray[7][1] = EightTwo;
boardArray[7][2] = EightThree;
boardArray[7][3] = EightFour;
boardArray[7][4] = EightFive;
boardArray[7][5] = EightSix;
boardArray[7][6] = EightSeven;
boardArray[7][7] = EightEight;
String SquareName;
String boardArrayName[];
boardArrayName = new String[]{"One","Two","Three","Four","Five","Six","Seven","Eight"};
for(int firstParameter = 0; firstParameter < 8; firstParameter++){
for(int secondParameter = 0; secondParameter < 8; secondParameter++){
SquareName = boardArrayName[firstParameter] + boardArrayName[secondParameter];
contentPane.add(boardArray[firstParameter][secondParameter]);
}
}
}
}
答案 0 :(得分:2)
您可以使用Map跟踪每个名称 - 面板映射,例如本例中的字段panelMap:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
public class ChessBoardPractice extends JFrame {
private final Map<String, JPanel> panelMap = new LinkedHashMap<String, JPanel>();
/**
* Launch the application.
*/
public static void main(final String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
final ChessBoardPractice frame = new ChessBoardPractice();
frame.setVisible(true);
} catch (final Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public ChessBoardPractice() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 500, 500);
final JPanel boardArray[][] = new JPanel[8][8];
final JPanel contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new GridLayout(8, 8));
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
final JPanel panel = new JPanel();
panel.setPreferredSize(new Dimension(20, 20));
boardArray[i][j] = panel;
if ((i + j) % 2 == 0) {
panel.setBackground(Color.WHITE);
panel.setForeground(Color.BLACK);
} else {
panel.setBackground(Color.BLACK);
panel.setForeground(Color.WHITE);
}
}
}
final String boardArrayName[] = new String[] { "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight" };
for (int firstParameter = 0; firstParameter < 8; firstParameter++) {
for (int secondParameter = 0; secondParameter < 8; secondParameter++) {
final String name = boardArrayName[firstParameter] + boardArrayName[secondParameter];
contentPane.add(boardArray[firstParameter][secondParameter]);
panelMap.put(name, boardArray[firstParameter][secondParameter]);
}
}
System.out.println(panelMap);
}
}