简体   繁体   中英

Swing bind a dialog box to a JButton

I am trying to bind a new customer menu dialog box to a newCustomer button in my application. Any ideas?

Well to bind actions in java you add ActionListener s.

When constructing your button you need to add an ActionListener to it. That way, when the click event happens, the button knows what to do.

newCustomerButon.add(new ActionListener(){

    public void actionPerformed(ActionEvent e){
        // This is where you put the code for popping up the form.
        yourForm.setVisible(true); // Or something similar.
    }

});

As far as I know, there are several add() methods which are inherited from Component, but none of which will add an ActionListener to a JButton. Do you mean addActionListener() instead?

JButton newCustomer = new JButton();

newCustomer.addActionListener(new ActionListener(){

    public void actionPerformed(ActionEvent e){
        // TODO bind the new customer menu dialog box 
    }

});

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