这是个练习程序,简而言之就是要弹出一个窗口,在上面点击鼠标就会出现那个点的坐标。但是这个程序一开头哪怕不点击也会在左上角出现一个(0,0),十分不好看,应该怎么改?
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);
}
}
添加回答
举报
0/150
提交
取消