简体   繁体   中英

Java | ActionListeners in two JButtons, no response

I've been stuck at this for a couple of days now (first time working with multiple ActionListener , so bear with me).

I have two buttons, with each an actionlistener for moving a drawing to either the left or the right.

Yet either the actionlisteners do not seem to work properly, or the actionperformed does not work.

Suggestions are much appreciated, I've tried switching them to Action as was suggested elsewhere on this forum, but that didn't work out either.

package h03verplaatsbarebal;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Paneel extends JPanel implements ActionListener{

//declare objects
private JButton knopLinks; // moves ball to left
private JButton knopRechts; // moves ball to right

//constants
private int horizontalePlaats; // variabele voor horizontale plaats
private int VERPLAATSING; // constante voor verplaatsing

/*create panel with 2 buttons (to left, to right) and a ball*/
public Paneel() {
    //create objects
    knopLinks = new JButton ("Naar links");
    knopLinks.addActionListener(this);
    knopRechts = new JButton ("Naar rechts");
    knopRechts.addActionListener(this);

    //Tooltips
    knopLinks.setToolTipText("Klik hier om de bal naar links te bewegen");
    knopRechts.setToolTipText("Klik hier om de bal naar rechts te bewegen");

    //add to window
    add(knopLinks);
    add(knopRechts);
}

public void paintComponent(Graphics g){
    super.paintComponent(g);
    int midden = getWidth() / 2; // halfway screen
    int balDiameter = 50;
    int ovaalDiameter = 25;
    horizontalePlaats = midden;

    //draw line
    g.setColor(Color.GREEN);
    g.drawLine(30, getHeight() - 30, getWidth() -30, getHeight() - 30); //lijn
    //draw ball
    g.setColor(Color.ORANGE);
    g.fillOval(horizontalePlaats - balDiameter, getHeight() - 130, 100, 100); // oranje bal
    g.setColor(Color.BLACK);
    g.drawOval(horizontalePlaats - balDiameter, getHeight() - 130, 100, 100); //lijn van bal
    g.setColor(Color.BLACK);
    g.drawOval(horizontalePlaats - ovaalDiameter, getHeight() - 130, 50, 100); // binnen lijnen
}

/*clicking buttons*/
public void actionPerformed(ActionEvent e) {
    VERPLAATSING = 15;
      if (e.getSource() == knopLinks){ //move to left
          horizontalePlaats = horizontalePlaats - VERPLAATSING;
      } 
      else { //move to right
          horizontalePlaats = horizontalePlaats + VERPLAATSING;
      }

    repaint(); // paint again
}
}

Your action listener should work, but what it does is only modifying the value of horizontalePlaats .

The problem is that horizontalePlaats gets overwritten by the value of midden in paintComponent , so you never see the result of the performed actions.

horizontalePlaats = midden;

You are overriding horizontalePlaats in your paint.

int midden = getWidth() / 2; // halfway screen
int balDiameter = 50;
int ovaalDiameter = 25;
horizontalePlaats = midden;

I think your action listener is fine, but you need to only initialize horizontalePlaats to the middle.

You can move this

int midden = getWidth() / 2; // halfway screen
horizontalePlaats = midden;

to your Paneel constructor.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM