how to get the name of a JMenu when a JMenuItem is clicked
问题 How would one get the name of the JMenu holding a clicked JMenuItem? I tried doing this: public void actionPerformed(ActionEvent arg0) { JMenu menuthing = (JMenu)(arg0.getSource()); String menuString = menuthing.getText(); JMenuItem source = (JMenuItem)(arg0.getSource()); String colorType = source.getText(); But it gives me this error: Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: javax.swing.JMenuItem cannot be cast to javax.swing.JMenu So is there a way to cast to