Java 访问actionPerformed中的变量

Java 访问actionPerformed中的变量,java,java-ee-6,actionlistener,Java,Java Ee 6,Actionlistener,我必须定义一个名为“path”的变量,这可以在ActionListener内部的代码中看到……但是我不能访问actionPerformed方法之外的“path”变量!它在方法外变为null…我如何访问此变量 button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { System.out.pr

我必须定义一个名为“path”的变量,这可以在ActionListener内部的代码中看到……但是我不能访问actionPerformed方法之外的“path”变量!它在方法外变为null…我如何访问此变量

button.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {

            System.out.println("Search " + textField.getText()
                    + " in Project");

            try {
                System.out.println(project.members().length);
                IResource[] ires = project.members();
                String path = "";
                String findResult = "notFound";
                for (int len = 0; len < ires.length; len++) {
                    if (!(path = loopInFolders(project, ires[len],
                            textField.getText())).equals("")) {
                        System.out.println("found at :" + path);
                        findResult = "found";

                        showResultBox(findResult, path);

                        break;
                    }
                }

                if (path.equals("")) {
                    showResultBox(findResult, path);
                }
            } catch (CoreException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }


        }
    });
button.addActionListener(新ActionListener(){
@凌驾
已执行的公共无效操作(操作事件e){
System.out.println(“搜索”+textField.getText()
+“项目内”);
试一试{
System.out.println(project.members().length);
IResource[]ires=project.members();
字符串路径=”;
字符串findResult=“notFound”;
对于(int len=0;len
甚至我在带有getter和setter的类中将变量定义为全局变量..但是变量没有保留它在actionPerformed中的值!只有在监听器内部才有我想要的正确值…我想在其他方法中使用这个值,但是在那里它变为null

这是我的班级!我想访问execute方法中的path变量,这个变量将在createOutput方法内的actionListener中填充,但在actionListener外它总是空的

public class FindHandler extends AbstractHandler {

private String path;

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {

    Shell shell = HandlerUtil.getActiveShell(event);
    ISelection sel = HandlerUtil.getActiveMenuSelection(event);

    IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
    IProject project = root.getProject(sel.toString()
            .substring(1, sel.toString().indexOf(" ")).trim());
    System.out.println("selected folder "
            + sel.toString().substring(1, sel.toString().indexOf(" "))
                    .trim());



    createOutput(shell, project);

    if (path != null) {
        System.out.println("Pathhhh***444"+ path);
        IPath iPath = new Path(path);
        IFile file = project.getFile(iPath);
        System.out.println("test file*****" + file.getName());

        file = ResourcesPlugin.getWorkspace().getRoot()
                .getFileForLocation(iPath);

        ISelection selection = new StructuredSelection(file);

        IViewReference[] views = PlatformUI.getWorkbench()
                .getActiveWorkbenchWindow().getActivePage()
                .getViewReferences();
        PlatformUI.getWorkbench().getActiveWorkbenchWindow()
                .getActivePage().resetPerspective();

        for (IViewReference view : views) {
            if ("org.eclipse.jdt.ui.PackageExplorer".equals(view.getId())) {
                IViewPart pExplorer = view.getView(true);
                pExplorer.getViewSite().getSelectionProvider()
                        .setSelection(selection);
                break;
            }
        }
    }

    return null;
}

private void createOutput(Shell shell, final IProject project) {

    // Creating the window with a textBox and two buttons of 'Search' and
    // 'Cancel'
    System.out.println(project.getLocation());
    final JTextField textField = new JTextField();
    final JFrame frame = new JFrame("Search File");
    frame.setLayout(null);
    frame.setSize(350, 250);
    frame.setLocation(350, 250);
    JLabel label = new JLabel("Enter File Name:");
    label.setSize(120, 20);
    label.setLocation(20, 25);
    textField.setSize(150, 20);
    textField.setLocation(150, 25);
    frame.getContentPane().add(textField);
    frame.getContentPane().add(label);
    JButton button = new JButton("Search");
    button.setSize(100, 30);
    button.setLocation(70, 100);
    button.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {

            System.out.println("Search " + textField.getText()
                    + " in Project");

            try {
                System.out.println(project.members().length);
                IResource[] ires = project.members();
                path = "";
                String findResult = "notFound";
                for (int len = 0; len < ires.length; len++) {
                    if (!(path = loopInFolders(project, ires[len],
                            textField.getText())).equals("")) {
                        System.out.println("found at :" + path);
                        findResult = "found";

                        showResultBox(findResult, path);

                        break;
                    }
                }

                if (path.equals("")) {
                    showResultBox(findResult, path);
                }
            } catch (CoreException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }


        }
    });
    frame.getContentPane().add(button); // Adds Button to content pane of
                                        // frame
    JButton button_2 = new JButton("Cancel");
    button_2.setSize(100, 30);
    button_2.setLocation(180, 100);
    button_2.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
            frame.dispose();
        }
    });
    frame.getContentPane().add(button_2);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);

    System.out.println("Pathhhh***333"+ path);

}


public String loopInFolders(IProject project, IResource ires,
        String fileName) {

    if (ires.getName().equals(fileName)) {
        return ires.getLocation().toString();
    } else {
        System.out.println(ires.getName());
        IFolder secondFolder = null;
        try {
            secondFolder = project.getFolder(ires.getName());
            System.out.println(secondFolder.members().length);
            if (secondFolder.members().length > 0) {
                IResource[] ires1 = secondFolder.members();
                for (int i = 0; i < ires1.length; i++) {
                    if (!loopInFolders(project, ires1[i], fileName).equals(
                            ""))
                        return ires1[i].getLocation().toString();
                }
            }
        } catch (Exception ex) {
        }
    }
    return "";

}

public void showResultBox(String findResult, String path) {

    if (findResult.equals("notFound")) {

        final JFrame frame = new JFrame("File Not Found");
        frame.setLayout(null);
        frame.setSize(350, 250);
        frame.setLocation(350, 250);
        JLabel label = new JLabel("There is no such file in the project!");
        label.setSize(600, 20);
        label.setLocation(20, 25);

        frame.getContentPane().add(label);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);

    } else {

        System.out.println("Pathhhh***111"+ path);

        final JFrame frame = new JFrame("File Found");
        frame.setLayout(null);
        frame.setSize(350, 250);
        frame.setLocation(350, 250);
        JLabel label = new JLabel("File is found at: " + path);
        label.setSize(600, 20);
        label.setLocation(20, 25);

        frame.getContentPane().add(label);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);

    }

}
公共类FindHandler扩展了AbstractHandler{
私有字符串路径;
@凌驾
公共对象执行(ExecutionEvent事件)引发ExecutionException{
Shell Shell=HandlerUtil.getActiveShell(事件);
ISelection sel=HandlerUtil.getActiveMenuSelection(事件);
IWorkspaceRoot root=ResourcesPlugin.getWorkspace().getRoot();
IProject project=root.getProject(sel.toString()
.substring(1,sel.toString().indexOf(“”).trim());
System.out.println(“选定文件夹”
+sel.toString()子字符串(1,sel.toString().indexOf(“”)
.trim());
createOutput(shell、项目);
if(路径!=null){
System.out.println(“路径HHH***444”+路径);
IPath IPath=新路径(路径);
IFile file=project.getFile(iPath);
System.out.println(“测试文件******”+文件.getName());
file=ResourcesPlugin.getWorkspace().getRoot()
.getFileForLocation(iPath);
ISelection selection=新结构DSelection(文件);
IViewReference[]视图=平台UI.getWorkbench()
.getActiveWorkbenchWindow().getActivePage()
.getViewReferences();
PlatformUI.getWorkbench().getActiveWorkbenchWindow()
.getActivePage().resetPerspective();
对于(IViewReference视图:视图){
if(“org.eclipse.jdt.ui.PackageExplorer.equals(view.getId())){
IViewPart pExplorer=view.getView(true);
pExplorer.getViewSite().getSelectionProvider()
.选举(甄选);
打破
}
}
}
返回null;
}
私有void createOutput(壳牌,最终IProject项目){
//使用一个文本框和两个“搜索”按钮和
//“取消”
System.out.println(project.getLocation());
最终JTextField textField=新JTextField();
最终JFrame=新JFrame(“搜索文件”);
frame.setLayout(空);
框架设置尺寸(350250);
帧设置位置(350250);
JLabel label=新的JLabel(“输入文件名:”);
标签。设置尺寸(120,20);
标签设置位置(20,25);
textField.setSize(150,20);
textField.setLocation(150,25);
frame.getContentPane().add(textField);
frame.getContentPane().add(标签);
JButton按钮=新JButton(“搜索”);
按钮。设置大小(100,30);
按钮。设置位置(70100);
addActionListener(新建ActionListener()){
@凌驾
已执行的公共无效操作(操作事件e){
System.out.println(“搜索”+textField.getText()
+“项目内”);
试一试{
System.out.println(project.members().length);
IResource[]ires=project.members();
路径=”;
字符串findResult=“notFound”;
对于(int len=0;lenpublic class MyClass {
    private String path;

    public MyClass() {
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                path = "";                
                //....//
            }
        }
    }
}
    // make path a global variable
    String path;


    // if you are trying to access path in another class,
    // make sure you have a getter method like 
    public String getPath() {
       return this.path;
    }

    button.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent e) {

        System.out.println("Search " + textField.getText()
                + " in Project");

        try {
            System.out.println(project.members().length);
            IResource[] ires = project.members();
            path = "";
            String findResult = "notFound";
            for (int len = 0; len < ires.length; len++) {
                if (!(path = loopInFolders(project, ires[len],
                        textField.getText())).equals("")) {
                    System.out.println("found at :" + path);
                    findResult = "found";

                    showResultBox(findResult, path);

                    break;
                }
            }

            if (path.equals("")) {
                showResultBox(findResult, path);
            }
        } catch (CoreException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }


    }
});