import java.awt.Color;
import java.awt.event.MouseAdapter;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class MouseEvent extends JFrame{
JFrame wanttoview=new JFrame("你看不见我");
private static final long serialVersionUID = 1L;
public static void main(String[] args) {
new MouseEvent();
}
public MouseEvent(){
super("鼠标事件");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(300, 300);
this.getContentPane().setBackground(Color.blue);
this.setLocation(200, 200);
this.addMouseListener(new MouseListen());
this.setVisible(true);
JLabel wanttoadd=new JLabel("要添加的...");
wanttoview.setSize(200, 200);
wanttoview.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
wanttoview.getContentPane().setBackground(Color.black);
wanttoview.setLocation(200, 200);
wanttoview.add(wanttoadd);
}
class MouseListen extends MouseAdapter{
@Override
public void mousePressed(java.awt.event.MouseEvent e) {
System.out.println("mousePressed");
wanttoview.setVisible(true);
}
@Override
public void mouseReleased(java.awt.event.MouseEvent e) {
System.out.println("mouseReleased");
wanttoview.setVisible(false);
}
}
}
我修改其中的:
wanttoview.setVisible(true);
// wanttoview.setUndecorated(true);
wanttoview.setTitle("Point Info:");
wanttoview.setSize(230, 200);
wanttoview.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
wanttoview.getContentPane().add(new JTextField("X position is:"+x+" "));
wanttoview.getContentPane().add(new JTextField("Y position is:"+y+" "));
wanttoview.getContentPane().setBackground(Color.white);
wanttoview.getContentPane().getMousePosition();
wanttoview.setLocation(x, y);
发现,只会显示Y的坐标,不会显示X坐标,请为是代码中哪有错误了?
再问,如果我想一行一行逐行显示一些信息,比如:
X= ?
Y=?
z=...
...
我应该用什么代码?