this is an exercise program, which in short is to pop up a window on which the coordinates of that point will appear when you click the mouse.
but this program will appear in the upper left corner even if it is not clicked at the beginning. It is very ugly. How should I change it?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class q08a extends JFrame
{
private int x=0,y=0;
public q08a()
{
mianban p1=new mianban();
add(p1);
p1.addMouseListener(new MouseAdapter()
{
@Override
public void mouseClicked(MouseEvent e)
{
super.mouseClicked(e);
x=e.getX();
y=e.getY();
p1.repaint();
}
}
);
}
class mianban extends JPanel
{
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
String str="("+x+","+y+")";
g.drawString(str, x, y);
}
}
public static void main(String[] args)
{
JFrame frame=new q08a();
frame.setTitle("q08a");
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setSize(300,300);
frame.setVisible(true);
}
}