简体   繁体   中英

How to add a listener to a button from a ActionListener inner class?

I'm trying to do a simple calculator program by building my swing interface on netbeans.

I want to have 3 Classes:

  1. GUI Class - which holds the codes for building the interface
  2. Listener Class - holds all the listener in the GUI interface
  3. Boot Class - this will start the application

For simplicity, I will post my code for a single button. My goal here is to change the Buttons visible text from "1" to "11" to test my design. After verifying that my design works I will continue on working on other buttons.

calculatorGUI.class

import javax.swing.JButton;

public class calculatorGUI extends javax.swing.JFrame {

public calculatorGUI() {
    initComponents();
}

private void initComponents() {
oneBtn = new javax.swing.JButton();
oneBtn.setText("1");
}

private javax.swing.JButton oneBtn;

public JButton getOneBtn() {
    return oneBtn;
}

public void setOneBtn(String name) {
    oneBtn.setText(name);
}
}

Listener.class

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Listener {

class oneBtnListener implements ActionListener {
    @Override
   public void actionPerformed(ActionEvent ev) {
       calculatorGUI g = new calculatorGUI();
       g.setOneBtn("11");
   }

} 

}

Boot.class

public class Boot {

public static void main(String[] args) {
     calculatorGUI gui = new calculatorGUI();
     Listener listen = new Listener();
     Listener.oneBtnListener oneListen = listen.new oneBtnListener();
     gui.getOneBtn().addActionListener(oneListen);
     gui.setVisible(true);
}

}

The problem is, nothing happens when I click the button. It seems that the actionListener is not being registered to the button. Can I ask for your help guys on which angle I missed?

The issue I am seeing is how you are initializing calculatorGUI twice, once with the default value and another with the changed value. Take out the initialization of calculatorGUI within your Listener class and pass it from your Boot class and it should work fine.

Although if I were you, I would add the GUI implementations within the GUI class, having it within the listener class that is using within the main function is not something I have seen before and would probably not advise.

Modify your code accordingly,

class Listener {

class oneBtnListener implements ActionListener {
   @Override
  public void actionPerformed(ActionEvent ev) {
      if(ev.getActionCommand() == "1")
      {
          JButton btn = (JButton)ev.getSource();
          btn.setText("11");
      }
  }

}
}

class calculatorGUI extends javax.swing.JFrame {

public calculatorGUI() {
   initComponents();
}

private void initComponents() {
oneBtn = new javax.swing.JButton();
oneBtnListener btnListener = new Listener().new oneBtnListener();
oneBtn.setText("1");
oneBtn.setBounds(100,100,100,25);
oneBtn.addActionListener(btnListener);
add(oneBtn);

setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400,400);
}

private javax.swing.JButton oneBtn;

public JButton getOneBtn() {
   return oneBtn;
}

public void setOneBtn(String name) {
   oneBtn.setText(name);
}
}

You can change now other part according to your requirement, I just gave you "1" -> "11", but you can do more.

Best of Luck.

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