What is java.awt.Component.getName() and setName() used for?

后端 未结 9 1567
半阙折子戏
半阙折子戏 2020-11-28 13:50

What is java.awt.Component.getName() used for? It always seems to be null in the applications I build with NetBeans. I\'m thinking of storing some help text p

9条回答
  •  情深已故
    2020-11-28 14:03

    I have searched many answers for getting name and i think this is the only easy solution

    public static void main(String[] args) {
        ActionListener actionListener = new ActionListener() {
            public void actionPerformed(ActionEvent actionEvent) {
                String name = actionEvent.getSource().toString();
                UserReaction(ObjectName.getComponentVariableName(name), "null");
            }
        };
        Button calculate_btn = new Button("Calculate");
        calculate_btn.setName("Calculate");
        calculate_btn.addActionListener(actionListener);
    }
    
    private static void UserReaction(String objectName) {
        if (objectName.equals("Calculate")) {
            //do something;         
        }
    }static public String getComponentVariableName(String name) {
        String res = (name.substring(name.indexOf("[") + 1));
        res = res.split(",")[0];
        return res;
    }
    

提交回复
热议问题