文档库 最新最全的文档下载
当前位置:文档库 › java任意显示三张牌

java任意显示三张牌

import javax.swing.*;
import java.awt.*;

public class Exercise12_09 extends JFrame {
private ImageIcon aIcon ;
private ImageIcon bIcon ;
private ImageIcon cIcon ;

public void assignName(){
int a=(int)(Math.random()*(54-1)+1);
System.out.println("a="+a);
aIcon=new ImageIcon("card/"+a+".png");
int b=(int)(Math.random()*(54-1)+1);
System.out.println("b="+b);
bIcon=new ImageIcon("card/"+b+".png");
int c=(int)(Math.random()*(54-1)+1);
System.out.println("c="+c);
cIcon=new ImageIcon("card/"+c+".png");
}

public Exercise12_09() {
assignName();
setLayout(new GridLayout(1, 3, 5, 5));
add(new JButton(aIcon));
add(new JButton(bIcon));
add(new JButton(cIcon));
}

/** Main method */
public static void main(String[] args) {
Exercise12_09 frame = new Exercise12_09();
frame.setTitle("TestImageIcon");
frame.setSize(300, 150);
frame.setLocationRelativeTo(null); // Center the frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}

相关文档