简体   繁体   中英

Java actionlistener between 2 class

i have 2 class, in second class (DisplayResult) have createAndShowGUI() method

public static void createAndShowGUI() {

    JFrame frame = new JFrame("Database Data");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    DisplayResult newContentPane = new DisplayResult();
    newContentPane.setOpaque(true); //content panes must be opaque
    frame.setContentPane(newContentPane);
    frame.pack();
    frame.setVisible(false);
}`

and in my main class(Mozijegy) a menu method and in menu method 3 button (a,b,c) So my question is if i want to that i click on C button in mozijegy class then it show me the createAndShowGUI() frame (creatAndShowGUI in other class)

The simplest option is to pass a reference to an instance of whatever class contains createAndShowGUI() to your Mozijegy instance. If that class is called ClassX :

 class Mozijegy { 
      ClassX classX;
      ...
      Mozigegy (ClassX classX) { //along with whatever other params you need 
         this.classX= classX;
         ...
      }
      ...
      buttonC.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent arg0) {
             classX.doWhenButtonCisClicked();
          }
       });
 }

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