返回列表 發帖
  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 java.awt.GridBagLayout;
  8. import com.jgoodies.forms.layout.FormLayout;
  9. import com.jgoodies.forms.layout.ColumnSpec;
  10. import com.jgoodies.forms.layout.RowSpec;
  11. import javax.swing.GroupLayout;
  12. import javax.swing.GroupLayout.Alignment;
  13. import javax.swing.JTextField;
  14. import javax.swing.JButton;
  15. import java.awt.event.ActionListener;
  16. import java.awt.event.ActionEvent;
  17. import javax.swing.JLabel;
  18. import javax.swing.ImageIcon;

  19. public class Main extends JFrame {

  20.         private JPanel contentPane;

  21.         /**
  22.          * Launch the application.
  23.          */
  24.         public static void main(String[] args) {
  25.                 EventQueue.invokeLater(new Runnable() {
  26.                         public void run() {
  27.                                 try {
  28.                                         Main frame = new Main();
  29.                                         frame.setVisible(true);
  30.                                 } catch (Exception e) {
  31.                                         e.printStackTrace();
  32.                                 }
  33.                         }
  34.                 });
  35.         }

  36.         /**
  37.          * Create the frame.
  38.          */
  39.         public Main() {
  40.                 setTitle("ImageIcon");
  41.                 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  42.                 setBounds(100, 100, 314, 396);
  43.                 contentPane = new JPanel();
  44.                 contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  45.                 setContentPane(contentPane);
  46.                 contentPane.setLayout(null);
  47.                
  48.                 JLabel lblNewLabel = new JLabel("Welcome");
  49.                 lblNewLabel.setLabelFor(this);
  50.                 lblNewLabel.setBounds(-1, 0, 296, 276);
  51.                 contentPane.add(lblNewLabel);
  52.                
  53.                 JButton btn2 = new JButton("\u7761\u89BA");
  54.                 btn2.setIcon(new ImageIcon(Main.class.getResource("/bbs/istak/org/tw/parrot.png")));
  55.                 btn2.setBounds(142, 274, 153, 39);
  56.                 contentPane.add(btn2);
  57.                
  58.                 JButton btn4 = new JButton("\u554F\u865F");
  59.                 btn4.setIcon(new ImageIcon("C:\\Users\\15LiveTW\\Downloads\\sleep.jfif"));
  60.                 btn4.setBounds(142, 310, 153, 40);
  61.                 contentPane.add(btn4);
  62.                
  63.                 JButton btn3 = new JButton("\u9E1A\u9D61");
  64.                 btn3.setIcon(new ImageIcon(Main.class.getResource("/bbs/istak/org/tw/qm.jpg")));
  65.                 btn3.addActionListener(new ActionListener() {
  66.                         public void actionPerformed(ActionEvent arg0) {
  67.                         }
  68.                 });
  69.                 btn3.setBounds(0, 310, 145, 40);
  70.                 contentPane.add(btn3);
  71.                
  72.                 JButton btn1 = new JButton("\u8C93\u54AA");
  73.                 btn1.setIcon(new ImageIcon("C:\\Users\\15LiveTW\\Downloads\\meow.jfif"));
  74.                 btn1.setBounds(-1, 274, 145, 38);
  75.                 contentPane.add(btn1);
  76.                
  77.                 JButton icon = new JButton("\u8C93\u54AA");
  78.                 icon.addActionListener(new ActionListener() {       
  79.                        
  80.                         public void actionPerformed(ActionEvent e) {
  81.                        
  82.                                 if(e.getSource()==btn1)                
  83.                                 {
  84.                                         lblNewLabel.setIcon(new ImageIcon("meow.jifi"));
  85.                         }
  86.                                 if(e.getSource()==btn2)
  87.                         {
  88.                                         lblNewLabel.setIcon(new ImageIcon("parrot.png"));
  89.                         }
  90.                                 if(e.getSource()==btn4)
  91.                                 {
  92.                                         lblNewLabel.setIcon(new ImageIcon("qm.jpg"));
  93.                                 }
  94.                                 if(e.getSource()==btn3)
  95.                                 {
  96.                                         lblNewLabel.setIcon(new ImageIcon("sleep.jifi"));
  97.                                 }

  98.                         }
  99.                 });

  100.                
  101.        
  102.                
  103.                        
  104.         }
  105. }
複製代碼

TOP

返回列表