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 }
小练习。
好像没有什么需要注意的东西。
转载于:https://www.cnblogs.com/nero3/p/7260669.html