Pour une raison quelconque, lorsque j'essaie d'appeler une méthode non statique d'une autre classe dans une action, j'obtiens une erreur indiquant que je ne peux pas appeler une méthode non statique dans une méthode statique. Cependant, je n'ai jamais défini l'action ou ses parents comme statiques. Pourquoi ai-je cette erreur?Java pense qu'une méthode est statique?
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class KeyListenerFrame extends JFrame implements KeyListener {
GridLayout gridLayout1 = new GridLayout();
JPanel listenerPan = new JPanel();
public KeyListenerFrame() {
try {
jbInit();
listenerPan.addKeyListener(this);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
listenerPan.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_PAGE_UP,0,true), "showKey");
listenerPan.getActionMap().put("showKey", showKey);
listenerPan.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_D, 0, true), "showKey");
}
catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
KeyListenerFrame klf = new KeyListenerFrame();
klf.setBounds(10,10,500,500);
klf.setVisible(true);
klf.focusPanel();
}
public void focusPanel() {
listenerPan.requestFocus();
}
private void jbInit() throws Exception {
this.getContentPane().add(listenerPan, null);
ColorPanel panel = new ColorPanel(Color.lightGray);
this.getContentPane().add(panel);
}
c'est la partie qui me donne mal à
Action showKey = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
ColorPanel.moveLeft();
}
};
public void keyTyped(KeyEvent e) {
System.out.println(e.toString());
}
public void keyPressed(KeyEvent e) {
System.out.println(e.toString());
}
public void keyReleased(KeyEvent e) {
System.out.println(e.toString());
}
}
ColorPanel.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Component.*;
import java.awt.Container.*;
import javax.swing.JComponent.*;
public class ColorPanel extends JPanel{
public Circle circle;
private javax.swing.Timer timer;
public ColorPanel(Color backColor){
int width = 500;
int height = 500;
setBackground(backColor);
setPreferredSize(new Dimension(width, height));
circle = new Circle(width/2, height/2, 50, Color.red);
circle.setVelocity(5);
addMouseListener(new MoveListener());
}
public void paintComponent(Graphics g){
super.paintComponent(g);
circle.fill(g);
}
public void moveLeft(){
circle.move();
repaint();
}
/* public class MoveListener extends MouseAdapter{
public void mousePressed(MouseEvent e){
circle.move();
repaint();
}
}
private class MoveListener implements ActionListener{
public void actionPerformed(ActionEvent e){
circle.move();
//circle.turn(1);
repaint();
}
} */
}
L'erreur générée à partir du ColorPanel.moveLeft(); est: KeyListenerFrame.java:51: méthode non statique moveLeft() ne peut pas être référencé à partir d'un contexte statique ColorPanel.moveLeft();
pourriez-vous poster l'erreur –
et aussi à quoi ressemble ColorPanel? – asgs