这是一个问题,它让我烦恼了3天。 我必须重写一个tictactoe(n×n的Gomoku)游戏的UI。 问题是,当我创建了swing GUI时,我做了一个继承JButton属性的新类,并为列添加了一个int,并为列添加了一个int。我不能这样做与SWT(没有继承)。有没有办法让我把i和j的值加到按钮上。SWT按钮网格
这里是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;
}
}
}
}
}
}
我给了游戏类给它一个表CLAS检查招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();
}
}
+1 - 我最喜欢setData()。 –
@ AndyThomas-Cramer你是一个绅士 –
所以,我使用setData(),给了我一个错误,该方法移动(int,int)类型游戏不适用于参数(对象,对象),然后我使用(string)cast和parseint来创建一个int,它给了我:java.lang.Integer不能转换为java.lang.String –