Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a Java code to create the following frame. You can see the frame by runnin

ID: 3577064 • Letter: W

Question

Write a Java code to create the following frame.

You can see the frame by running the JAR File: ClickTheFrame.jar

Make sure whenever a user clicks on the frame the mouse location's coordinates will be placed in parenthesis as a text in the textfield at the top of the frame.

You need to set the size of the frame to the following: width = 300 and Height = 200.

This is the code (below) I have so far, but It doesnt place the coordinates in the text field

import java.awt.Dimension;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JTextField;

public class ClickTester {
   public static void main(String[] args)
   {
       JFrame frame = new JFrame("Click the Frame");
       JTextField text = new JTextField();
      
       frame.add(text, "North");
       frame.addMouseListener(new MouseAdapter()
       {

           public void mouseClicked(MouseEvent a)
           {
               double x = a.getPoint().getX();
               double y = a.getPoint().getY();
               this.val$text.setText("[" + x + "," + y + "]");
              
           }
          
       });
       frame.setDefaultCloseOperation(3);
       frame.setSize(300, 200);
      
       Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
       int x = (int)((d.getWidth() - frame.getWidth()) / 2.0D);
       int y = (int)((d.getHeight() - frame.getHeight()) / 2.0D);
       frame.setLocation(x, y);
       frame.setVisible(true);
      
   }
}

-------------------------------------------------------------------------------------------

import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JTextField;


final class ClickTester$1 extends MouseAdapter
{

public void mouseClicked(MouseEvent a)
{
   double x = a.getPoint().getX();
   double y = a.getPoint().getY();
   this.val$text.setText("[" + x + "," + y + "]");
}
}

Click the frame

Explanation / Answer

import java.awt.Dimension;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JTextField;

public class ClickTester {

    public static void main(String[] args)
    {
        JFrame frame = new JFrame("Click the Frame");
        JTextField text = new JTextField();
        frame.add(text, "North");
        frame.addMouseListener(new MouseAdapter()
        {

            public void mouseClicked(MouseEvent a)
            {
                double x = a.getPoint().getX();
                double y = a.getPoint().getY();
                System.out.println(x+","+y);//these co-ords are relative to the component
                text.setText("["+x+", "+y+"]");
            }

        });
        frame.setDefaultCloseOperation(3);
        frame.setSize(300, 200);

        Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
        int x = (int)((d.getWidth() - frame.getWidth()) / 2.0D);
        int y = (int)((d.getHeight() - frame.getHeight()) / 2.0D);
        frame.setLocation(x, y);
        frame.setVisible(true);

    }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote