Ciao a tutti,
mi chiedevo come fa il compilatore a segnalare un errore se faccio per esempio un programma con i pulsanti e non implemento l'interfaccia ActionListener, pur implementando il suo metodo. come se ne accorge?![]()
Ciao a tutti,
mi chiedevo come fa il compilatore a segnalare un errore se faccio per esempio un programma con i pulsanti e non implemento l'interfaccia ActionListener, pur implementando il suo metodo. come se ne accorge?![]()
avrai sbagliato a dichiarare i pulsanti.
<´¯)(¯`¤._)(¯`»ANDREA«´¯)(_.¤´¯)(¯`>
"The answer to your question is: welcome to tomorrow"
io dico questo..se scrivo:
private class Color implements ActionListener
funziona...se scrivo:
private class Color
non funziona...ma questo in tutti i programmi che usano i pulsanti. Non è un problema del mio programma...solo mi chiedevo come sia possibile che il compilatore capisca che una classe deve implementare una certa interfaccia...atrimenti segnalare errore.
posta il codice... ti dimenticherai un import da qualche parte.
<´¯)(¯`¤._)(¯`»ANDREA«´¯)(_.¤´¯)(¯`>
"The answer to your question is: welcome to tomorrow"
codice:import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ButtonTest { public static void main(String[] args) { ButtonFrame frame = new ButtonFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } } class ButtonFrame extends JFrame { public ButtonFrame() { setTitle("ButtonTest"); setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); ButtonPanel panel = new ButtonPanel(); add(panel); } public static final int DEFAULT_WIDTH = 300; public static final int DEFAULT_HEIGHT = 200; } class ButtonPanel extends JPanel { public ButtonPanel() { JButton yellowButton = new JButton("Yellow"); JButton blueButton = new JButton("Blue"); JButton redButton = new JButton("Red"); add(yellowButton); add(blueButton); add(redButton); ColorAction yellowAction = new ColorAction(Color.YELLOW); ColorAction blueAction = new ColorAction(Color.BLUE); ColorAction redAction = new ColorAction(Color.RED); yellowButton.addActionListener(yellowAction); blueButton.addActionListener(blueAction); redButton.addActionListener(redAction); } private class ColorAction implements ActionListener { public ColorAction(Color c) { backgroundColor = c; } public void actionPerformed(ActionEvent event) { setBackground(backgroundColor); } private Color backgroundColor; } }
a parte il fatto che manca un getContentPane() la cosa va.
<´¯)(¯`¤._)(¯`»ANDREA«´¯)(_.¤´¯)(¯`>
"The answer to your question is: welcome to tomorrow"
non hai risposto alla mia domanda, cmq per curiosità come lo avresti modificato usando getContentPane()?
il tuo programma da un warning in compilazione:
al posto di add(panel) ci devi mettere getContentPane().add(panel)
e poi personalmente avrei fatto il mio bottone personale
codice:import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ButtonTest { public static void main(String[] args) { ButtonFrame frame = new ButtonFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } } class ButtonFrame extends JFrame { public ButtonFrame() { setTitle("ButtonTest"); setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); ButtonPanel panel = new ButtonPanel(); getContentPane().add(panel); } public static final int DEFAULT_WIDTH = 300; public static final int DEFAULT_HEIGHT = 200; } class ButtonPanel extends JPanel { private class colorButton extends JButton implements ActionListener { private Color backGroundColor; public void actionPerformed(ActionEvent ae) { this.getParent().setBackground(backGroundColor); } colorButton(String label, Color c) { super(label); backGroundColor = c; this.addActionListener(this); } } public ButtonPanel() { colorButton yellowButton = new colorButton("Yellow", Color.YELLOW); colorButton blueButton = new colorButton("Blue", Color.BLUE); colorButton redButton = new colorButton("Red", Color.RED); add(yellowButton); add(blueButton); add(redButton); } }
<´¯)(¯`¤._)(¯`»ANDREA«´¯)(_.¤´¯)(¯`>
"The answer to your question is: welcome to tomorrow"
no, quello ti capita perche hai una versione di java <= 1.4
se te usi un'istruzione del tipo:Originariamente inviato da Lucked
Ciao a tutti,
mi chiedevo come fa il compilatore a segnalare un errore se faccio per esempio un programma con i pulsanti e non implemento l'interfaccia ActionListener, pur implementando il suo metodo. come se ne accorge?![]()
button.addActionListener(this);
il compilatore sa che il metodo addActionListener vuole come argomento un ActionListener e se la classe dell'argomento (cioè this, ovvero la classe in cui c'è l'istruzione) non implementa tale interfaccia te lo può segnalare.
Sun Certified Java Programmer
EUCIP Core Level Certified
European Certification of Informatics Professionals