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

  13. public class Test extends JFrame {

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

  35.         /**
  36.          * Create the frame.
  37.          */
  38.         public Test() {
  39.                 ICON1 = new ImageIcon(Main.class.getResource("icon4.png"));
  40.         Image img1 = ICON1.getImage().getScaledInstance(376,208,Image.SCALE_FAST);
  41.       
  42.                 setIconImage(img1);
  43.          
  44.          
  45.                 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  46.                 setBounds(100, 100, 412, 300);
  47.                 contentPane = new JPanel();
  48.                 contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  49.                 setContentPane(contentPane);
  50.                 contentPane.setLayout(null);
  51.                
  52.                 lblNewLabel = new JLabel();
  53.                 lblNewLabel.setBounds(10, 10, 376, 208);
  54.                 contentPane.add(lblNewLabel);
  55.                
  56.                 JButton btnNewButton = new JButton();
  57.                 btnNewButton.setIcon(ciconsm("image3.jpg"));
  58.                 btnNewButton.addActionListener(new ActionListener() {
  59.                         public void actionPerformed(ActionEvent arg0) {
  60.                                 cicon("image3.jpg");
  61.                         }
  62.                 });
  63.                 btnNewButton.setBounds(10, 229, 87, 30);
  64.                 contentPane.add(btnNewButton);
  65.                
  66.                 JButton btnNewButton_1 = new JButton();
  67.                 btnNewButton_1.setIcon(ciconsm("image0.jpg"));
  68.                 btnNewButton_1.addActionListener(new ActionListener() {
  69.                         public void actionPerformed(ActionEvent e) {
  70.                                 cicon("image0.jpg");
  71.                         }
  72.                 });
  73.                 btnNewButton_1.setBounds(107, 229, 87, 30);
  74.                 contentPane.add(btnNewButton_1);
  75.                
  76.                 JButton btnNewButton_2 = new JButton();
  77.                 btnNewButton_2.setIcon(ciconsm("image2.jpg"));
  78.                 btnNewButton_2.addActionListener(new ActionListener() {
  79.                         public void actionPerformed(ActionEvent e) {
  80.                                 cicon("image2.jpg");
  81.                         }
  82.                 });
  83.                 btnNewButton_2.setBounds(201, 229, 87, 30);
  84.                 contentPane.add(btnNewButton_2);
  85.                
  86.                 JButton btnNewButton_3 = new JButton();
  87.                 btnNewButton_3.setIcon(ciconsm("image1.jpg"));
  88.                 btnNewButton_3.addActionListener(new ActionListener() {
  89.                         public void actionPerformed(ActionEvent e) {
  90.                                 cicon("image1.jpg");
  91.                         }
  92.                 });
  93.                 btnNewButton_3.setBounds(298, 229, 87, 30);
  94.                 contentPane.add(btnNewButton_3);
  95.        
  96.                  
  97.         }
  98.         public static void cicon (String str){
  99.                    icon = new ImageIcon(Main.class.getResource(str));
  100.                  Image img = icon.getImage().getScaledInstance(376,208,Image.SCALE_FAST);
  101.                  ImageIcon newicon = new ImageIcon(img);
  102.                  lblNewLabel.setIcon(newicon);
  103.                
  104.         }
  105.         public static ImageIcon ciconsm (String str){
  106.                    iconsm = new ImageIcon(Main.class.getResource(str));
  107.                  Image imgsm = iconsm.getImage().getScaledInstance(47,26,Image.SCALE_FAST);
  108.                  ImageIcon newiconsm = new ImageIcon(imgsm);
  109.                  return newiconsm;
  110.                
  111.         }
  112. }
複製代碼

TOP

返回列表