標題:
翻牌遊戲(二)
[打印本頁]
作者:
周政輝
時間:
2018-4-21 11:47
標題:
翻牌遊戲(二)
本帖最後由 周政輝 於 2018-4-21 11:52 編輯
接續上周進度,能讓12張撲克牌進行翻牌
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;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
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];
ImageIcon[] answercard = 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();
InitAnsImg(randomnum);
AddListener();
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(answercard[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;
}
}
}
}
private void InitAnsImg(int[] randomnum) {
for(int i=1;i<=6;i++) {
ImageIcon icon = new ImageIcon(Main.class.getResource(String.format("pic/%d.png",i)));
Image img = icon.getImage().getScaledInstance(128, 128, java.awt.Image.SCALE_FAST );
answercard[randomnum[i-1]] = new ImageIcon(img);
answercard[randomnum[i+5]] = new ImageIcon(img);
}
}
private void AddListener() {
for(int i=0;i<pokers.length;i++) {
pokers[i].addMouseListener(adapter);
}
}
// 給予一個共用的MouseAdapter事件
MouseAdapter adapter=new MouseAdapter(){
@Override
public void mouseClicked(MouseEvent e)
{
for(int i=0; i<12; i++)
{
if(e.getSource()==pokers[i]) {
pokers[i].setIcon(answercard[i]);
}
}
}
};
}
複製代碼
作者:
蔡庭豪
時間:
2018-4-21 12:03
package bbs.istak.org.tw;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Image;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
public class PtSC extends JFrame {
private JPanel contentPane;
private static ImageIcon icon;
private ImageIcon poker[] = new ImageIcon [12];
private int num[] = new int [12];
private ImageIcon answer[] = new ImageIcon [12];
private JLabel larray[] = new JLabel [12];
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
PtSC frame = new PtSC();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public PtSC() {
icon = new ImageIcon(PtSC.class.getResource("star.png"));
Image imgicon = icon.getImage().getScaledInstance(128,128,java.awt.Image.SCALE_FAST );
this.setIconImage(imgicon);
larray = new JLabel [12];
poker = new ImageIcon[12];
answer = new ImageIcon[12];
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 620, 462);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel label_0 = new JLabel(pokercard(0));
label_0.setBounds(10, 10, 128, 128);
contentPane.add(label_0);
larray[0] = label_0;
JLabel label_1 = new JLabel(pokercard(1));
label_1.setBounds(148, 10, 128, 128);
contentPane.add(label_1);
larray[1] = label_1;
JLabel label_2 = new JLabel(pokercard(2));
label_2.setBounds(286, 10, 128, 128);
contentPane.add(label_2);
larray[2] = label_2;
JLabel label_3 = new JLabel(pokercard(3));
label_3.setBounds(424, 10, 128, 128);
contentPane.add(label_3);
larray[3] = label_3;
JLabel label_4 = new JLabel(pokercard(4));
label_4.setBounds(10, 149, 128, 128);
contentPane.add(label_4);
larray[4] = label_4;
JLabel label_5 = new JLabel(pokercard(5));
label_5.setBounds(148, 149, 128, 128);
contentPane.add(label_5);
larray[5] = label_5;
JLabel label_6 = new JLabel(pokercard(6));
label_6.setBounds(286, 149, 128, 128);
contentPane.add(label_6);
larray[6] = label_6;
JLabel label_7 = new JLabel(pokercard(7));
label_7.setBounds(424, 149, 128, 128);
contentPane.add(label_7);
larray[7] = label_7;
JLabel label_8 = new JLabel(pokercard(8));
label_8.setBounds(10, 286, 128, 128);
contentPane.add(label_8);
larray[8] = label_8;
JLabel label_9 = new JLabel(pokercard(9));
label_9.setBounds(148, 286, 128, 128);
contentPane.add(label_9);
larray[9] = label_9;
JLabel label_10 = new JLabel(pokercard(10));
label_10.setBounds(286, 286, 128, 128);
contentPane.add(label_10);
larray[10] = label_10;
JLabel label_11 = new JLabel(pokercard(11));
label_11.setBounds(424, 286, 128, 128);
contentPane.add(label_11);
larray[11] = label_11;
icon = new ImageIcon(PtSC.class.getResource("return.png"));
Image bimg = icon.getImage().getScaledInstance(33,33,java.awt.Image.SCALE_FAST );
ImageIcon back = new ImageIcon(bimg);
JLabel labelreset = new JLabel(back);
labelreset.setBounds(562, 10, 33, 33);
contentPane.add(labelreset);
num = new int [12];
for(int i = 0 ; i < num.length ; i++){
num[i] = (int) (Math.random()*12);
for(int j = 0 ; j < i ; j++){
if(num[i] == num[j]){
i--;
break;
}
}
}
for (int k = 0 ; k < num.length ; k++){
System.out.println(num[k]);
}
backcard(num);
for(int i = 0 ; i < larray.length ; i++){
}
}
private ImageIcon pokercard (int n){
poker = new ImageIcon[12];
icon = new ImageIcon(PtSC.class.getResource("0.png"));
Image andimg = icon.getImage().getScaledInstance(128,128,java.awt.Image.SCALE_FAST );
ImageIcon andriod = new ImageIcon(andimg);
for(int a = 0 ; a < poker.length ; a++){
poker[a] = andriod;
}
return poker[n];
}
private void backcard (int[] num){
for(int x = 1 ; x <= 6 ; x++){
icon = new ImageIcon(PtSC.class.getResource(String.format("%d.png", x)));
Image img = icon.getImage().getScaledInstance(128,128,java.awt.Image.SCALE_FAST );
ImageIcon answercard = new ImageIcon(img);
answer[num[x-1]] = answercard;
answer[num[x+5]] = answercard;
}
}
private void AddListener(){
for(int i = 0 ; i < larray.length ; i++)
larray[i].addMouseListener(adpater);
}
MouseAdapter adpater = new MouseAdapter(
private void MouseClicked(MouseEvent e){
}
);
}
複製代碼
作者:
林侑成
時間:
2018-4-21 12:03
本帖最後由 林侑成 於 2018-4-28 12:00 編輯
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;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
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];
ImageIcon[] answercard = 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();
InitAnsImg(randomnum);
AddListener();
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(answercard[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;
}
}
}
}
private void InitAnsImg(int[] randomnum) {
for(int i=1;i<=6;i++) {
ImageIcon icon = new ImageIcon(Main.class.getResource(String.format("pic/%d.png",i)));
Image img = icon.getImage().getScaledInstance(128, 128, java.awt.Image.SCALE_FAST );
answercard[randomnum[i-1]] = new ImageIcon(img);
answercard[randomnum[i+5]] = new ImageIcon(img);
}
}
private void AddListener() {
for(int i=0;i<pokers.length;i++) {
pokers[i].addMouseListener(adapter);
}
}
// 給予一個共用的MouseAdapter事件
MouseAdapter adapter=new MouseAdapter(){
@Override
public void mouseClicked(MouseEvent e)
{
for(int i=0; i<12; i++)
{
if(e.getSource()==pokers[i]) {
pokers[i].setIcon(answercard[i]);
}
}
}
};
}
複製代碼
作者:
張健勳
時間:
2018-4-22 21:15
順便把Reset做好了
package bbs.istak.org.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;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.Toolkit;
public class Game extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Game frame = new Game();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
JLabel[] pokers = new JLabel[12];
ImageIcon[] randomcard = new ImageIcon[12];
ImageIcon[] answercard = new ImageIcon[12];
int randomnumber[]=new int[12];
public Game() {
setIconImage(Toolkit.getDefaultToolkit().getImage(Game.class.getResource("pic/star.png")));
setTitle("\u7FFB\u724C\u904A\u6232");
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();
Poker_1.setBounds(10, 10, 128, 128);
contentPane.add(Poker_1);
pokers[0] = Poker_1;
JLabel Poker_2 = new JLabel();
Poker_2.setBounds(159, 10, 128, 128);
contentPane.add(Poker_2);
pokers[1] = Poker_2;
JLabel Poker_3 = new JLabel();
Poker_3.setBounds(304, 10, 128, 128);
contentPane.add(Poker_3);
pokers[2] = Poker_3;
JLabel Poker_4 = new JLabel();
Poker_4.setBounds(453, 10, 128, 128);
contentPane.add(Poker_4);
pokers[3] = Poker_4;
JLabel Poker_5 = new JLabel();
Poker_5.setBounds(10, 155, 128, 128);
contentPane.add(Poker_5);
pokers[4] = Poker_5;
JLabel Poker_6 = new JLabel();
Poker_6.setBounds(159, 155, 128, 128);
contentPane.add(Poker_6);
pokers[5] = Poker_6;
JLabel Poker_7 = new JLabel();
Poker_7.setBounds(304, 155, 128, 128);
contentPane.add(Poker_7);
pokers[6] = Poker_7;
JLabel Poker_8 = new JLabel();
Poker_8.setBounds(453, 155, 128, 128);
contentPane.add(Poker_8);
pokers[7] = Poker_8;
JLabel Poker_9 = new JLabel();
Poker_9.setBounds(10, 293, 128, 128);
contentPane.add(Poker_9);
pokers[8] = Poker_9;
JLabel Poker_10 = new JLabel();
Poker_10.setBounds(159, 293, 128, 128);
contentPane.add(Poker_10);
pokers[9] = Poker_10;
JLabel Poker_11 = new JLabel();
Poker_11.setBounds(304, 293, 128, 128);
contentPane.add(Poker_11);
pokers[10] = Poker_11;
JLabel Poker_12 = new JLabel();
Poker_12.setBounds(453, 293, 128, 128);
contentPane.add(Poker_12);
pokers[11] = Poker_12;
InitializeImg(pokers);
rcard();
InitAnsImg(randomnumber);
AddListener();
JLabel Reset = new JLabel();
Reset.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent a) {
if(a.getSource()==Reset)
{
InitializeImg(pokers);
}
}
});
Reset.setIcon(new ImageIcon(Game.class.getResource("pic/R.png")));
Reset.setBounds(603, 418, 33, 33);
contentPane.add(Reset);
for(int i=0;i<12;i++) {
System.out.println(answercard[i]);
}
}
private ImageIcon Android() {
ImageIcon Android = new ImageIcon(Game.class.getResource("pic/0.png"));
Image AndroidImg = Android.getImage().getScaledInstance(128, 128, java.awt.Image.SCALE_FAST );
ImageIcon nAndroid = new ImageIcon(AndroidImg);
return nAndroid;
}
private void InitializeImg(JLabel[] pokers) {
for(int i=0;i<pokers.length;i++) {
ImageIcon icon = new ImageIcon(Game.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 rcard() {
for(int i=0; i<12; i++) {
randomnumber[i]=(int)(Math.random()*12); //0~11
for(int j=0; j<i; j++) {
if(randomnumber[i]==randomnumber[j]) {
i--;
break;
}
}
}
}
private void InitAnsImg(int[] randomnum) {
for(int i=1;i<=6;i++) {
ImageIcon icon = new ImageIcon(Game.class.getResource(String.format("pic/%d.png",i)));
Image img = icon.getImage().getScaledInstance(128, 128, java.awt.Image.SCALE_FAST );
answercard[randomnumber[i-1]] = new ImageIcon(img);
answercard[randomnumber[i+5]] = new ImageIcon(img);
}
}
private void AddListener() {
for(int i=0;i<pokers.length;i++) {
pokers[i].addMouseListener(adapter);
}
}
MouseAdapter adapter=new MouseAdapter(){
@Override
public void mouseClicked(MouseEvent e)
{
for(int i=0; i<12; i++)
{
if(e.getSource()==pokers[i]) {
pokers[i].setIcon(answercard[i]);
}
}
}
};
}
複製代碼
作者:
黃茂勛
時間:
2018-5-21 21:03
package bbs.istak.org.tw;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Image;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Main extends JFrame {
JLabel lb13;
private JPanel contentPane;
public JLabel ii_b[] = new JLabel[12];
public ImageIcon ii_r[] = new ImageIcon[12];
public ImageIcon[] card = new ImageIcon[12];
public int randomnum[] = new int[12];
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Main frame = new Main();
frame.setVisible(true);
frame.setTitle("翻牌遊戲");
ImageIcon icon = new ImageIcon(Main.class.getResource("star.png"));
Image image = icon.getImage();
frame.setIconImage(image);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Main() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 589, 518);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lb1 = new JLabel("lb1");
lb1.setBounds(10, 20, 128, 128);
contentPane.add(lb1);
//lb1.setIcon(entericon(0));
ii_b[0] = lb1;
JLabel lb2 = new JLabel("lb2");
lb2.setBounds(148, 20, 128, 128);
contentPane.add(lb2);
//lb2.setIcon(entericon(0));
ii_b[1] = lb2;
JLabel lb3 = new JLabel("lb3");
lb3.setBounds(288, 20, 128, 128);
contentPane.add(lb3);
//lb3.setIcon(entericon(0));
ii_b[2] = lb3;
JLabel lb4 = new JLabel("lb4");
lb4.setBounds(426, 20, 128, 128);
contentPane.add(lb4);
//lb4.setIcon(entericon(0));
ii_b[3] = lb4;
JLabel lb5 = new JLabel("lb5");
lb5.setBounds(10, 155, 128, 128);
contentPane.add(lb5);
//lb5.setIcon(entericon(0));
ii_b[4] = lb5;
JLabel lb6 = new JLabel("lb6");
lb6.setBounds(148, 155, 128, 128);
contentPane.add(lb6);
//lb6.setIcon(entericon(0));
ii_b[5] = lb6;
JLabel lb7 = new JLabel("lb7");
lb7.setBounds(288, 155, 128, 128);
contentPane.add(lb7);
//lb7.setIcon(entericon(0));
ii_b[6] = lb7;
JLabel lb8 = new JLabel("lb8");
lb8.setBounds(426, 155, 128, 128);
contentPane.add(lb8);
//lb8.setIcon(entericon(0));
ii_b[7] = lb8;
JLabel lb9 = new JLabel("lb9");
lb9.setBounds(10, 293, 128, 128);
contentPane.add(lb9);
//lb9.setIcon(entericon(0));
ii_b[8] = lb9;
JLabel lb10 = new JLabel("lb10");
lb10.setBounds(148, 293, 128, 128);
contentPane.add(lb10);
//lb10.setIcon(entericon(0));
ii_b[9] = lb10;
JLabel lb11 = new JLabel("lb11");
lb11.setBounds(288, 293, 128, 128);
contentPane.add(lb11);
//lb11.setIcon(entericon(0));
ii_b[10] = lb11;
JLabel lb12 = new JLabel("lb12");
lb12.setBounds(426, 293, 128, 128);
contentPane.add(lb12);
//lb12.setIcon(entericon(0));
ii_b[11] = lb12;
lb13 = new JLabel("lb13");
lb13.setBounds(521, 431, 33, 33);
contentPane.add(lb13);
lb13.setIcon(new ImageIcon(Main.class.getResource("/bbs/istak/org/tw/13.png")));
lb13.addMouseListener(adapter);
entericon_2(ii_b);
randomcard();
enter_rc(randomnum);
addlistener();
}
/*
public ImageIcon entericon(int n)
{
ImageIcon icon = new ImageIcon(Main.class.getResource(n+".png"));
return icon;
}
*/
public void entericon_2(JLabel[] lb)
{
for(int i=0; i<lb.length; i++)
{
ImageIcon icon = new ImageIcon(Main.class.getResource(String.format("%d.%s", 0,"png")));
lb[i].setIcon(icon);
lb[i].setSize(128, 128);
}
}
public void randomcard()
{
for(int i=0; i<12; i++)
{
randomnum[i]=(int)(Math.random()*12);
for(int j=0; j<i; j++)
{
if(randomnum[i]==randomnum[j])
{
i--;
break;
}
}
}
}
private void enter_rc(int[] randomnum)
{
for(int i=1; i<=6; i++)
{
ImageIcon icon = new ImageIcon(Main.class.getResource(String.format("%d.png", i)));
Image img = icon.getImage().getScaledInstance(128, 128, java.awt.Image.SCALE_FAST );
ii_r[randomnum[i-1]]=new ImageIcon(img);
ii_r[randomnum[i+5]]=new ImageIcon(img);
//Ex 0 配 0+6 相同ImageIcon
}
}
private void addlistener()
{
for(int i=0; i<ii_b.length; i++)
ii_b[i].addMouseListener(adapter);
}
MouseAdapter adapter=new MouseAdapter(){
public void mouseClicked(MouseEvent e)
{
ImageIcon icon = new ImageIcon(Main.class.getResource("0.png"));
//Image img = icon.getImage().getScaledInstance(128, 128, java.awt.Image.SCALE_FAST );
for(int i=0; i<12; i++)
{
if(e.getSource()==ii_b[i]) {
ii_b[i].setIcon(ii_r[i]);
}
}
if(e.getSource()==lb13)
{
for(int i=0; i<12; i++)
ii_b[i].setIcon(icon);
}
}
};
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://seed.istak.org.tw/)
Powered by Discuz! 7.2