编写一个Java程序,在程序中建立一个窗口,在文本框中显示鼠标的信息。要求运行结果如图1所示。

如题所述

private static TextField tf;
private static Frame f;
public static void main(String[] args) {

f = new Frame("鼠标事件窗口");
// f.setSize(500, 400);
// f.setLocation(400, 200);
f.setBounds(400, 200, 400,200);
f.setLayout(null);
//f.setLayout(new FlowLayout());//设置流式布局
tf=new TextField();
tf.setBounds(100, 120, 190, 20);
f.add(tf);
f.addMouseMotionListener(new MouseAdapter() {
public void mouseMoved(MouseEvent e){
int x=e.getX();
int y=e.getY();
tf.setText("鼠标移动,坐标为"+x+","+y);
}
});
            f.setVisible(true);

温馨提示:内容为网友见解,仅供参考
无其他回答
相似回答