返回列表 發帖

ImageIcon 類別 (四)

試以陣列方式布置大量元件.

JFrame 的設定: 100, 100, 420, 320
JLabel 的設定:
12, 10, 128, 128
142, 10, 128, 128
272, 10, 128, 128
12, 150, 128, 128
142, 150, 128, 128
272, 150, 128, 128



素材:pic.zip
  1. import javax.swing.JFrame;
  2. import javax.swing.JLabel;
  3. import javax.swing.ImageIcon;

  4. public class Ch87 {

  5.         JFrame fm;
  6.         JLabel lb[]=new JLabel[6];
  7.         ImageIcon ii[]=new ImageIcon[6];
  8.         ImageIcon icon;
  9.        
  10.         Ch87()
  11.         {
  12.                 icon=new ImageIcon(Ch87.class.getResource("pic/icon.png"));
  13.                 for(int i=0; i<6; i++)
  14.                         ii[i]=new ImageIcon(Ch87.class.getResource("pic/0"+(i+1)+".png"));
  15.                
  16.                 for(int i=0; i<6; i++)
  17.                         lb[i]=new JLabel(ii[i]);
  18.                
  19.                 for(int i=0; i<3; i++)
  20.                         lb[i].setBounds(12+i*130, 10, 128, 128);
  21.                
  22.                 for(int i=3; i<6; i++)
  23.                         lb[i].setBounds(12+(i-3)*130, 150, 128, 128);
  24.                
  25.                 fm=new JFrame("以陣列方式布置大量元件");
  26.                 fm.setBounds(100, 100, 420, 320);
  27.                 fm.setVisible(true);
  28.                 fm.setResizable(false);
  29.                 fm.setLayout(null);
  30.                 fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  31.                 fm.setIconImage(icon.getImage());
  32.                 for(int i=0; i<6; i++)
  33.                         fm.add(lb[i]);       
  34.         }
  35.        
  36.         public static void main(String[] args) {
  37.                 new Ch87();
  38.         }
  39. }
複製代碼

返回列表