编写一个JFrame窗口,要求如下:
1)在窗口的最上方放置一个JLabel标签,标签中默认的文本是“鼠标右键点击的坐标”
2)为JFrame窗口添加一个鼠标事件,当鼠标右键点击窗口时,鼠标的坐标在JLabel标签中显示
注意:是鼠标右击(BUTTON3)``
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class text13 extends JFrame {
public text13() {
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.BUTTON3) {
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 text13();
}
}