Salve. Sono ore ormai che provo ad inserire un pulsante JButton, il problema risiede nel fatto che non mi fa compilare a causa dell'evento che ho associato al pulsante, eppure mi pare di aver scritto tutto bene. Ho provato a mettere l'evento ActionListener anche all'interno di main() ma siccome è di tipo static, non mi fa usare le variabili interne che sono private... cosa posso fare??
codice:import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Lavagna extends JPanel { private int pointCount=0; private Point points[] = new Point[99999]; private JButton cancella = new JButton("Cancella tutto"); cancella.addActionListener ( new ActionListener() { public void actionPerformed(ActionEvent event) { for (int i=0;i<pointCount;i++) { points[i] = null; } } } ); public Lavagna() { addMouseMotionListener ( new MouseMotionAdapter() { public void mouseDragged(MouseEvent event) { if (pointCount < points.length) { points[pointCount] = event.getPoint(); pointCount++; repaint(); } } } ); } public void paintComponent(Graphics g) { super.paintComponent(g); for (int i=0;i<pointCount;i++) { g.fillOval(points[i].x,points[i].y,4,4); } } public static void main(String[] args) { JFrame frame = new JFrame("My Java Window"); Lavagna pannello = new Lavagna(); Pulsante cancella = new Pulsante(); frame.setSize(600,400); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(pannello,BorderLayout.CENTER); frame.add(new JLabel("Disegna con il mouse"),BorderLayout.SOUTH); frame.add(cancella,BorderLayout.NORTH); frame.setVisible(true); } }

Rispondi quotando
