今3日間イライラするのが問題です。小さな三目並べ(nxnの五目並べ)ゲームのUIを書き直さなければなりません。問題は、swing GUIを作成したときに、JButtonプロパティを継承する新しいクラスを作成し、行のintと列のintを追加したことです。SWT(継承なし)ではそれができません。iとjの値をボタンに追加する方法はありますか。
Swingの例は次のとおりです。
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
final MyJButton button = new MyJButton(i, j);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
MoveResult move = game.move(button.getRow(), button.getCol());
switch (move) {
case ValidMove:
button.setBackground(game.getCurrentPlayer().getColor());
game.changePlayer();
break;
}
}
}
}
}
}
動きをチェックするためにテーブルクラスにそれを与えるゲームクラスのiとjを与えます。
if (table.getElement(x, y) != PieceType.NONE) return MoveResult.InvalidMove;
private PieceType[][] table;
SWTで同じことを行う方法はありますか?どんな兆候も歓迎されます。
これは私が作ったものです
buttonpanel = new Composite(shell, SWT.NONE);
buttonpanel.setLayout(new org.eclipse.swt.layout.GridLayout(cols, true));
buttonTable = new Button[rows][cols];
for (int i = 0; i < rows; ++i){
for (int j = 0; j < cols; ++j) {
gridData.heightHint = 45;
gridData.widthHint = 45;
Button button = new Button(buttonpanel, SWT.PUSH);
button.setLayoutData(gridData);
buttonTable[i][j] = button;
buttonTable[i][j].addSelectionListener(new buttSelectionListener());
// buttonpanel.pack();
}
}