简体   繁体   English

摆动将对话框绑定到JButton

[英]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. 我试图将新的客户菜单对话框绑定到应用程序中的newCustomer按钮。 Any ideas? 有任何想法吗?

Well to bind actions in java you add ActionListener s. 为了在Java中绑定动作,您可以添加ActionListener

When constructing your button you need to add an ActionListener to it. 构造按钮时,您需要向其添加一个ActionListener。 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. 据我所知,有几个add()方法是从Component继承的,但是没有一个方法会将ActionListener添加到JButton。 Do you mean addActionListener() instead? 您是说addActionListener()吗?

JButton newCustomer = new JButton();

newCustomer.addActionListener(new ActionListener(){

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

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM