標籤:tcl xtend random operation 需要 this set size imp
1 package test.com; 2 3 import java.awt.GridLayout; 4 5 import javax.swing.*; 6 /* 7 * 顯示一個包含9個標籤的架構,標籤有3類圖形。圈、叉和空白 8 * 每次運行隨機顯示 9 */10 public class Game extends JFrame {11 private ImageIcon x = new ImageIcon("d:\\x.jpg");12 private ImageIcon o = new ImageIcon("d:\\o.jpg");13 private ImageIcon w = new ImageIcon("d:\\w.jpg");14 15 16 public static void main(String[] args) {17 Game pictureGame = new Game();18 pictureGame.setTitle("pictrueGame");19 pictureGame.setSize(400, 400);20 pictureGame.setLocationRelativeTo(null);21 pictureGame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);22 pictureGame.setVisible(true);23 24 25 }26 27 //建立一個GridLayout布局28 public Game(){29 setLayout(new GridLayout(3,3,5,5));30 this.random();31 }32 33 //隨機顯示的方法34 public void random(){35 int j = 0;36 while(j<9){37 int i = (int) (Math.random()*3);38 switch(i){39 case 0 : 40 add(new JLabel(o));break; 41 case 1 : 42 add(new JLabel(x));break;43 case 2 : 44 add(new JLabel(w));break;45 }46 j++;47 }48 }49 }
小練習。
好像沒有什麼需要注意的東西。
Java語言程式設計 基礎篇 編程練習題 12.7