We have an assigment of making a game of tic-tac-toe in SWT and have problems using the listener.
We are using an matrix of 3x3 and for-loop for the buttons.
Our code look like this:
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.*;
import org.eclipse.swt.layout.*;
public class Tictactoe {
public static void main(String[] args) {
int str = 9;
int size = str/3;
String toggle = "";
int [][] matris = new int[str][str];
Button [][] knappmatris = new Button [size][size];
Display display = new Display();
Shell shell = new Shell(display);
shell.setText("Tic-Tac-Toe!");
shell.setSize(500, 500);
shell.setLayout(new GridLayout(3, true));
// GridData gridData = new GridData(GridData.FILL_BOTH);
for (int i=0; i<3; i++)
for (int k=0; k<3; k++)
{
GridData gridData = new GridData();
gridData.horizontalAlignment = GridData.FILL;
gridData.verticalAlignment = GridData.FILL;
gridData.grabExcessHorizontalSpace = true;
gridData.grabExcessVerticalSpace = true;
knappmatris[i][k] = new Button(shell, SWT.TOGGLE);
knappmatris[i][k].setLayoutData(gridData);
knappmatris[i][k].setText(toggle);
knappmatris[i][k].addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent selectionEvent)
{
SOMETHING = ((Button)(selectionEvent.widget)).getText();
}
});
}
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
}
Somehow, but were finding it difficult to know what to replace SOMETHING with. Also, we need to put an X or O every other time you click on one of the buttons which are related. We tried to use toggle, knappmatris[i][k] but it just says that we cannot refer to a non-final variable inside a inner class defined in another method. but we dont want it do be final since the 1st it will show X, next time O and so on (with if()...)
/Manne & Jonas
Edited by Darkone85, 21 April 2010 - 07:32 AM.


Sign In
Create Account


Back to top









