package cn;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class MyLayout extends JFrame{
public MyLayout() {
final JLabel label=new JLabel("此处显示鼠标右键点击的坐标");
label.setOpaque(true);
label.setBackground(Color.PINK);
this.add(label, BorderLayout.NORTH);
this.setSize(300,200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getButton() == e.BUTTON1) {
int x = e.getX();
int y = e.getY();
String banner = "鼠标当前点击位置的坐标是" + x +", " + y;
label.setText(banner);
}
}
});
this.setVisible(true);
}
public static void main(String[] args) {
new MyLayout();
}
}
java 鼠标事件
最新推荐文章于 2021-02-15 15:03:30 发布