返回列表 發帖
  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.JFrame;
  6. import javax.swing.JPanel;
  7. import javax.swing.border.EmptyBorder;
  8. import javax.swing.GroupLayout;
  9. import javax.swing.GroupLayout.Alignment;
  10. import javax.swing.ImageIcon;
  11. import javax.swing.JLabel;
  12. import javax.swing.JButton;

  13. import java.awt.event.ActionListener;
  14. import java.awt.event.ActionEvent;

  15. public class Main extends JFrame {

  16.         private JPanel contentPane;
  17.     private ImageIcon icon;
  18.        
  19.         public static void main(String[] args) {
  20.                 EventQueue.invokeLater(new Runnable() {
  21.                         public void run() {
  22.                                 try {
  23.                                         Main frame = new Main();
  24.                                         frame.setVisible(true);
  25.                                 } catch (Exception e) {
  26.                                         e.printStackTrace();
  27.                                 }
  28.                         }
  29.                 });
  30.         }

  31.         JLabel lblNewLabel;
  32.         public Main() {
  33.                 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  34.                 setBounds(100, 100, 444, 372);
  35.                 contentPane = new JPanel();
  36.                 contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  37.                 setContentPane(contentPane);
  38.                 contentPane.setLayout(null);
  39.                
  40.                 lblNewLabel = new JLabel();
  41.                 lblNewLabel.setBounds(0, 0, 429, 293);
  42.                 contentPane.add(lblNewLabel);
  43.                
  44.                 Changeicon("Koala.jpg");
  45.                
  46.                
  47.                
  48.                 JButton btnNewButton = new JButton("無尾熊");
  49.                 btnNewButton.setBounds(0, 293, 112, 41);
  50.                 contentPane.add(btnNewButton);
  51.                 btnNewButton.addActionListener(new ActionListener() {
  52.              public void actionPerformed(ActionEvent arg0) {
  53.                      Changeicon("Koala.jpg");
  54.              }
  55.                 });                    
  56.                 JButton btnNewButton_1 = new JButton("菊花");
  57.                 btnNewButton_1.setBounds(110, 293, 112, 41);
  58.                 contentPane.add(btnNewButton_1);
  59.                 btnNewButton_1.addActionListener(new ActionListener() {
  60.             public void actionPerformed(ActionEvent arg0) {
  61.                     Changeicon("Chrysanthemum.jpg");
  62.             }
  63.                 });   
  64.                
  65.                 JButton btnNewButton_2 = new JButton("沙漠");
  66.                 btnNewButton_2.addActionListener(new ActionListener() {
  67.                         public void actionPerformed(ActionEvent arg0) {
  68.                                 Changeicon("Desert.jpg");
  69.                         }
  70.                 });
  71.                 btnNewButton_2.setBounds(221, 293, 112, 41);
  72.                 contentPane.add(btnNewButton_2);
  73.                
  74.                 JButton btnNewButton_3 = new JButton("燈塔");
  75.                 btnNewButton_3.addActionListener(new ActionListener() {
  76.                         public void actionPerformed(ActionEvent e) {                       
  77.                                 Changeicon("Lighthouse.jpg");
  78.                         }
  79.                 });
  80.                 btnNewButton_3.setBounds(329, 293, 100, 41);
  81.                 contentPane.add(btnNewButton_3);
  82.                
  83.        
  84.                
  85.         }

  86.         public void Changeicon(String str)
  87.         {       
  88.                 icon = new ImageIcon(Main.class.getResource(str));
  89.                 Image img = icon.getImage().getScaledInstance(429, 293, java.awt.Image.SCALE_FAST);
  90.                 ImageIcon newicon = new ImageIcon(img);
  91.                 lblNewLabel.setIcon(newicon);       
  92.         }

  93. }
複製代碼

TOP

返回列表