📄 mainframe.java
字号:
package com.fanpai.ui.swing;
import java.awt.GridLayout;
import java.awt.SystemColor;
import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JLabel;
import javax.swing.UIManager;
import com.fanpai.data.DataBean;
import com.fanpai.logic.TCLogic;
import com.fanpai.logic.TCLogicFactory;
import java.awt.*;
public class MainFrame extends JFrame {
//逻辑变量
TCLogic logic = TCLogicFactory.getTCLogic();
//牌数种类
int numtypes = 8;
int pwidth = 100;
int pheight = 160;
//线程变量
static Thread ts = null;
static ValueClear vc = null;
static javax.swing.ImageIcon[] img=null;
static javax.swing.ImageIcon bj =null;
/** Creates a new instance of GetImage */
public MainFrame() {
try {
jbInit();
ButtonListener.mf = this;
//初始化线程
vc = new ValueClear();
this.ts = new Thread(vc);
//启动线程
this.ts.start();
} catch (Exception ex) {
ex.printStackTrace();
}
}
/**
* reset游戏
*/
void resetgame() {
//初始化数据
this.logic.init(numtypes);
//清空翻牌次数
ButtonListener.openedCount = 0;
//清空点击缓存
ButtonListener.data.reset();
//重置状态栏
this.statueArea.setText("共翻开:0次");
int btcount = this.jPanel1.getComponentCount();
//重新打乱顺序
if (btcount > 0) {
for (int i = 0; i < this.logic.length(); i++) {
JButton tjb = (JButton)this.jPanel1.getComponent(i);
tjb.setEnabled(true);
//清空显示
tjb.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/BJ.jpg")));
}
//提醒用户完成分牌
JOptionPane.showMessageDialog(this,"发牌完成.","确认开始游戏",JOptionPane.WARNING_MESSAGE);
}
//初次创建
else {
//循环产生按钮
for (int i = 0; i < this.logic.length(); i++) {
JButton tjb = new JButton();
tjb.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/BJ.jpg")));
tjb.putClientProperty("index",new Integer(i));
//添加动作
tjb.addActionListener(new ButtonListener(this.logic));
this.jPanel1.add(tjb);
}
}
}
private void jbInit() throws Exception {
this.setTitle("扑克牌");
this.gridLayout1.setColumns(this.numtypes);
this.gridLayout1.setRows(2);
this.getContentPane().setLayout(borderLayout1);
jPanel1.setLayout(gridLayout1);
this.getContentPane().setBackground(SystemColor.control);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
jPanel2.setLayout(borderLayout2);
statueArea.setText("共翻开:0次");
jButton1.setText("重来");
jButton1.addActionListener(new MainFrame_jButton1_actionAdapter(this));
borderLayout1.setHgap(0);
borderLayout1.setVgap(0);
this.getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);
this.getContentPane().add(jPanel2, java.awt.BorderLayout.SOUTH);
jPanel2.add(statueArea, java.awt.BorderLayout.CENTER);
jPanel2.add(jButton2, java.awt.BorderLayout.EAST);
jPanel2.add(jButton1, java.awt.BorderLayout.WEST);
this.resetgame();
this.setSize(this.numtypes * this.pwidth, 2 * this.pheight);
}
BorderLayout borderLayout1 = new BorderLayout();
JPanel jPanel1 = new JPanel();
GridLayout gridLayout1 = new GridLayout();
JPanel jPanel2 = new JPanel();
JLabel statueArea = new JLabel();
BorderLayout borderLayout2 = new BorderLayout();
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
public void jButton1_actionPerformed(ActionEvent actionEvent) {
this.resetgame();
}
}
class MainFrame_jButton1_actionAdapter implements ActionListener {
private MainFrame adaptee;
MainFrame_jButton1_actionAdapter(MainFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent actionEvent) {
adaptee.jButton1_actionPerformed(actionEvent);
}
}
class ButtonListener implements ActionListener {
//逻辑变量
static TCLogic logic = null;
static DataBean data = new DataBean();
static MainFrame mf = null;
//翻开的次数
static int openedCount = 0;
public ButtonListener(TCLogic inputlogic) {
if (logic == null)
logic = inputlogic;
}
public void actionPerformed(ActionEvent e) {
JButton tjb = (JButton) e.getSource();
getImage img = new getImage();
JPanel jp = (JPanel) tjb.getParent();
int index = ((Integer) tjb.getClientProperty("index")).intValue();
int value = this.logic.get(index);
//当没有点击过时
if (this.data.getFirst() == -1 && this.data.getSecond() == -1) {
tjb.setIcon(img.img[value]);
tjb.repaint();
this.data.setFirst(index);
return;
}
//当点击了一次之后
if (this.data.getFirst() != -1 && this.data.getSecond() == -1) {
//如果重复点击上次按钮
if (this.data.getFirst() == index) {
return;
}
//填充数据
this.data.setSecond(index);
//取得已经点击过的按钮
JButton tjb1 = (JButton) jp.getComponent(this.data.getFirst());
//判断是否一致
boolean hit = this.logic.judge(data);
//如果命中了
if (hit) {
//设置按钮
tjb.setIcon(img.img[value]);
tjb.repaint();
//点过按钮不能再点击
tjb.setEnabled(false);
tjb1.setEnabled(false);
//判断是否完成游戏
if (this.logic.finish()) {
int tint = JOptionPane.showConfirmDialog(jp,
"恭喜,在第" + (++openedCount) + "次猜对了。\n重新游戏?", "确定",
JOptionPane.YES_OPTION);
//选择ok时重启游戏
if (tint == JOptionPane.YES_OPTION) {
mf.resetgame();
return;
}
}
}
//如果没有命中
else {
// tjb.setText(value + "");
//设置线程数据
tjb.setIcon(img.img[value]);
this.mf.vc.setData(tjb,tjb1);
}
openedCount++;
mf.statueArea.setText("共翻开:" + openedCount + "次");
this.data.reset();
}
}
}
class ValueClear implements Runnable {
private JButton jb1 = null;
private JButton jb2 = null;
private Thread ts = null;
public ValueClear(){
this.ts = Thread.currentThread();
}
public void setData(JButton input1, JButton input2) {
this.jb1 = input1;
this.jb2 = input2;
}
public void run() {
getImage data = new getImage();
while (true) {
try {
if (this.jb1 != null && this.jb2 != null) {
this.ts.sleep(200);
this.jb1.setIcon(data.aa);
this.jb2.setIcon(data.aa);
this.jb1 = null;
this.jb2 = null;
this.ts.yield();
continue;
}
else{
this.ts.sleep(200);
}
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -