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();
}
}