J'ai donc un écouteur de bouton externe dans la portée de la fonction setPaintFlag()
et j'ai besoin de l'appeler, mais java me donne des erreurs d'exécution. De plus, mon panneau de dessin ne s'affiche pas. Quelqu'un peut-il repérer mon erreur? le but du programme est de dessiner soit un cercle ou un carré dans une position aléatoire avec une taille aléatoire et une couleur. Le programme se compile et la fenêtre apparaît, seuls les boutons sont visibles mais ils ne se déclenchent pas. l'erreur visible quand je passe la souris sur l'setPaintFlag()
est The method setPaintFlag(int) is undefined for the type PaintLab.buttonListener
java classe interne appel de la fonction avec l'écouteur de bouton
Un message d'erreur plus bavard après une pression sur le bouton est
Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problems: The method setPaintFlag(int) is undefined for the type PaintLab.ButtonListener The method setPaintFlag(int) is undefined for the type PaintLab.ButtonListener
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class PaintLab extends JPanel {
private JButton circle, square;
private JPanel drawingPanel;
public PaintLab() {
circle = new JButton("Draw Circle");
circle.addActionListener(new ButtonListener());
square = new JButton("Draw Square");
square.addActionListener(new ButtonListener());
JPanel buttonPanel = new JPanel();
JPanel drawingPanel = new DrawingPanel(400, 400);
add(drawingPanel);
BoxLayout bX = new BoxLayout(buttonPanel, BoxLayout.X_AXIS);
buttonPanel.setLayout(bX);
buttonPanel.add(circle);
buttonPanel.add(square);
add(buttonPanel);
}
private class DrawingPanel extends JPanel { // outer
private boolean paintFlag = false;
private int theMode = -1;
private int width, height;
private DrawingPanel(int width, int height) { // inner
this.width = width;
this.height = height;
}
public void setPaintFlag(int current) { // inner
paintFlag = true;
theMode = current;
}
public void paintComponent(Graphics g) { // inner
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
int r = (int) (Math.random() * 255) + 1;
int gn = (int) (Math.random() * 255) + 1;
int b = (int) (Math.random() * 255) + 1;
g2.setColor(new Color(r, gn, b));
int w = (int) (Math.random() * width) + 1;
int h = (int) (Math.random() * height) + 1;
int x = (int) (Math.random() * width) + 1;
int y = (int) (Math.random() * height) + 1;
if (theMode == 0) {
g2.drawOval(x, y, w, h);
g2.fillOval(x, y, w, h);
} else if (theMode == 1) {
g2.drawRect(x, y, w, h);
g2.fillRect(x, y, w, h);
}
g2.dispose();
}
}
private class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
if (event.getSource() == circle) {
setPaintFlag(0);
drawingPanel.repaint();
} else if (event.getSource() == square) {
setPaintFlag(1);
drawingPanel.repaint();
}
}
}
}
Et ceci est la principale.
public class paintLabMain {
public static void main(String[] args) {
JFrame frame=new JFrame("Paint Lab");
frame.setPreferredSize(new Dimension (300,600));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new PaintLab());
frame.pack();
frame.setVisible(true);
}
}
1. S'il vous plaît poster un [MCVE]. 2. Où di appelez-vous 'buttonListener'? 3. Quelles erreurs obtenez-vous? – c0der
désolé j'ai oublié d'ajouter mon principal. Le programme se compile et la fenêtre apparaît, seuls les boutons sont visibles mais ils ne se déclenchent pas. –
Les boutons ne se déclenchent pas car ils n'ont pas d'écouteur d'action – c0der