Home > Enterprise >  Else if only displays the last panel
Else if only displays the last panel

Time:04-07

I have set up a huge game project. I'm using the CardLayout to show when the mouse is released on the JLabel. I have it working on other classes' JLabels, but when I press a JLabel it only shows the last JPanel of the CardLayout. I want it to show the specific JPanel that I have it set to.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Timer;
import java.util.TimerTask;

class ChooseGame extends JPanel {
  Icon imgIcon = new ImageIcon(this.getClass().getResource("gameOne.gif"));
  Icon imgIcon2 = new ImageIcon(this.getClass().getResource("gameTwo.gif"));
  Icon imgIcon3 = new ImageIcon(this.getClass().getResource("gameThree.gif"));
  Icon imgIcon4 = new ImageIcon(this.getClass().getResource("gameFour.gif"));
  JLabel game1 = new JLabel(imgIcon);
  JLabel game2 = new JLabel(imgIcon2);
  JLabel game3 = new JLabel(imgIcon3);
  JLabel game4 = new JLabel(imgIcon4);
  JLabel game5 = new JLabel(imgIcon);
  JLabel game6 = new JLabel(imgIcon2);
  JLabel game7 = new JLabel(imgIcon);
  JLabel game8 = new JLabel(imgIcon2);
  JLabel game9 = new JLabel(imgIcon);

  Handler handler = new Handler();

  public ChooseGame() {
    setVisible(true);
    setBackground(Color.RED);
    setForeground(Color.BLACK);
    setLayout(new GridLayout(3, 3));
    addMouseListener(handler);
    addMouseMotionListener(handler);
    buttons();
    add(game1);
    add(game2);
    add(game3);
    add(game4);
    add(game5);
    add(game6);
    add(game7);
    add(game8);
    add(game9);

  }

  public void buttons() {
    game1.addMouseListener(handler);
    game1.addMouseMotionListener(handler);
    game1.setVisible(true);
    game2.addMouseListener(handler);
    game2.addMouseMotionListener(handler);
    game2.setVisible(true);
    game3.addMouseListener(handler);
    game3.addMouseMotionListener(handler);
    game3.setVisible(true);
    game4.addMouseListener(handler);
    game4.addMouseMotionListener(handler);
    game4.setVisible(true);
    game5.addMouseListener(handler);
    game5.addMouseMotionListener(handler);
    game5.setVisible(true);
    game6.addMouseListener(handler);
    game6.addMouseMotionListener(handler);
    game6.setVisible(true);
    game7.addMouseListener(handler);
    game7.addMouseMotionListener(handler);
    game7.setVisible(true);
    game8.addMouseListener(handler);
    game8.addMouseMotionListener(handler);
    game8.setVisible(true);
    game9.addMouseListener(handler);
    game9.addMouseMotionListener(handler);
    game9.setVisible(true);
  }

  private class Handler implements MouseListener, MouseMotionListener {
    @Override
    public void mouseClicked(MouseEvent e) {

    }

    @Override
    public void mouseReleased(MouseEvent e) {
      time(e);
    }

    public void time(MouseEvent e) {
      Timer timer = new Timer();
      // adds new Timer
      TimerTask task = new TimerTask() {
        // sets new task for said timer
        @Override
        public void run() {
          if (game1.contains(e.getX(), e.getY())) {
            CardPanel.layout.show(CardPanel.cardPanel, "one");
          } else if (game2.contains(e.getX(), e.getY())) {
            CardPanel.layout.show(CardPanel.cardPanel, "two");
          } else if (game3.contains(e.getX(), e.getY())) {
            CardPanel.layout.show(CardPanel.cardPanel, "three");
          } else if (game4.contains(e.getX(), e.getY())) {
            CardPanel.layout.show(CardPanel.cardPanel, "four");
          } else if (game5.contains(e.getX(), e.getY())) {
            CardPanel.layout.show(CardPanel.cardPanel, "five");
          } else if (game6.contains(e.getX(), e.getY())) {
            CardPanel.layout.show(CardPanel.cardPanel, "six");
          } else if (game7.contains(e.getX(), e.getY())) {
            CardPanel.layout.show(CardPanel.cardPanel, "seven");
          } else if (game8.contains(e.getX(), e.getY())) {
            CardPanel.layout.show(CardPanel.cardPanel, "eight");
          } else if (game9.contains(e.getX(), e.getY())) {
            CardPanel.layout.show(CardPanel.cardPanel, "nine");
          }
        }

      };

      timer.scheduleAtFixedRate(task, 0, 1000);

    }

    @Override
    public void mousePressed(MouseEvent e) {
    }

    @Override
    public void mouseExited(MouseEvent e) {

    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseDragged(MouseEvent e) {
      System.out.println("Dragged");
    }

    @Override
    public void mouseMoved(MouseEvent e) {

    }

  }

}

CodePudding user response:

There's a lot going on which is, well, questionable at best, for example...

public ChooseGame() {
    //...
    addMouseListener(handler);
    addMouseMotionListener(handler);
    //...
}

public void buttons() {
    game1.addMouseListener(handler);
    game1.addMouseMotionListener(handler);
    //...
}

Now, both the ChooseGame and each individual label has a the MouseListener attached to them, why? This could cause a double trigger of events

Then...

public void time(MouseEvent e) {
  Timer timer = new Timer();
  // adds new Timer
  TimerTask task = new TimerTask() {
    // sets new task for said timer
    @Override
    public void run() {
      if (game1.contains(e.getX(), e.getY())) {
        CardPanel.layout.show(CardPanel.cardPanel, "one");
      } else if (game2.contains(e.getX(), e.getY())) {
        CardPanel.layout.show(CardPanel.cardPanel, "two");
      } else if (game3.contains(e.getX(), e.getY())) {
        CardPanel.layout.show(CardPanel.cardPanel, "three");
      } else if (game4.contains(e.getX(), e.getY())) {
        CardPanel.layout.show(CardPanel.cardPanel, "four");
      } else if (game5.contains(e.getX(), e.getY())) {
        CardPanel.layout.show(CardPanel.cardPanel, "five");
      } else if (game6.contains(e.getX(), e.getY())) {
        CardPanel.layout.show(CardPanel.cardPanel, "six");
      } else if (game7.contains(e.getX(), e.getY())) {
        CardPanel.layout.show(CardPanel.cardPanel, "seven");
      } else if (game8.contains(e.getX(), e.getY())) {
        CardPanel.layout.show(CardPanel.cardPanel, "eight");
      } else if (game9.contains(e.getX(), e.getY())) {
        CardPanel.layout.show(CardPanel.cardPanel, "nine");
      }
    }
  };
  timer.scheduleAtFixedRate(task, 0, 1000);
}

you're comparing the point of the event against the each labels location, which in my testing, had each label return true for the contains check

  • Related