Variable name of Swing component in Netbeans
问题 I have an application that is currently using over 200 buttons, each of which returns the String of their variable name. Is there any way to do this? Setting the name property for each of these would be far too time consuming. 回答1: Use a collection of buttons: ActionListener theActionListener = new ActionListener() { @Override public void actionPerformed(ActionEvent e) { System.out.println(((JButton) e.getSource()).getName()); } }; List<JButton> buttons = new ArrayList<JButton>(); for (int i