Salve a tutti,
sto implementando un'interfaccia con le swing e avrei bisogno di aiuto.
Ho realizzato un JFrame al cui interno setto una classe Menu che estende JMenuBar.
Questo menù ha due voci e vorrei che a seconda di quella che clicco mi aprisse il giusto pannello nel frame.
Come faccio a far catturare l'evento della classe menu al frame?
codice:public class MenuBar extends JMenuBar implements IMenuBar,ActionListener { private JMenuItem exitItem; private JMenuItem generaTracciato; public JComponent getComponent() { JMenuBar menuBar = new JMenuBar(); JMenu m = new JMenu("File"); generaTracciato = new JMenuItem("Genera Tracciato"); generaTracciato.addActionListener(this); m.add(generaTracciato); exitItem = new JMenuItem("Exit"); exitItem.addActionListener(this); m.add(exitItem); menuBar.add(m); return menuBar; } @Override public void actionPerformed(ActionEvent evt) { Object source = evt.getSource(); if (source == exitItem){ System.exit(0); } else if (source == generaTracciato) { System.out.println("ciao"); } } }
codice:public class FactoryUI extends JFrame implements IFactoryUI{ /** * */ private static final long serialVersionUID = 1L; public static Console console; final static int widthPercent = 53; final static int heightPercent = 65; public FactoryUI() { } public static JFrame create(JFrame frame){ frame.setName("framePrincipale"); frame.setTitle("pdp reader"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); impostaDimensioni(frame); JMenuBar menu = (JMenuBar) buildMenuBar().getComponent(); frame.setJMenuBar(menu); frame.setResizable(false); frame.getContentPane().setLayout(new BorderLayout()); // frame.getContentPane().add(buildFileChooserPanel().getComponent(), BorderLayout.CENTER); // frame.getContentPane().add(buildConsole().getComponent(), BorderLayout.SOUTH); try { UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); frame.setIconImage(new ImageIcon(frame.getClass() .getResource("/logo_android.png")).getImage()); } catch (ClassNotFoundException e) { JOptionPane.showMessageDialog(null, e.getMessage() , "", JOptionPane.ERROR_MESSAGE); e.printStackTrace(); } catch (InstantiationException e) { JOptionPane.showMessageDialog(null, e.getMessage() , "", JOptionPane.ERROR_MESSAGE); e.printStackTrace(); } catch (IllegalAccessException e) { JOptionPane.showMessageDialog(null, e.getMessage() , "", JOptionPane.ERROR_MESSAGE); e.printStackTrace(); } catch (UnsupportedLookAndFeelException e) { JOptionPane.showMessageDialog(null, e.getMessage() , "", JOptionPane.ERROR_MESSAGE); e.printStackTrace(); } frame.setVisible(true); return frame; }

Rispondi quotando