我无法从java中的较低级别访问“更高级别”变量

时间:2011-12-09 03:02:58

标签: java variables windowbuilder

我最近一直在搞乱windowbuilder pro。我最近遇到的问题是,当按下按钮时,如何将项目添加到列表中。错误是它似乎无法访问列表。这是我的代码:

package wb;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import swing2swt.layout.BorderLayout;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.List;
import swing2swt.layout.BoxLayout;
import swing2swt.layout.FlowLayout;

public class WindowBuilderTest {

/**
 * Launch the application.
 * @param args
 */
public static void main(String[] args) {
    try {
        WindowBuilderTest window = new WindowBuilderTest();
        window.open();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

/**
 * Open the window.
 */
public void open() {
    Display display = Display.getDefault();
    Shell shell = new Shell();
    shell.setSize(450, 300);
    shell.setText("SWT Application");
    shell.setLayout(new BorderLayout(0, 0));

    TabFolder tabFolder = new TabFolder(shell, SWT.NONE);

    TabItem tbtmListTest = new TabItem(tabFolder, SWT.NONE);
    tbtmListTest.setText("List Test");

    Composite composite = new Composite(tabFolder, SWT.NONE);
    tbtmListTest.setControl(composite);
    composite.setLayout(new BorderLayout(0, 0));

    Composite composite_1 = new Composite(composite, SWT.NONE);
    composite_1.setLayoutData(BorderLayout.SOUTH);
    composite_1.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));

    Button button = new Button(composite_1, SWT.NONE);
    button.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseUp(MouseEvent arg0) {
            list.add("Herpity Derpity"); //It screws up here.
        }
    });
    button.setText("+");

    Button button_1 = new Button(composite_1, SWT.NONE);
    button_1.setText("-");

    List list = new List(composite, SWT.BORDER);
    list.setLayoutData(BorderLayout.CENTER);

    TabItem tabItem = new TabItem(tabFolder, SWT.NONE);
    tabItem.setText("New Item");

    Button btnNewButton = new Button(shell, SWT.NONE);
    btnNewButton.setLayoutData(BorderLayout.SOUTH);
    btnNewButton.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseUp(MouseEvent arg0) {
            System.exit(0);
        }
    });
    btnNewButton.setText("Quit");

    shell.open();
    shell.layout();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch()) {
            display.sleep();
        }
    }
    }
}

这是我第一次在stackoverflow上提问,所以我寄予厚望。我把有限的耐心放在你们手中。

3 个答案:

答案 0 :(得分:2)

list的来电之上声明addMouseListener,并将其声明为最终版,例如final List list = ...

答案 1 :(得分:1)

list 变量具有函数作用域(即局部变量),因此在anonymous内部类中不可见。您必须将列表声明为final(SO Thread - Why do we use final keyword with anonymous inner classes?)或声明为WindowBuilderTest类的字段。

public class WindowBuilderTest
{
  List list;
  .....
}

答案 2 :(得分:0)

你应该在使用之前声明清单。

final List list = new List(composite, SWT.BORDER);
Button button = new Button(composite_1, SWT.NONE);
button.addMouseListener(new MouseAdapter() {
    @Override
    public void mouseUp(MouseEvent arg0) {
        list.add("Herpity Derpity"); //It screws up here.
    }
});