Professional Documents
Culture Documents
import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; @SuppressWarnings("serial") public class InteractiveFrame extends JFrame { private JPanel pane; private JButton button; public InteractiveFrame(String title){ super(title); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); pane = new JPanel(); button = new JButton("Colora"); //*****Registrazione del rilevatore di evento***** button.addActionListener(new ButtonListener()); //************************************************ pane.add(button); add(pane); pack(); setVisible(true); } public static void main(String[] args) { InteractiveFrame frame = new InteractiveFrame("Eventi di azione con classi interne"); } class ButtonListener implements ActionListener { public static final int MAX_VALUE = 256; public void actionPerformed(ActionEvent e){ Random randomGenerator = new Random(); int red = randomGenerator.nextInt(MAX_VALUE); int green = randomGenerator.nextInt(MAX_VALUE); int blue = randomGenerator.nextInt(MAX_VALUE); pane.setBackground(new Color(red, green, blue)); } } } Bocchi Cinzia Ultimo aggiornamento: 06/11/2011
@SuppressWarnings("serial") public class InteractiveFrame extends JFrame { private JPanel pane; private JButton button; public InteractiveFrame(String title){ super(title); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); pane = new JPanel(); button = new JButton("Colora"); //*****Classe interna locale********************* class ButtonListener implements ActionListener { public static final int MAX_VALUE = 256; public void actionPerformed(ActionEvent e){ Random randomGenerator = new Random(); int red = randomGenerator.nextInt(MAX_VALUE); int green = randomGenerator.nextInt(MAX_VALUE); int blue = randomGenerator.nextInt(MAX_VALUE); pane.setBackground(new Color(red, green, blue)); } } //************************************************ //*****Registrazione del rilevatore di evento***** button.addActionListener(new ButtonListener()); //************************************************ pane.add(button); add(pane); pack(); setVisible(true); } public static void main(String[] args) { InteractiveFrame frame = new InteractiveFrame("Eventi di azione con classi interne"); } }
button.addActionListener(new ButtonListener()); e definire in seguito la classe ButtonListener, si utilizza la sintassi seguente: button.addActionListener(new ActionListener() { //codice contenuto nella classe ButtonListener });
import import import import import import import java.awt.Color; java.awt.event.ActionEvent; java.awt.event.ActionListener; java.util.Random; javax.swing.JButton; javax.swing.JFrame; javax.swing.JPanel;
@SuppressWarnings("serial") public class InteractiveFrame extends JFrame { private JPanel pane; private JButton button; public InteractiveFrame(String title){ super(title); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); pane = new JPanel(); button = new JButton("Colora"); //*****Classe interna locale anonima********************* button.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ final int MAX_VALUE = 256; Random randomGenerator = new Random(); int red = randomGenerator.nextInt(MAX_VALUE); int green = randomGenerator.nextInt(MAX_VALUE); int blue = randomGenerator.nextInt(MAX_VALUE); pane.setBackground(new Color(red, green, blue)); } }); //******************************************************** pane.add(button); add(pane); pack(); setVisible(true); } public static void main(String[] args) { InteractiveFrame frame = new InteractiveFrame("Eventi di azione con classi interne anonime"); } }
Quest'opera stata rilasciata con licenza Creative Commons Attribution-ShareAlike 3.0 Unported. Per leggere una copia della licenza visita il sito web http://creativecommons.org/licenses/by-sa/3.0/ o spedisci una lettera a Creative Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA. Bocchi Cinzia Ultimo aggiornamento: 06/11/2011