简体   繁体   中英

Java using ActionListener in different file in same package /NetBeans/

java file in a same package. Its bus booking system, one is Reservation.java that has the program and the other is JPanel.java GUI. My problem is: I wanna increase counter in Main.java by pressing button in JPanel.java

Main.java

public static void main(String args[]) {
    JFrame frame = new JFrame();

    JPanel panel = new JPanel();
    frame.getContentPane().add(panel);

JPanel.java

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    BookWindow window = new BookWindow(); //not working
    i++;
}    

Thank you <3

How about declaring your counter as a static variable in your 'Main' class and adding a static method to increment your counter:

public final class Main extends Application {
 private static int counter=0;

 static void incrementMainCounter()
  {
   counter++;
  }

 public static void main(String args[]) {
    JFrame frame = new JFrame();

    JPanel panel = new JPanel();
    frame.getContentPane().add(panel);
 }

}

Then just call your increment function whenever you need to:

 private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    BookWindow window = new BookWindow(); //not working
    incrementMainCounter();
}   

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