简体   繁体   English

无法解析在公共方法中启动的对象?

[英]Can't resolve an object initiated in a public method?

I'm just starting out with java and I was working on a new GUI.我刚刚开始使用 Java,我正在开发一个新的 GUI。 I made a method that makes my JFrame, sets its default location, close operation, background colour etc.我做了一个方法来制作我的 JFrame,设置它的默认位置,关闭操作,背景颜色等。

I made a separate method for this to keep it out of the main code for the sake of tidiness.为了整洁,我为此创建了一个单独的方法以将其保留在主代码之外。 I already know how to solve this method if I just put all of those things in the main method.如果我把所有这些东西都放在 main 方法中,我已经知道如何解决这个方法了。

This is the code:这是代码:

public class Main {

    public static void makeWindow(){
        JFrame mainWindow = new JFrame();
        mainWindow.setVisible(true);
        mainWindow.setPreferredSize(new Dimension(400,400));
        mainWindow.pack();
        mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainWindow.setLocationRelativeTo(null);
    }

    public static void main(String args[]){
        makeWindow();
        JPanel mainPanel = new JPanel();
        mainWindow.add(mainPanel);
    }
}

I get an error in my main method saying that "mainWindow" can't be resolved.我的主要方法中出现错误,提示无法解析“mainWindow”。 Why is this?为什么是这样? is it because I made the mainWindow object in the other method?是不是因为我在另一个方法中创建了 mainWindow 对象? Is there a way to resolve this issue without putting everything from the makeWindow() method into the main method?有没有办法解决这个问题,而无需将 makeWindow() 方法中的所有内容都放入 main 方法中?

That won't work because mainWindow only exists in the context of your makeWindow method.这是行不通的,因为mainWindow只存在于makeWindow方法的上下文中。 You can make it a static field instead:您可以将其设为静态字段:

public class Main {

    private static JFrame mainWindow; // declare it here so it can be seen from your main method.

    public static void makeWindow(){
        mainWindow = new JFrame();
        mainWindow.setVisible(true);
        mainWindow.setPreferredSize(new Dimension(400,400));
        mainWindow.pack();
        mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainWindow.setLocationRelativeTo(null);
    }

    public static void main(String args[]){
        makeWindow();
        JPanel mainPanel = new JPanel();
        mainWindow.add(mainPanel);
    }
}

EDIT编辑

As @Xing commented (credits shared), you can return the mainWindow from your makeWindow method:正如@Xing 所评论的(分享了学分),您可以从makeWindow方法返回mainWindow

public class Main {

    public static JFrame makeWindow(){
        JFrame mainWindow = new JFrame();
        mainWindow.setVisible(true);
        mainWindow.setPreferredSize(new Dimension(400,400));
        mainWindow.pack();
        mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainWindow.setLocationRelativeTo(null);
        return mainWindow;
    }

    public static void main(String args[]){
        JFrame mainWindow = makeWindow();
        JPanel mainPanel = new JPanel();
        mainWindow.add(mainPanel);
    }
}

create mainWindow instance/ reference globally全局创建 mainWindow 实例/引用

private static JFrame mainWindow;

public static void makeWindow(){
    mainWindow = new JFrame();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM