- package bbs.istak.org.tw;
- import java.awt.BorderLayout;
- import java.awt.EventQueue;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import javax.swing.border.EmptyBorder;
- import javax.swing.JLabel;
- import java.awt.event.MouseMotionAdapter;
- import java.awt.event.MouseEvent;
- public class Main extends JFrame {
- private JPanel contentPane;
- /**
- * Launch the application.
- */
- public static void main(String[] args) {
- EventQueue.invokeLater(new Runnable() {
- public void run() {
- try {
- Main frame = new Main();
- frame.setVisible(true);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- }
- /**
- * Create the frame.
- */
- public Main() {
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setBounds(100, 100, 450, 300);
- contentPane = new JPanel();
- contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
- setContentPane(contentPane);
- contentPane.setLayout(null);
-
- JLabel TX = new JLabel("x:");
- TX.setBounds(266, 237, 14, 15);
- contentPane.add(TX);
-
- JLabel TY = new JLabel("y:");
- TY.setBounds(320, 237, 14, 15);
- contentPane.add(TY);
-
- JLabel x = new JLabel("");
- x.setBounds(279, 237, 33, 15);
- contentPane.add(x);
-
-
-
- JLabel y = new JLabel("");
- y.setBounds(332, 237, 33, 15);
- contentPane.add(y);
-
- contentPane.addMouseMotionListener(new MouseMotionAdapter() {
- @Override
- public void mouseMoved(MouseEvent e) {
- int x2 = e.getX();
- int y2 = e.getY();
- x.setText(String.valueOf(x2));
- y.setText(String.valueOf(y2));
- }
- });
-
- }
- }
複製代碼 |