0

I'm trying to launch the jframe but this Frame doesn't open at all. It crashes when i run the program. I'm using a mouselistner and mouse motion listener interfaces.Please suggest any fixes.............................

 public class TwoListener implements MouseMotionListener, MouseListener{

    private JFrame f;
    private TextField tf;

    public TwoListener(){
        f = new JFrame("Two Listeners Example");
        tf = new TextField(30);


    }

    public void launchFrame(){
        Label label= new Label("Click and Drag the mouse");

        // add components to the frame
        f.add(label,BorderLayout.NORTH);
        f.add(tf,BorderLayout.SOUTH);

        //add this object to a listener

        f.addMouseMotionListener(this);
        f.addMouseListener(this);

        //set size of the frame

        f.setSize(300,200);
        f.setVisible(true);
    }


    @Override
    public void mouseEntered(MouseEvent e) {
        String s = "Mouse moved";
        tf.setText(s);          
    }



    @Override
    public void mouseDragged(MouseEvent e) {

            String s = "Mouse Dragging: X " +e.getX() + " Y: " +e.getY();
            tf.setText(s);
    }



    @Override
    public void mouseExited(MouseEvent e) {
        String s = "mouse has exited the building";
        tf.setText(s);
    }



    //unimplemented methods
    public void mouseClicked(MouseEvent e) {}
    public void mousePressed(MouseEvent e) {}
    public void mouseReleased(MouseEvent e) {}
    public void mouseMoved(MouseEvent e) {}


    public static void main(String[] args){
        new TwoListener();

    }

}

1
  • Crashes how? Does it generate any output? How are you calling it? Jan 21, 2016 at 22:12

2 Answers 2

2

It does not crash. You simply forgot to call launchFrame (thanx to MadProgrammer)

public class TwoListener implements MouseMotionListener, MouseListener {

    private final JFrame    f;
    private final TextField tf;

    public TwoListener() {
        f = new JFrame("Two Listeners Example");
        tf = new TextField(30);

        launchFrame();
    }

    public void launchFrame() {
        final Label label = new Label("Click and Drag the mouse");

        // add components to the frame
        f.add(label, BorderLayout.NORTH);
        f.add(tf, BorderLayout.SOUTH);

        //add this object to a listener

        f.addMouseMotionListener(this);
        f.addMouseListener(this);

        //set size of the frame

        f.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); // SOMEWHAT IMPORTANT
        f.setSize(300, 200);
        f.setVisible(true);
    }


    @Override public void mouseEntered(final MouseEvent e) {
        final String s = "Mouse moved";
        tf.setText(s);
    }

    @Override public void mouseDragged(final MouseEvent e) {

        final String s = "Mouse Dragging: X " + e.getX() + " Y: " + e.getY();
        tf.setText(s);
    }

    @Override public void mouseExited(final MouseEvent e) {
        final String s = "mouse has exited the building";
        tf.setText(s);
    }

    //unimplemented methods
    @Override public void mouseClicked(final MouseEvent e) {}
    @Override public void mousePressed(final MouseEvent e) {}
    @Override public void mouseReleased(final MouseEvent e) {}
    @Override public void mouseMoved(final MouseEvent e) {}

    public static void main(final String[] args) {
        new TwoListener();

    }

}
6
  • 1
    Which the OP is doing in launchFrame? Jan 21, 2016 at 22:12
  • Ah yeah. But he's never calling that^^
    – JayC667
    Jan 21, 2016 at 22:14
  • 1
    Ah, yes, but how do you know? They've not provided any context into how they are using the class Jan 21, 2016 at 22:15
  • Isn't the main method enough of an indication? ;-) With such a basic problem I don't assume they're far beyond any basics.
    – JayC667
    Jan 21, 2016 at 22:17
  • 1
    Nothing wrong with the approach, this is just a personal nitpick/grind, personally, I'd refrain from creating (or at least showing) windows from the constructor, favouring to allow the developer to control this ... but in this case, obviously, things didn't quite working so well for the OP :P Jan 21, 2016 at 22:28
1

You're not calling the launchFrame method from your main method, I'd recommend changing your main method to look something more like...

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                ex.printStackTrace();
            }

            new TwoListener().launchFrame();
        }
    });
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.