您的位置:首页 > 编程语言 > Java开发

<<Java语言程序设计>>基础篇 Exercise12_9 随机显示3张牌

2014-02-19 23:04 465 查看
import javax.swing.*;
import java.awt.*;

public class Exercise12_9 extends JFrame{
public Exercise12_9(){
setLayout(new GridLayout(1,3,2,2));
int a = (int)(Math.random()*65);
int t = (int)(Math.random()*65);

while (t == a)
t = (int)(Math.random()*65);

int b = t;

t = (int)(Math.random()*65);
while ((t == a) || (t == b))
t = (int)(Math.random()*65);

int c = t;

ImageIcon imagea = new ImageIcon("image/card/" + a + ".png");
ImageIcon imageb = new ImageIcon("image/card/" + b + ".png");
ImageIcon imagec = new ImageIcon("image/card/" + c + ".png");
add(new JLabel(imagea));
add(new JLabel(imageb));
add(new JLabel(imagec));
}

public static void main(String[] args){
Exercise12_9 frame = new Exercise12_9();
frame.setTitle("Exercise12_9");
frame.setSize(1200,400);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: