返回列表 發帖
  1. package bbs.istak.org.tw;

  2. import java.awt.BorderLayout;
  3. import java.awt.EventQueue;

  4. import javax.swing.JFrame;
  5. import javax.swing.JPanel;
  6. import javax.swing.border.EmptyBorder;
  7. import javax.swing.JLabel;
  8. import java.awt.event.MouseMotionAdapter;
  9. import java.awt.event.MouseEvent;

  10. public class Main extends JFrame {

  11.         private JPanel contentPane;

  12.         /**
  13.          * Launch the application.
  14.          */
  15.         public static void main(String[] args) {
  16.                 EventQueue.invokeLater(new Runnable() {
  17.                         public void run() {
  18.                                 try {
  19.                                         Main frame = new Main();
  20.                                         frame.setVisible(true);
  21.                                 } catch (Exception e) {
  22.                                         e.printStackTrace();
  23.                                 }
  24.                         }
  25.                 });
  26.         }

  27.         /**
  28.          * Create the frame.
  29.          */
  30.         public Main() {
  31.                 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  32.                 setBounds(100, 100, 450, 300);
  33.                 contentPane = new JPanel();

  34.                 contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  35.                 setContentPane(contentPane);
  36.                 contentPane.setLayout(null);
  37.                
  38.                 JLabel TX = new JLabel("x:");
  39.                 TX.setBounds(266, 237, 14, 15);
  40.                 contentPane.add(TX);
  41.                
  42.                 JLabel TY = new JLabel("y:");
  43.                 TY.setBounds(320, 237, 14, 15);
  44.                 contentPane.add(TY);
  45.                
  46.                 JLabel x = new JLabel("");
  47.                 x.setBounds(279, 237, 33, 15);
  48.                 contentPane.add(x);
  49.                
  50.                
  51.                
  52.                 JLabel y = new JLabel("");
  53.                 y.setBounds(332, 237, 33, 15);
  54.                 contentPane.add(y);
  55.                
  56.                 contentPane.addMouseMotionListener(new MouseMotionAdapter() {
  57.                         @Override
  58.                         public void mouseMoved(MouseEvent e) {
  59.                                 int x2 = e.getX();
  60.                                 int y2 = e.getY();
  61.                                 x.setText(String.valueOf(x2));
  62.                                 y.setText(String.valueOf(y2));
  63.                         }
  64.                 });
  65.                
  66.         }
  67. }
複製代碼

TOP

返回列表