...un main program che implementa un egocentrico AllyEventListener :
public class MainProgram implements AllyEventListener { private MainLayout mainLayout; public static void main(String args[]){ new MainProgram(); } public MainProgram(){ this.mainLayout = new MainLayout(); this.mainLayout.setListeners(this); this.mainLayout.showLayoutSuperScemo(); } public void performedEvent(AllyEvent e) { System.out.println("ho ricevuto un evento ! " +e.getEventName()); if(e.getEventName().equals("vedi")) { this.mainLayout.hideLayoutSuperScemo(); this.mainLayout.showLayoutSuperScemo2(); } if(e.getEventName().equals("nascondi")) { this.mainLayout.hideLayoutSuperScemo2(); this.mainLayout.showLayoutSuperScemo(); } } }
...un mainLayout che visualizza e nasconde pannelli e passa loro l'allyEventListener per l'ascolto degli eventi :public class MainLayout { private LayoutSuperScemo layoutSuperScemo; private LayoutSuperScemo2 layoutSuperScemo2; private JFrame mainFrame; private AllyEventListener listeners; public MainLayout(){ try { mainFrame = new JFrame(); mainFrame.setLayout(null); mainFrame.setDefaultCloseOperation(2); mainFrame.setSize(800, 600); mainFrame.setBounds(0, 0, 800, 600); mainFrame.setUndecorated(true); mainFrame.getContentPane().setBackground(Color.white); mainFrame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } public void showLayoutSuperScemo(){ if(layoutSuperScemo==null) { layoutSuperScemo = new LayoutSuperScemo(); layoutSuperScemo.addAllyEventListener(listeners); mainFrame.add(layoutSuperScemo); } layoutSuperScemo.setVisible(true); mainFrame.repaint(); } public void hideLayoutSuperScemo(){ layoutSuperScemo.setVisible(false); mainFrame.repaint(); } public void showLayoutSuperScemo2(){ if(layoutSuperScemo2==null) { layoutSuperScemo2 = new LayoutSuperScemo2(); layoutSuperScemo2.addAllyEventListener(listeners); mainFrame.add(layoutSuperScemo2); } layoutSuperScemo2.setVisible(true); mainFrame.repaint(); } public void hideLayoutSuperScemo2(){ layoutSuperScemo2.setVisible(false); mainFrame.repaint(); } public AllyEventListener getListeners() { return listeners; } public void setListeners(AllyEventListener listeners) { this.listeners = listeners; } }
...l'allyEventListener :public interface AllyEventListener extends EventListener{ public void performedEvent(AllyEvent e); }
...l'oggetto palleggiato da chi scatena l'evento all'ascoltatore da ampliare a seconda delle esigenze:public class AllyEvent extends EventObject { private static final long serialVersionUID = 1L; String eventName; public AllyEvent(Object source,String eventName) { super(source); this.eventName = eventName; } public String getEventName() { return eventName; } public void setEventName(String eventName) { this.eventName = eventName; } }
...i due layout con il listener e il lancio dell'evento legato al tasto :public class LayoutSuperScemo2 extends JPanel{ private JButton invia; private JLabel etichetta; private JTextField campo; private AllyEventListener listeners; public static void main(String args[]){ JFrame mainFrame = new JFrame(); mainFrame.setLayout(null); mainFrame.setDefaultCloseOperation(2); mainFrame.setSize(300, 200); LayoutSuperScemo2 layout = new LayoutSuperScemo2(); layout.setOpaque(false); layout.setVisible(true); mainFrame.add(layout); mainFrame.setVisible(true); } public LayoutSuperScemo2(){ setLayout(null); setBounds(0, 0, 600, 800); etichetta = new JLabel("etichetta"); etichetta.setBounds(410, 210, 90, 25); add(etichetta); campo = new JTextField("campo"); campo.setBounds(400, 210, 90, 25); add(campo); invia = new JButton("invia"); invia.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { inviaButtonEvent(evt); } }); invia.setBounds(500, 540, 90, 25); add(invia); repaint(); } public void addAllyEventListener(AllyEventListener listener) { this.listeners = listener; } private void launchEvent(String eventName) { if(this.listeners!=null) this.listeners.performedEvent(new AllyEvent(this,eventName)); } private void inviaButtonEvent(java.awt.event.ActionEvent evt) { // TODO add your handling code here: this.launchEvent("nascondi"); } }public class LayoutSuperScemo extends JPanel{ private JButton invia; private JLabel etichetta; private JTextField campo; private AllyEventListener listeners; public static void main(String args[]){ JFrame mainFrame = new JFrame(); mainFrame.setLayout(null); mainFrame.setDefaultCloseOperation(2); mainFrame.setSize(300, 200); LayoutSuperScemo layout = new LayoutSuperScemo(); layout.setOpaque(false); layout.setVisible(true); mainFrame.add(layout); mainFrame.setVisible(true); } public LayoutSuperScemo(){ setLayout(null); setBounds(0, 0, 300, 200); etichetta = new JLabel("etichetta"); etichetta.setBounds(10, 10, 90, 25); add(etichetta); campo = new JTextField("campo"); campo.setBounds(200, 10, 90, 25); add(campo); invia = new JButton("invia"); invia.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { inviaButtonEvent(evt); } }); invia.setBounds(200, 140, 90, 25); add(invia); repaint(); } public void addAllyEventListener(AllyEventListener listener) { this.listeners = listener; } private void launchEvent(String eventName) { if(this.listeners!=null) this.listeners.performedEvent(new AllyEvent(this,eventName)); } private void inviaButtonEvent(java.awt.event.ActionEvent evt) { // TODO add your handling code here: this.launchEvent("vedi"); } }
......
...gran bel pezzo di codice...