package Applications.EventChannelFactory; import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.Vector; public class PrincipalFrame extends JFrame { private JMenuBar menuBar1 = new JMenuBar(); private JMenu menu; private JMenuItem menuItem; private JToolBar toolBar = new JToolBar(); private JButton jButton = new JButton(); private ImageIcon image1; private JTabbedPane jTabbedPane = new JTabbedPane(); private void DestruirCanal () { Channel c = (Channel) jTabbedPane.getSelectedComponent(); if ( c!= null ) { c.Destruir (); jTabbedPane.remove(c); } else JOptionPane.showMessageDialog(this, "Necesita seleccionar el canal a destruir.", "Error", JOptionPane.ERROR_MESSAGE); } private void CrearCanal () { final Channel canal = new Channel (); final JDialog dial = new JDialog ( this, "Datos del nuevo canal", false); dial.getContentPane().add (canal); dial.pack (); dial.setVisible (true); dial.setLocationRelativeTo ( this ); dial.setDefaultCloseOperation(dial.DISPOSE_ON_CLOSE); canal.Creado.addActionListener(new ActionListener () {public void actionPerformed ( ActionEvent e ) { jTabbedPane.addTab (canal.Nombre.getText(), null, canal, null); dial.dispose(); }}); canal.Cancelar.addActionListener(new ActionListener () {public void actionPerformed ( ActionEvent e ) { dial.dispose(); }}); } private void CrearMenu () { menu = new JMenu(); menu.setText ("Acciones"); menuItem = new JMenuItem(); menuItem.setText("Crear Canal"); menuItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { CrearCanal (); } }); menu.add(menuItem); menuItem = new JMenuItem(); menuItem.setText("Destruir Canal"); menuItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { DestruirCanal (); } }); menu.add(menuItem); menu.addSeparator(); menuItem = new JMenuItem(); menuItem.setText("Salir"); menuItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { fileExit_actionPerformed(e); } }); menu.add(menuItem); menuBar1.add(menu); this.setJMenuBar(menuBar1); } //Construir el marco public PrincipalFrame() { enableEvents(AWTEvent.WINDOW_EVENT_MASK); try { CrearMenu (); this.getContentPane().setLayout(new BorderLayout()); this.setSize(new Dimension(600, 500)); this.setTitle("Event Channel Factory"); this.getContentPane().add(jTabbedPane, BorderLayout.CENTER); CrearCanal(); } catch(Exception e) { System.err.println( "ERROR: " + e ); } } //Acción de Archivo | Salir realizada private void fileExit_actionPerformed(ActionEvent e) { // Destruimos todos los canales for ( int i = 0; i < jTabbedPane.getComponentCount(); i ++ ) { Channel c = (Channel) jTabbedPane.getComponentAt(i); c.Destruir(); } System.exit(0); } //Modificado para poder salir cuando se cierre el sistema protected void processWindowEvent(WindowEvent e) { super.processWindowEvent(e); if(e.getID() == WindowEvent.WINDOW_CLOSING) { fileExit_actionPerformed(null); } } }