本帖最後由 周政輝 於 2018-4-14 12:04 編輯
試完成一翻牌遊戲之雛形, 執行畫面如下, 該視窗程式需做到:
1. 六組各兩張的圖片, 能隨機出現在被點擊的按鈕上.
2. 當點擊右下角的回復鈕, 所有圖片皆回復原狀, 並重新洗牌.
素材: pic.zip- package tw.kuas.edu.tw;
- import java.awt.BorderLayout;
- import java.awt.EventQueue;
- import java.awt.Image;
- import javax.swing.ImageIcon;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import javax.swing.border.EmptyBorder;
- import java.awt.FlowLayout;
- import javax.swing.JLabel;
- public class Main extends JFrame {
- private JPanel contentPane;
- /**
- * Launch the application.
- */
- public static void main(String[] args) {
- EventQueue.invokeLater(new Runnable() {
- public void run() {
- try {
- Main frame = new Main();
- frame.setVisible(true);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- }
- /**
- * Create the frame.
- */
- JLabel[] pokers = new JLabel[12];
- ImageIcon[] randomcard = new ImageIcon[12];
- int randomnum[]=new int[12];
- public Main() {
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setBounds(100, 100, 662, 499);
-
- contentPane = new JPanel();
- contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
- setContentPane(contentPane);
- contentPane.setLayout(null);
-
- JLabel Poker_1 = new JLabel("New label");
- Poker_1.setBounds(10, 10, 128, 128);
- contentPane.add(Poker_1);
- pokers[0] = Poker_1;
-
- JLabel Poker_2 = new JLabel("New label");
- Poker_2.setBounds(159, 10, 128, 128);
- contentPane.add(Poker_2);
- pokers[1] = Poker_2;
-
- JLabel Poker_3 = new JLabel("New label");
- Poker_3.setBounds(304, 10, 128, 128);
- contentPane.add(Poker_3);
- pokers[2] = Poker_3;
-
- JLabel Poker_4 = new JLabel("New label");
- Poker_4.setBounds(453, 10, 128, 128);
- contentPane.add(Poker_4);
- pokers[3] = Poker_4;
-
- JLabel Poker_5 = new JLabel("New label");
- Poker_5.setBounds(10, 155, 128, 128);
- contentPane.add(Poker_5);
- pokers[4] = Poker_5;
-
- JLabel Poker_6 = new JLabel("New label");
- Poker_6.setBounds(159, 155, 128, 128);
- contentPane.add(Poker_6);
- pokers[5] = Poker_6;
- JLabel Poker_7 = new JLabel("New label");
- Poker_7.setBounds(304, 155, 128, 128);
- contentPane.add(Poker_7);
- pokers[6] = Poker_7;
-
- JLabel Poker_8 = new JLabel("New label");
- Poker_8.setBounds(453, 155, 128, 128);
- contentPane.add(Poker_8);
- pokers[7] = Poker_8;
-
- JLabel Poker_9 = new JLabel("New label");
- Poker_9.setBounds(10, 293, 128, 128);
- contentPane.add(Poker_9);
- pokers[8] = Poker_9;
-
- JLabel Poker_10 = new JLabel("New label");
- Poker_10.setBounds(159, 293, 128, 128);
- contentPane.add(Poker_10);
- pokers[9] = Poker_10;
-
- JLabel Poker_11 = new JLabel("New label");
- Poker_11.setBounds(304, 293, 128, 128);
- contentPane.add(Poker_11);
- pokers[10] = Poker_11;
- JLabel Poker_12 = new JLabel("New label");
- Poker_12.setBounds(453, 293, 128, 128);
- contentPane.add(Poker_12);
- pokers[11] = Poker_12;
-
-
- InitializeImg(pokers);
- rc();
- JLabel Reset = new JLabel("New label");
- Reset.setIcon(new ImageIcon(Main.class.getResource("/tw/kuas/edu/tw/pic/return.png")));
- Reset.setBounds(603, 418, 33, 33);
- contentPane.add(Reset);
- for(int i=0;i<12;i++) {
- System.out.println(randomnum[i]);
- }
-
- }
-
- private ImageIcon InitImg() {
- ImageIcon icon = new ImageIcon(Main.class.getResource("pic/0.png"));
- Image img = icon.getImage().getScaledInstance(128, 128, java.awt.Image.SCALE_FAST );
- ImageIcon newIcon = new ImageIcon(img);
- return newIcon;
- }
-
- private void InitializeImg(JLabel[] pokers) {
- for(int i=0;i<pokers.length;i++) {
- ImageIcon icon = new ImageIcon(Main.class.getResource("pic/0.png"));
- Image img = icon.getImage().getScaledInstance(128, 128, java.awt.Image.SCALE_FAST );
- ImageIcon newIcon = new ImageIcon(img);
- pokers[i].setIcon(newIcon);
- }
- }
-
- private void rc() {
- for(int i=0; i<12; i++) {
- randomnum[i]=(int)(Math.random()*12); //0~11
- for(int j=0; j<i; j++) {
- if(randomnum[i]==randomnum[j]) {
- i--;
- break;
- }
- }
- }
- }
-
-
- }
複製代碼 |