简体   繁体   中英

How can I reference or access objects such as buttons made in another method in Java Swing?

I don't know if I am using the correct terminology in my question, sorry in advance if it sounds a bit confusing. So basically I am trying to write a Java Swing program to create a GUI. There are multiple components/objects that are more or less uniform throughout the GUI so I made separate methods to create different objects and panels and combined them together in the master panel that will be passed back to the frame .

I laid out the entire GUI's layout and look and feel, but it is barely functional yet. My problem is that I have action listeners that need to reference or access buttons or objects that was made in and added to a panel in another method/class and passed back as a panel. For example,

    class makeComponents extends JPanel {
        public makeComponents() { 

        }

        public JToggleButton ToggleButton() {
          // Creates On/Off buttons as switches
            JToggleButton button = new JToggleButton("OFF", false);
            button.addActionListener(new ActionListener() {
                AbstractButton abstractButton = (AbstractButton)e.getSource();
                if (abstractButton.isSelected()) {
                    abstractButton.setText("ON");
                  // Want to add CardLayout card.show() to switch pictures
                }
                else {
                    abstractButton.setText("OFF");
                  // Want to add CardLayout card.show() to switch pictures
                }
            }
            return button;
         }
     }

I have an on/off button like the above on a separate left JPanel then I have 2 images in CardLayout on right JPanel and the 2 panels are joined together in master JPanel which is passed back to the frame. I have created so many layers that I don't know how to backtrack and access that the pics so I can flip the CardLayout of the 2 images in the other panel.

I hope I was clear enough and any help would be much appreciated. Thank you in advance!

You can simply merge all in one class that have private classes inside, then you have acces to master clas objects like this:

public class Master {

    int x;
    int y;
    Inside1 inside;
    Inside2 in2;
    private class Inside1{
        public Inside1(){
            foo();
        }
        private void foo(){
            x=5; //Master.x <---
        }
    }

    private class Inside2{
        public Inside2(){
            inside = new Inside1(); //<- new object of inside1 form Master 
            bar();
        }

        private  void bar(){
            y=15; //Master.y
        }
    }

    public Master(){
        in2 = new Inside2();
    }


}

As you can see, i have access to all private fields of master class inside private classes. You can rewrite your classes and put them inside JFrame class and problem solved!

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