问题
I'm trying to create an i*j number of buttons.
On each button I want to add an mouseUp action, but I get an error.
I tried to create a "Line" class to handle the listener event but failed.
This is what I currently have:
protected void createContents() {
// Connect to prolog engine
Query.hasSolution("use_module(library(jpl))"); // only because we call e.g. jpl_pl_syntax/1 below
String t1 = "consult('dots_lines.pl')";
Query.hasSolution(t1);
// Build GUI
shell = new Shell();
shell.setSize(450, 300);
shell.setText("SWT Application");
shell.setLayout(null);
txtDotsAndLines = new Text(shell, SWT.BORDER);
txtDotsAndLines.setBounds(175, 10, 86, 21);
txtDotsAndLines.setText("Dots and lines");
Button btnStartGame = new Button(shell, SWT.NONE);
btnStartGame.setBounds(180, 226, 70, 25);
btnStartGame.setText("Start Game");
// TODO: Play with sizes
int size = 5;
Line[] lines = new Line[size * (size - 1)];
// Buttons are line
for (int i = 0; i < size - 1; i++) {
for (int j = 0; j < size; j++) {
lines[i] = new Line(shell, SWT.NONE);
lines[i].setBounds(100 + i * 35,54 + j * 13,32,11);
lines[i].addMouseListener(new MouseAdapter() {
@Override
public void mouseUp(MouseEvent e) {
lines[i].setEnabled(false);
lines[i].setBackground(Display.getDefault().getSystemColor(SWT.COLOR_GREEN));
String t4 = "move([[line(9,10,cpu),line(11,16,cpu),line(16,17,cpu),line(17,22,p1),line(5,6,p1),line(2,3,p1),line(1,2,p1)],cpu],4,X).";
System.out.println("each solution of " + t4);
}
});
}
}
}
This is the Line class:
public class Line extends Button {
public Line(Composite parent, int style) {
super(parent, style);
// TODO Auto-generated constructor stub
}
protected void checkSubclass() {
// Disable the check that prevents sub-classing of SWT components
}
public void mouseUp(MouseEvent e) {
Line.this.setEnabled(false);
Line.this.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_GREEN));
String t4 = "move([[line(9,10,cpu),line(11,16,cpu),line(16,17,cpu),line(17,22,p1),line(5,6,p1),line(2,3,p1),line(1,2,p1)],cpu],4,X).";
System.out.println("each solution of " + t4);
}
public void pc() {
Line.this.setEnabled(false);
Line.this.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_RED));
}
The error I get is: Local variable i defined in an enclosing scope must be final or effectively final.
Many thanks in advance.
回答1:
You can't reference i
in your mouse adapter anonymous class because its value changes. Instead use:
for (int i = 0; i < size - 1; i++) {
for (int j = 0; j < size; j++) {
final Line line = new Line(shell, SWT.NONE);
lines[i] = line;
line.setBounds(100 + i * 35,54 + j * 13,32,11);
line.addMouseListener(new MouseAdapter() {
@Override
public void mouseUp(final MouseEvent e) {
line.setEnabled(false);
line.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_GREEN));
...
}
});
}
}
Here a final Line line
creates a final instance of a line which can be referenced in the listener.
来源:https://stackoverflow.com/questions/52560744/java-swt-local-variable-i-defined-in-an-enclosing-scope-must-be-final-or-effect