简体   繁体   中英

Getting a JFrame from the starting method into another method

I have a problem where I'm trying to call a JFrame that is in the public Menu method at the start from another method ( addAButton ) inside the same class but is not working. I have tried calling addAButton inside of public Menu but i cant because I can't put a container in that class. Code :

public class Menu {

    public Menu(Component component) {

        JFrame frame = new JFrame("...");
        frame.setSize(new Dimension(1050, 700));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(component);

        // Set up the content pane.
        try {
            frame.setContentPane(new JLabel(new ImageIcon(ImageIO
                    .read(new File("res/menuBackground.png")))));
        } catch (IOException e) {
            e.printStackTrace();
        }
        addComponentsToPane(frame.getContentPane());

        // Display the window.
        frame.pack();
        frame.setVisible(true);
    }



        public static void addComponentsToPane(Container pane) {

        //some code...

        pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));

        addAButton("SP", "res/Singleplayer.png",
                "res/Singleplayer_pressed.png", pane, true);

      //other buttons...
       }



        public static void addAButton(final String text, String BtnIcon,
            String PressBtnIcon, Container container, Boolean isEnabled) {

        //stuff for buttons...

        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (button.getText().equals("Q")) {
                    System.exit(0);
                } else if (button.getText().equals("SP")) {
                    Component component = new Component();

                     //here I want to put frame.dispose to close this window for when the game window opens.

                    component.start();

                } else if(button.getText().equals("O")) {

                 //here I want to put frame.dispose to close this window for when the options window opens.

                    Component.Options();

                }
            }

        });
    }
}

First, the public Menu block of code is not a method. It's a constructor. Its function is to initialize and prepare the fields of a new object of this class.

The frame variable is a local variable . If you declare a variable inside a block of code, it can only be used inside that block of code. A local variable is thrown away as soon as the block of code where it is declared ends.

If you want to be able to access a data item from different methods, it means that the item is part of the state of the object. That is, the object should be keeping that item inside it through its lifetime, so that the next method that is called on it will have that item available.

When a data item is part of the state of an object, it should be declared as a field . That is, it should not be declared inside any method or constructor, but rather, it should be declared before all the methods and constructors.

Once you have declared a field, you can initialize it in the constructor. And then you can access that field from any of the methods in the same class.

public class Menu {

    private JFrame frame;  // This is the field declaration

    public Menu( Component component ) {

         frame = new JFrame("..."); // Here you just initialize, not declare.

         ... // Do the rest of your initializations

    }

    ... // Other methods
}

Now you can just use the field frame in any method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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