0

I have two classes one of them is where my JFrame is built and the second one is my Main_Loop and MouseListener.

How could I call the mouse listener to execute?

FIRST CLASS:

public class Main_Frame extends Main_Loop{

public static void main(String[]args){
    //SETTING UP THE JFRAME
    JFrame frame = new JFrame();
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(700,500);

    JPanel panel = new JPanel();
    panel.setLayout(null);

    frame.add(panel);

    Main_Loop cl = new Main_Loop();

    while (true){
    System.out.println(cl.clicked);
    }

SECOND CLASS:

public class Main_Loop implements MouseListener {

public int mouseY,x;
public int mouseX;
public boolean clicked;

public void mouseClicked(MouseEvent arg0) {
    mouseY = arg0.getX();
    mouseX = arg0.getY();
    clicked = true;
}

public void mouseEntered(MouseEvent arg0) {
}

public void mouseExited(MouseEvent arg0) {
}

public void mousePressed(MouseEvent arg0) {
}

public void mouseReleased(MouseEvent arg0) {
}
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    1) `panel.addMouseListener(cl)` 2) `while (true){` No, that will block the EDT. Use a Swing `Timer` to do animation. 3) `panel.setLayout(null);` Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Oct 01 '15 at 12:28
  • .. 4) A single blank line of white space in source code is all that is *ever* needed. Blank lines after `{` or before `}` are also typically redundant. – Andrew Thompson Oct 01 '15 at 12:29

0 Answers0