返回列表 發帖
本帖最後由 張健勳 於 2018-3-4 22:22 編輯
  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.Toolkit;
  9. import javax.swing.ImageIcon;
  10. import java.awt.event.MouseAdapter;
  11. import java.awt.event.MouseEvent;

  12. public class Main extends JFrame {

  13.         private JPanel contentPane;
  14.         private int x,y;

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

  30.         /**
  31.          * Create the frame.
  32.          */
  33.         public Main() {
  34.                 setIconImage(Toolkit.getDefaultToolkit().getImage(Main.class.getResource("icon.png")));
  35.                 setTitle("\u6ED1\u9F20\u6307\u6A19\u727D\u5F15\u5716\u6848");
  36.                 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  37.                 setBounds(100, 100, 450, 300);
  38.                 contentPane = new JPanel();
  39.                 contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  40.                 setContentPane(contentPane);
  41.                 contentPane.setLayout(null);
  42.                
  43.                 JLabel lb = new JLabel("");
  44.                
  45.                
  46.                 addMouseListener(new MouseAdapter() {
  47.                         @Override
  48.                         public void mouseClicked(MouseEvent a) {
  49.                                 x = a.getX();
  50.                                 y = a.getY();
  51.                                
  52.                                
  53.                                 lb.setLocation(x,y);
  54.                                
  55.                         }
  56.                 });
  57.                
  58.                
  59.                 lb.setIcon(new ImageIcon(Main.class.getResource("icon.png")));
  60.                 lb.setBounds(10, 10, 89, 79);
  61.                 contentPane.add(lb);

  62.         }
  63. }
複製代碼

TOP

返回列表