標題:
翻牌遊戲 (一)
[打印本頁]
作者:
周政輝
時間:
2018-4-14 10:15
標題:
翻牌遊戲 (一)
本帖最後由 周政輝 於 2018-4-14 12:04 編輯
試完成一翻牌遊戲之雛形, 執行畫面如下, 該視窗程式需做到:
1. 六組各兩張的圖片, 能隨機出現在被點擊的按鈕上.
2. 當點擊右下角的回復鈕, 所有圖片皆回復原狀, 並重新洗牌.
[attach]3845[/attach]
[attach]3846[/attach]
[attach]3847[/attach]
素材:
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;
}
}
}
}
}
複製代碼
作者:
黃茂勛
時間:
2018-4-14 11:56
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 javax.swing.JLabel;
public class Main extends JFrame {
private JPanel contentPane;
public JLabel lb[] = new JLabel[12];
public ImageIcon[] card = new ImageIcon[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));
lb[0] = lb1;
JLabel lb2 = new JLabel("lb2");
lb2.setBounds(148, 20, 128, 128);
contentPane.add(lb2);
//lb2.setIcon(entericon(0));
lb[1] = lb2;
JLabel lb3 = new JLabel("lb3");
lb3.setBounds(288, 20, 128, 128);
contentPane.add(lb3);
//lb3.setIcon(entericon(0));
lb[2] = lb3;
JLabel lb4 = new JLabel("lb4");
lb4.setBounds(426, 20, 128, 128);
contentPane.add(lb4);
//lb4.setIcon(entericon(0));
lb[3] = lb4;
JLabel lb5 = new JLabel("lb5");
lb5.setBounds(10, 155, 128, 128);
contentPane.add(lb5);
//lb5.setIcon(entericon(0));
lb[4] = lb5;
JLabel lb6 = new JLabel("lb6");
lb6.setBounds(148, 155, 128, 128);
contentPane.add(lb6);
//lb6.setIcon(entericon(0));
lb[5] = lb6;
JLabel lb7 = new JLabel("lb7");
lb7.setBounds(288, 155, 128, 128);
contentPane.add(lb7);
//lb7.setIcon(entericon(0));
lb[6] = lb7;
JLabel lb8 = new JLabel("lb8");
lb8.setBounds(426, 155, 128, 128);
contentPane.add(lb8);
//lb8.setIcon(entericon(0));
lb[7] = lb8;
JLabel lb9 = new JLabel("lb9");
lb9.setBounds(10, 293, 128, 128);
contentPane.add(lb9);
//lb9.setIcon(entericon(0));
lb[8] = lb9;
JLabel lb10 = new JLabel("lb10");
lb10.setBounds(148, 293, 128, 128);
contentPane.add(lb10);
//lb10.setIcon(entericon(0));
lb[9] = lb10;
JLabel lb11 = new JLabel("lb11");
lb11.setBounds(288, 293, 128, 128);
contentPane.add(lb11);
//lb11.setIcon(entericon(0));
lb[10] = lb11;
JLabel lb12 = new JLabel("lb12");
lb12.setBounds(426, 293, 128, 128);
contentPane.add(lb12);
//lb12.setIcon(entericon(0));
lb[11] = lb12;
JLabel lb13 = new JLabel("lb13");
lb13.setBounds(521, 431, 33, 33);
contentPane.add(lb13);
lb13.setIcon(entericon(7));
entericon_2(lb);
}
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(ImageIcon card[])
{
ImageIcon icon = new ImageIcon(Main.class.getResource(String.format("%d.%s", Math.random()*12,"png")));
Image image = icon.getImage().getScaledInstance(128, 128, java.awt.Image.SCALE_FAST);
ImageIcon newicon = new ImageIcon(image);
for(int i=0; i<card.length; i++)
{
}
}
}
複製代碼
作者:
蔡庭豪
時間:
2018-4-14 11:56
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 javax.swing.JLabel;
public class PtSC extends JFrame {
private JPanel contentPane;
private static ImageIcon icon;
private ImageIcon randomcard[] = new ImageIcon [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);
icon = new ImageIcon(PtSC.class.getResource("0.png"));
Image img = icon.getImage().getScaledInstance(128,128,java.awt.Image.SCALE_FAST );
ImageIcon andriod = new ImageIcon(img);
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(andriod);
Label_0.setBounds(10, 10, 128, 128);
contentPane.add(Label_0);
JLabel label_1 = new JLabel(andriod);
label_1.setBounds(148, 10, 128, 128);
contentPane.add(label_1);
JLabel label_2 = new JLabel(andriod);
label_2.setBounds(286, 10, 128, 128);
contentPane.add(label_2);
JLabel label_3 = new JLabel(andriod);
label_3.setBounds(424, 10, 128, 128);
contentPane.add(label_3);
JLabel label_4 = new JLabel(andriod);
label_4.setBounds(10, 149, 128, 128);
contentPane.add(label_4);
JLabel label_5 = new JLabel(andriod);
label_5.setBounds(148, 149, 128, 128);
contentPane.add(label_5);
JLabel label_6 = new JLabel(andriod);
label_6.setBounds(286, 149, 128, 128);
contentPane.add(label_6);
JLabel label_7 = new JLabel(andriod);
label_7.setBounds(424, 149, 128, 128);
contentPane.add(label_7);
JLabel label_8 = new JLabel(andriod);
label_8.setBounds(10, 286, 128, 128);
contentPane.add(label_8);
JLabel label_9 = new JLabel(andriod);
label_9.setBounds(148, 286, 128, 128);
contentPane.add(label_9);
JLabel label_10 = new JLabel(andriod);
label_10.setBounds(286, 286, 128, 128);
contentPane.add(label_10);
JLabel label_11 = new JLabel(andriod);
label_11.setBounds(424, 286, 128, 128);
contentPane.add(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);
randomcard = new ImageIcon[12];
}
private void randomputin (ImageIcon[] card){
}
}
複製代碼
作者:
陳泓瑜
時間:
2018-4-14 11:56
本帖最後由 陳泓瑜 於 2018-4-21 10:52 編輯
public class RANDOM
{
public static void main(String[] args)
{
int j[]=new int[12];
for(int i=0;i<12;i++)
{
j[i]=(int)(Math.random()*12);
for(int k=0; k<i; k++)
{
if(j[i]==j[k])
{
i--;
break;
}
}
}
for(int m=0; m<=11; m++)
{
System.out.println(j[m]);
}
}
}
複製代碼
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 javax.swing.JLabel;
public class FLIP extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
FLIP frame = new FLIP();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public FLIP() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 572, 466);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel C1 = new JLabel("");
C1.setIcon(new ImageIcon("C:\\Users\\student\\Downloads\\pic\\pic\\0.png"));
C1.setBounds(10, 10, 128, 128);
contentPane.add(C1);
JLabel C2 = new JLabel("");
C2.setIcon(new ImageIcon("C:\\Users\\student\\Downloads\\pic\\pic\\0.png"));
C2.setBounds(148, 10, 128, 128);
contentPane.add(C2);
JLabel C3 = new JLabel("");
C3.setIcon(new ImageIcon("C:\\Users\\student\\Downloads\\pic\\pic\\0.png"));
C3.setBounds(286, 10, 128, 128);
contentPane.add(C3);
JLabel C4 = new JLabel("");
C4.setIcon(new ImageIcon("C:\\Users\\student\\Downloads\\pic\\pic\\0.png"));
C4.setBounds(424, 10, 128, 128);
contentPane.add(C4);
//----------------------
JLabel C5 = new JLabel("");
C5.setIcon(new ImageIcon("C:\\Users\\student\\Downloads\\pic\\pic\\0.png"));
C5.setBounds(10, 148, 128, 128);
contentPane.add(C5);
JLabel C6 = new JLabel("");
C6.setIcon(new ImageIcon("C:\\Users\\student\\Downloads\\pic\\pic\\0.png"));
C6.setBounds(148, 148, 128, 128);
contentPane.add(C6);
JLabel C7 = new JLabel("");
C7.setIcon(new ImageIcon("C:\\Users\\student\\Downloads\\pic\\pic\\0.png"));
C7.setBounds(286, 148, 128, 128);
contentPane.add(C7);
JLabel C8 = new JLabel("");
C8.setIcon(new ImageIcon("C:\\Users\\student\\Downloads\\pic\\pic\\0.png"));
C8.setBounds(424, 148, 128, 128);
contentPane.add(C8);
//----------------------
JLabel C9 = new JLabel("");
C9.setIcon(new ImageIcon("C:\\Users\\student\\Downloads\\pic\\pic\\0.png"));
C9.setBounds(10, 286, 128, 128);
contentPane.add(C9);
JLabel C10 = new JLabel("");
C10.setIcon(new ImageIcon("C:\\Users\\student\\Downloads\\pic\\pic\\0.png"));
C10.setBounds(148, 286, 128, 128);
contentPane.add(C10);
JLabel C11 = new JLabel("");
C11.setIcon(new ImageIcon("C:\\Users\\student\\Downloads\\pic\\pic\\0.png"));
C11.setBounds(286, 286, 128, 128);
contentPane.add(C11);
JLabel C12 = new JLabel("");
C12.setIcon(new ImageIcon("C:\\Users\\student\\Downloads\\pic\\pic\\0.png"));
C12.setBounds(424, 286, 128, 128);
contentPane.add(C12);
}
}
複製代碼
作者:
林侑成
時間:
2018-4-14 12:04
package 翻牌;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.Toolkit;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.AbstractAction;
import java.awt.event.ActionEvent;
import javax.swing.Action;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class Asdf {
private JFrame frame;
private final Action action = new SwingAction();
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Asdf window = new Asdf();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Asdf() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setIconImage(Toolkit.getDefaultToolkit().getImage("C:\\Users\\student\\Desktop\\pic\\pic\\star.png"));
frame.setTitle("\u7FFB\u724C\u904A\u6232");
frame.setBounds(100, 100, 609, 504);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JLabel lblNewLabel = new JLabel("New label");
lblNewLabel.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
lblNewLabel.setIcon(new ImageIcon("C:\\Users\\student\\Desktop\\pic\\pic\\1.png"));
}
});
lblNewLabel.setIcon(new ImageIcon("C:\\Users\\student\\Desktop\\pic\\pic\\0.png"));
lblNewLabel.setBounds(10, 10, 128, 128);
frame.getContentPane().add(lblNewLabel);
JLabel label = new JLabel("New label");
label.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
label.setIcon(new ImageIcon("C:\\Users\\student\\Desktop\\pic\\pic\\2.png"));
}
});
label.setIcon(new ImageIcon("C:\\Users\\student\\Desktop\\pic\\pic\\0.png"));
label.setBounds(156, 10, 128, 128);
frame.getContentPane().add(label);
JLabel label_1 = new JLabel("New label");
label_1.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
label_1.setIcon(new ImageIcon("C:\\Users\\student\\Desktop\\pic\\pic\\3.png"));
}
});
label_1.setIcon(new ImageIcon("C:\\Users\\student\\Desktop\\pic\\pic\\0.png"));
label_1.setBounds(310, 10, 128, 128);
frame.getContentPane().add(label_1);
JLabel label_2 = new JLabel("New label");
label_2.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
label_2.setIcon(new ImageIcon("C:\\Users\\student\\Desktop\\pic\\pic\\4.png"));
}
});
label_2.setIcon(new ImageIcon("C:\\Users\\student\\Desktop\\pic\\pic\\0.png"));
label_2.setBounds(455, 10, 128, 128);
frame.getContentPane().add(label_2);
JLabel label_3 = new JLabel("New label");
label_3.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
label_3.setIcon(new ImageIcon("C:\\Users\\student\\Desktop\\pic\\pic\\5.png"));
}
});
label_3.setIcon(new ImageIcon("C:\\Users\\student\\Desktop\\pic\\pic\\0.png"));
label_3.setBounds(10, 149, 128, 128);
frame.getContentPane().add(label_3);
JLabel label_4 = new JLabel("New label");
label_4.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
label_4.setIcon(new ImageIcon("C:\\Users\\student\\Desktop\\pic\\pic\\6.png"));
}
});
label_4.setIcon(new ImageIcon("C:\\Users\\student\\Desktop\\pic\\pic\\0.png"));
label_4.setBounds(156, 148, 128, 128);
frame.getContentPane().add(label_4);
JLabel label_5 = new JLabel("New label");
label_5.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
label_5.setIcon(new ImageIcon("C:\\Users\\student\\Desktop\\pic\\pic\\6.png"));
}
});
label_5.setIcon(new ImageIcon("C:\\Users\\student\\Desktop\\pic\\pic\\0.png"));
label_5.setBounds(310, 148, 128, 128);
frame.getContentPane().add(label_5);
JLabel label_6 = new JLabel("New label");
label_6.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
label_6.setIcon(new ImageIcon("C:\\Users\\student\\Desktop\\pic\\pic\\5.png"));
}
});
label_6.setIcon(new ImageIcon("C:\\Users\\student\\Desktop\\pic\\pic\\0.png"));
label_6.setBounds(455, 149, 128, 128);
frame.getContentPane().add(label_6);
JLabel label_7 = new JLabel("New label");
label_7.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
label_7.setIcon(new ImageIcon("C:\\Users\\student\\Desktop\\pic\\pic\\4.png"));
}
});
label_7.setIcon(new ImageIcon("C:\\Users\\student\\Desktop\\pic\\pic\\0.png"));
label_7.setBounds(10, 287, 128, 128);
frame.getContentPane().add(label_7);
JLabel label_8 = new JLabel("New label");
label_8.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
label_8.setIcon(new ImageIcon("C:\\Users\\student\\Desktop\\pic\\pic\\3.png"));
}
});
label_8.setIcon(new ImageIcon("C:\\Users\\student\\Desktop\\pic\\pic\\0.png"));
label_8.setBounds(156, 287, 128, 128);
frame.getContentPane().add(label_8);
JLabel label_9 = new JLabel("New label");
label_9.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
label_9.setIcon(new ImageIcon("C:\\Users\\student\\Desktop\\pic\\pic\\2.png"));
}
});
label_9.setIcon(new ImageIcon("C:\\Users\\student\\Desktop\\pic\\pic\\0.png"));
label_9.setBounds(455, 287, 128, 128);
frame.getContentPane().add(label_9);
JLabel label_10 = new JLabel("New label");
label_10.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
label_10.setIcon(new ImageIcon("C:\\Users\\student\\Desktop\\pic\\pic\\1.png"));
}
});
label_10.setIcon(new ImageIcon("C:\\Users\\student\\Desktop\\pic\\pic\\0.png"));
label_10.setBounds(310, 286, 128, 128);
frame.getContentPane().add(label_10);
JButton btnNewButton = new JButton("New button");
btnNewButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
}
});
btnNewButton.setIcon(new ImageIcon("C:\\Users\\student\\Desktop\\pic\\pic\\return.png"));
btnNewButton.setBounds(550, 425, 33, 33);
frame.getContentPane().add(btnNewButton);
}
private class SwingAction extends AbstractAction {
public SwingAction() {
putValue(NAME, "SwingAction");
putValue(SHORT_DESCRIPTION, "Some short description");
}
public void actionPerformed(ActionEvent e) {
}
}
}
複製代碼
作者:
蔡季樺
時間:
2018-4-20 20:53
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class main {
private JFrame fm;
private JButton b[]=new JButton[12];
private JButton b_r;
private ImageIcon mm[]=new ImageIcon[12];
private ImageIcon ic,mm_r,mm_b;
private int rc[]=new int[12];
main()
{
rc();
ic=new ImageIcon(main.class.getResource("star.png"));
mm_b=new ImageIcon(main.class.getResource("0.png"));
mm_r=new ImageIcon(main.class.getResource("return.png"));
for(int i=0; i<6; i++)
{
mm[i]=new ImageIcon(main.class.getResource((i+1)+".png"));
mm[i+6]=new ImageIcon(main.class.getResource((i+1)+".png"));
}
for(int i=0; i<12; i++)
b[i]=new JButton(mm_b);
for(int i=0; i<4; i++)
b[ i ].setBounds(i*130+12, 10, 128, 128);
for(int i=0; i<4; i++)
b[ i+4 ].setBounds(i*130+12, 140, 128, 128);
for(int i=0; i<4; i++)
b[ i+8 ].setBounds(i*130+12, 270, 128, 128);
for(int i=0; i<12; i++)
b[i].addMouseListener(ma);
b_r=new JButton(mm_r);
b_r.setBounds(497, 407, 33, 33);
b_r.addActionListener(al);
fm=new JFrame("翻牌遊戲");
fm.setBounds(100, 100, 548, 480);
fm.setIconImage(ic.getImage());
fm.setVisible(true);
fm.setResizable(false);
fm.setLayout(null);
fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
for(int i=0; i<12; i++)
fm.add(b[i]);
fm.add(b_r);
}
void rc()
{
for(int i=0; i<12; i++)
{
rc[i]=(int)(Math.random()*12); //0~11
for(int j=0; j<i; j++)
{
if(rc[i]==rc[j])
{
i--;
break;
}
}
}
}
MouseAdapter ma=new MouseAdapter(){
public void mousePressed(MouseEvent e)
{
for(int i=0; i<12; i++)
{
if(e.getSource()==b[i])
b[i].setIcon(mm[rc[i]]);
}
}
};
ActionListener al=new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(e.getSource()==b_r)
{
rc();
for(int i=0; i<12; i++)
b[i].setIcon(mm_b);
}
}
};
public static void main(String[] args) {
new main();
}
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/)
Powered by Discuz! 7.2