📄 duiduipeng.java
字号:
import java.applet.AudioClip;
import java.awt.*;
import java.awt.event.*;
import java.net.URL;
import java.util.Random;
import javax.swing.*;
public class duiduipeng extends JFrame {
/////////////////////图形界面控件定义///////////////////////////
JPanel contentPane;
BorderLayout borderLayout1 = new BorderLayout();
JPanel jPanel1 = new JPanel();
JPanel jPanel2 = new JPanel();
GridLayout gridLayout1 = new GridLayout();
JMenuBar jMenuBar1 = new JMenuBar();
JMenu jMenuFile = new JMenu();
JMenuItem jMenuItem1 = new JMenuItem(); //开始游戏
JMenuItem jMenuItem2 = new JMenuItem(); //洗牌
JMenuItem jMenuItem3 = new JMenuItem(); //退出游戏
JButton jButton50 = new JButton(); //开始游戏
JButton jButton51 = new JButton(); //洗牌
JLabel jLabel1 = new JLabel(); //时间
JTextField jTextField1 = new JTextField(); //显示时间
JLabel jLabel2 = new JLabel(); //得分
JTextField jTextField2 = new JTextField(); //显示得分
JButton jButton[][] = new JButton[7][];
ImageIcon icon[] = new ImageIcon[7];
///////////////////////////其它变量定义////////////////////////////////
Doblat doblat = new Doblat(); //声效
Random r = new Random(); //随机生成数(用于初始化界面)
int fristx = -1, fristy = -1, secondx = -1, secondy = -1;
boolean frist = false, second = false;
int del[][] = new int[7][7];
int delnumber = 0;
int mark = 1000;
int i = 0; //用于指定线程
TimeThread timethread[] = new TimeThread[1000];
SoundThread soundthread[] = new SoundThread[1000];
public duiduipeng() {
try {
setUndecorated(false); //去掉窗体修饰,包括最大化按钮
setResizable(false); //禁止改变窗体大
setDefaultCloseOperation(EXIT_ON_CLOSE);
jbInit(); //界面初始化及监听
} catch (Exception exception) {
exception.printStackTrace();
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
UIManager.setLookAndFeel(UIManager.
getSystemLookAndFeelClassName());
} catch (Exception exception) {
exception.printStackTrace();
}
duiduipeng frame = new duiduipeng();
Dimension screenSize = Toolkit.getDefaultToolkit().
getScreenSize();
Dimension frameSize = frame.getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
frame.setLocation((screenSize.width - frameSize.width) / 2,
(screenSize.height - frameSize.height) / 2);
frame.setVisible(true);
}
});
}
private void jbInit() throws Exception {
///////////////////////////////图形界面设计////////////////////////////
contentPane = (JPanel) getContentPane();
contentPane.setLayout(borderLayout1);
setTitle("对对碰");
setSize(new Dimension(380, 440));
contentPane.add(jPanel2, java.awt.BorderLayout.CENTER);
contentPane.add(jPanel1, java.awt.BorderLayout.NORTH);
jMenuFile.setText("File");
jMenuItem1.setText("开始新游戏");
jMenuItem2.setText("洗牌");
jMenuItem3.setText("退出");
jMenuItem1.addActionListener(new duiduipeng_jButton50_actionAdapter(this));
jMenuItem2.addActionListener(new duiduipeng_jButton51_actionAdapter(this));
jMenuItem3.addActionListener(new
duiduipeng_jMenuFileExit_ActionAdapter(this));
jButton50.setText("开始游戏");
jButton50.addActionListener(new duiduipeng_jButton50_actionAdapter(this));
jButton51.setText("重新洗牌");
jButton51.addActionListener(new duiduipeng_jButton51_actionAdapter(this));
jLabel1.setText("得分:");
jTextField1.setEditable(false);
jTextField1.setText(String.valueOf(mark));
jLabel2.setText("时间");
jTextField2.setEditable(false);
jTextField2.setText("未开始");
icon[0] = new ImageIcon("images/1.gif");
icon[1] = new ImageIcon("images/2.gif");
icon[2] = new ImageIcon("images/3.gif");
icon[3] = new ImageIcon("images/4.gif");
icon[4] = new ImageIcon("images/5.gif");
icon[5] = new ImageIcon("images/6.gif");
icon[6] = new ImageIcon("images/7.gif");
for (int i = 0; i < 7; i++) {
jButton[i] = new JButton[] {new JButton(), new JButton(),
new JButton(), new JButton(), new JButton(),
new JButton(), new JButton()};
}
jPanel2.setLayout(gridLayout1);
gridLayout1.setColumns(7);
gridLayout1.setRows(7);
jMenuBar1.add(jMenuFile);
jMenuFile.add(jMenuItem1);
jMenuFile.add(jMenuItem2);
jMenuFile.addSeparator();
jMenuFile.add(jMenuItem3);
jPanel1.add(jLabel2);
jPanel1.add(jTextField2);
jPanel1.add(jButton50);
jPanel1.add(jButton51);
jPanel1.add(jLabel1);
jPanel1.add(jTextField1);
setJMenuBar(jMenuBar1);
for (int i = 0; i < 7; i++) {
for (int j = 0; j < 7; j++) {
jPanel2.add(jButton[i][j]);
}
}
//////////////////////变量初始化/////////////////////////////////////
jMenuItem2.setEnabled(false);
jButton51.setEnabled(false);
for (int i = 0; i < 7; i++) {
for (int j = 0; j < 7; j++) {
del[i][j] = 1;
jButton[i][j].setIcon(null);
jButton[i][j].setEnabled(false);
}
}
//////////////////////监听/////////////////////////////////////////
for (int i = 0; i < 7; i++) {
for (int j = 0; j < 7; j++) {
jButton[i][j].addMouseListener
(new duiduipeng_MouseAdapter(i, j, this));
}
}
}
void jMenuFileExit_actionPerformed(ActionEvent actionEvent) {
System.exit(0);
}
public void start() { //开始新游戏
jButton51.setEnabled(true);
jMenuItem2.setEnabled(true);
for (int i = 0; i < 7; i++) {
for (int j = 0; j < 7; j++) {
jButton[i][j].setEnabled(true);
}
}
timethread[i] = new TimeThread(this);
soundthread[i] = new SoundThread();
if (i > 0) {
if (timethread[i - 1].restart == false) {
timethread[i - 1].stopthread();
soundthread[i - 1].stopthread();
}
JOptionPane.showMessageDialog(null, "现在重新开始游戏...", "准备",
JOptionPane.PLAIN_MESSAGE);
}
for (int i = 0; i < 7; i++) {
for (int j = 0; j < 7; j++) {
jButton[i][j].setIcon(icon[r.nextInt(7)]);
del[i][j] = 1;
}
} while (waitdel()) {
delete();
}
mark = 1000;
jTextField1.setText(String.valueOf(mark));
timethread[i].start(); //添加时间线程
soundthread[i].start(); //添加音乐线程
i++;
}
public void shuffle() { //游戏中可以重新洗牌,但要减去一定的分数
int m = mark;
for (int i = 0; i < 7; i++) {
for (int j = 0; j < 7; j++) {
jButton[i][j].setIcon(icon[r.nextInt(7)]);
del[i][j] = 1;
}
} while (waitdel()) {
delete();
}
mark = m - 50;
jTextField1.setText(String.valueOf(mark));
}
public void clickButton(int a, int b) {
if (frist) {
if (fristy == 0) {
if (fristx == 0) { //左上角
if ((a == 0 && b == 1) || (a == 1 && b == 0)) {
secondx = a;
secondy = b;
second = true;
} else {
jButton[fristx][fristy].setBackground(Color.white);
fristx = a;
fristy = b;
jButton[a][b].setBackground(Color.red);
}
} else if (fristx == 6) { //左下角
if ((a == 5 && b == 0) || (a == 6 && b == 1)) {
secondx = a;
secondy = b;
second = true;
} else {
jButton[fristx][fristy].setBackground(Color.white);
fristx = a;
fristy = b;
jButton[a][b].setBackground(Color.red);
}
} else {
if ((a == fristx - 1 && b == 0) ||
(a == fristx && b == 1) ||
(a == fristx + 1 && b == 0)) {
secondx = a;
secondy = b;
second = true;
} else {
jButton[fristx][fristy].setBackground(Color.white);
fristx = a;
fristy = b;
jButton[a][b].setBackground(Color.red);
}
}
} else if (fristy == 6) {
if (fristx == 0) { //右上角
if ((a == 0 && b == 5) || (a == 1 && b == 6)) {
secondx = a;
secondy = b;
second = true;
} else {
jButton[fristx][fristy].setBackground(Color.white);
fristx = a;
fristy = b;
jButton[a][b].setBackground(Color.red);
}
} else if (fristx == 6) { //右下角
if ((a == 6 && b == 5) || (a == 5 && b == 6)) {
secondx = a;
secondy = b;
second = true;
} else {
jButton[fristx][fristy].setBackground(Color.white);
fristx = a;
fristy = b;
jButton[a][b].setBackground(Color.red);
}
} else {
if ((a == fristx - 1 && b == 6) ||
(a == fristx && b == 5) ||
(a == fristx + 1 && b == 6)) {
secondx = a;
secondy = b;
second = true;
} else {
jButton[fristx][fristy].setBackground(Color.white);
fristx = a;
fristy = b;
jButton[a][b].setBackground(Color.red);
}
}
} else {
if (fristx == 0) { //最上层
if ((a == 0 && b == fristy - 1) ||
(a == 1 && b == fristy) ||
(a == 0 && b == fristy + 1)) {
secondx = a;
secondy = b;
second = true;
} else {
jButton[fristx][fristy].setBackground(Color.white);
fristx = a;
fristy = b;
jButton[a][b].setBackground(Color.red);
}
} else if (fristx == 6) { //最下层
if ((a == 6 && b == fristy - 1) ||
(a == 5 && b == fristy) ||
(a == 6 && b == fristy + 1)) {
secondx = a;
secondy = b;
second = true;
} else {
jButton[fristx][fristy].setBackground(Color.white);
fristx = a;
fristy = b;
jButton[a][b].setBackground(Color.red);
}
} else {
if ((a == fristx - 1 && b == fristy) ||
(a == fristx + 1 && b == fristy)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -