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

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

  5. import javax.swing.Icon;
  6. import javax.swing.ImageIcon;
  7. import javax.swing.JFrame;
  8. import javax.swing.JPanel;
  9. import javax.swing.border.EmptyBorder;

  10. import java.awt.Toolkit;

  11. import javax.swing.JLabel;

  12. import java.awt.event.MouseAdapter;
  13. import java.awt.event.MouseEvent;

  14. public class Main extends JFrame {

  15.         private JPanel contentPane;
  16.         private ImageIcon icon;
  17.         private JLabel label;
  18.         private int t=0;
  19.         private Icon newIcon;
  20.         /**
  21.          * Launch the application.
  22.          */
  23.         public static void main(String[] args) {
  24.                 EventQueue.invokeLater(new Runnable() {
  25.                         public void run() {
  26.                                 try {
  27.                                         Main frame = new Main();
  28.                                         frame.setVisible(true);
  29.                                 } catch (Exception e) {
  30.                                         e.printStackTrace();
  31.                                 }
  32.                         }
  33.                 });
  34.         }

  35.         /**
  36.          * Create the frame.
  37.          */
  38.         public Main() {
  39.                 setIconImage(Toolkit.getDefaultToolkit().getImage(Main.class.getResource("/bbs/istak/org/tw/star.png")));
  40.                 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  41.                 setBounds(100, 100, 450, 300);
  42.                 contentPane = new JPanel();
  43.                 contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  44.                 setContentPane(contentPane);
  45.                 contentPane.setLayout(null);
  46.                
  47.                 String[] s = {"hippo.png","gift-green.png"};
  48.                
  49.                 icon = new ImageIcon(Main.class.getResource(s[0]));
  50.                 Image img = icon.getImage().getScaledInstance(50, 50, Image.SCALE_FAST);
  51.                 newIcon = new ImageIcon(img);
  52.                
  53.                 JLabel label = new JLabel(newIcon);
  54.                 label.setBounds(143, 57, 115, 111);
  55.                 contentPane.add(label);
  56.                 label.addMouseListener(new MouseAdapter() {
  57.                        

  58.                         @Override
  59.                         public void mousePressed(MouseEvent arg0) {
  60.                                 icon = new ImageIcon(Main.class.getResource(s[t]));
  61.                                 Image img = icon.getImage().getScaledInstance(50, 50, Image.SCALE_FAST);
  62.                                 newIcon = new ImageIcon(img);
  63.                                
  64.                                 label.setIcon(newIcon);
  65.                                
  66.                                
  67.                                 if(t==1)
  68.                                 {
  69.                                         t=0;
  70.                                 }else
  71.                                 {
  72.                                         t++;
  73.                                 }
  74.                         }
  75.                 });

  76.         }

  77. }
複製代碼

TOP

返回列表