标签:
也比较简单,写的不好,代码里用到了LOOKANDFELL,QUAQUA8.0的包
package com.Game24; import java.awt.Container; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.GridLayout; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JPopupMenu; import javax.swing.JRootPane; import javax.swing.JTextArea; import javax.swing.JTextField; import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException; import ch.randelshofer.quaqua.jaguar.Quaqua15JaguarLookAndFeel; /* ############################################################ # # # 【名称】 : 游戏-24点 # # 【作者】 : Sevck(一个写代码很帅的男人) # # 【团队】 : 网络尖刀 # # 【主页】 : http://sevck.lofter.com # # 【日期】 : 2015-10-17 # # 【功能】 : 24点,并且按钮不可以重复,并且结果不可以编辑 # # # ############################################################ # ┏┓ ┏┓ #┏┛┻━━━┛┻┓ #┃ ┃ #┃ ━ ┃ #┃ ┳┛ ┗┳ ┃ #┃ ┃ #┃ ``` ┻ ```┃ #┃ ┃ #┗━┓ ┏━┛ #####┃ ┃Code is far away from bug with the animal protecting. #####┃ ┃神兽护佑,代码无Bug. #####┃ ┗━━━━━┓ #####┃ ┣┓ #####┃ ┏┛ #####┗┓┓┏━┳┓┏┛ #######┃┫┫ ┃┫┫ #######┗┻┛ ┗┻┛ ############################################################ */ public class Game24 extends JFrame { static JButton b[] = null;// 随机数按钮 JButton law[] = new JButton[] { new JButton("+"), new JButton("-"), new JButton("*"), new JButton("/") };// 四则运算按钮 JLabel label = new JLabel("结果:"); JTextArea text = new JTextArea(1, 15); JButton of[] = new JButton[] { new JButton("确定"), new JButton("刷新") }; static int mark = 0; JLabel point = new JLabel("当前分数:" + mark); JPanel jp = new JPanel(); JPanel jp2 = new JPanel(); JPanel jp3 = new JPanel(); JPanel jp4 = new JPanel(); JPanel jp5 = new JPanel(); String reg = "[0-9]{1,2}.[0-9]{1,2}.[0-9]{1,2}.[0-9]{1,2}"; // 11 + 4 * 6 - 2 char c[] = new char[3]; public static String getNumber() {// 随机数方法 int i = (int) (Math.random() * 13 + 1); String number = String.valueOf(i); return number; } // public static void setbname(){ // n1=Game24.getNumber(); // } public Game24() { setUndecorated(true); setDefaultCloseOperation(3); setFocusableWindowState(true); JRootPane rp = getRootPane(); setVisible(true); rp.setWindowDecorationStyle(JRootPane.FRAME); // System.out.println(Game24.getNumber()); b = new JButton[] { new JButton(Game24.getNumber()), new JButton(Game24.getNumber()), new JButton(Game24.getNumber()), new JButton(Game24.getNumber()) }; init(); } public void init() { this.setTitle("24点--By:Sevck"); this.setVisible(true);// 是否可视 this.setDefaultCloseOperation(3);// 关闭窗口模式 this.setSize(700, 330);// 设置窗口尺寸 this.setResizable(false);// 设置是否可以修改面板大小 this.setLocationRelativeTo(null); this.setLayout(new GridLayout(5, 5, 5, 5));// 设置布局为流式布局 this.setLocationRelativeTo(null); for (int i = 0; i < b.length; i++) { jp.add(b[i]); } for (int i = 0; i < law.length; i++) { jp2.add(law[i]); } Container cp = this.getContentPane(); cp.add(jp); cp.add(jp2); text.setEditable(false); jp3.add(label); jp3.add(text); cp.add(jp3); jp4.add(of[0]); jp4.add(of[1]); cp.add(jp4); jp5.add(point); cp.add(jp5); b[0].addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub b[0].setEnabled(false); String str = b[0].getText(); text.append(str); } }); b[1].addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub b[1].setEnabled(false); String str = b[1].getText(); text.append(str); } }); b[2].addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub b[2].setEnabled(false); String str = b[2].getText(); text.append(str); } }); b[3].addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub b[3].setEnabled(false); String str = b[3].getText(); text.append(str); } }); law[0].addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub String str = law[0].getText(); text.append(str); } }); law[1].addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub String str = law[1].getText(); text.append(str); } }); law[2].addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub String str = law[2].getText(); text.append(str); } }); law[3].addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub String str = law[3].getText(); text.append(str); } }); of[0].addActionListener(new ActionListener() { // /////// int dian; @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub String str = text.getText(); boolean z = str.matches(reg);// 符合运算规则 if (z) { String rule = text.getText(); // System.out.println(rule); String sp = "[^0-9]"; String num[] = rule.split(sp); // 得到4个数字 // for (int i = 0; i < num.length; i++) { // System.out.println(num[i]); // } // ///////////////////////////// String faze = "[0-9]+"; String[] aa = rule.split(faze); // for (int j = 0; j < aa.length; j++) {// 得到3个运算符 // System.out.println(aa[j]); // } // ///////////////////////////// int num1 = Integer.parseInt(num[0]); int num2 = Integer.parseInt(num[1]); int num3 = Integer.parseInt(num[2]); int num4 = Integer.parseInt(num[3]); int d1 = getjg(aa[1], num1, num2); int d2 = getjg(aa[2], d1, num3); int d3 = getjg(aa[3], d2, num4); if (d3 == 24) { JOptionPane.showMessageDialog(null, "恭喜,进入下一关!"); mark = mark + 5; point.setText("当前分数:" + mark); b[0].setText(Game24.getNumber());// 刷新 b[1].setText(Game24.getNumber()); b[2].setText(Game24.getNumber()); b[3].setText(Game24.getNumber()); for (int i = 0; i <= 3; i++) { b[i].setEnabled(true); text.setText(""); } } else { JOptionPane.showMessageDialog(null, "不对呀,再想想~"); b[0].setEnabled(true); b[1].setEnabled(true); b[2].setEnabled(true); b[3].setEnabled(true); } } else { JOptionPane.showMessageDialog(null, "错误的运算步骤!"); } } public int getjg(String fh, int s1, int s2) { if (fh.equalsIgnoreCase("+")) { dian = s1 + s2; } if (fh.equalsIgnoreCase("-")) { dian = s1 - s2; } if (fh.equalsIgnoreCase("*")) { dian = s1 * s2; } if (fh.equalsIgnoreCase("/")) { dian = s1 / s2; } return dian; } }); of[1].addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub // Game24.setbname(); b[0].setText(Game24.getNumber()); b[1].setText(Game24.getNumber()); b[2].setText(Game24.getNumber()); b[3].setText(Game24.getNumber()); for (int i = 0; i <= 3; i++) { b[i].setEnabled(true); text.setText(""); } } }); } public static void main(String[] args) { System.setProperty("Quaqua.tabLayoutPolicy", "wrap"); try { UIManager.setLookAndFeel(ch.randelshofer.quaqua.QuaquaManager .getLookAndFeel()); } catch (UnsupportedLookAndFeelException e) { // TODO Auto-generated catch block } new Game24(); } }
标签:
原文地址:http://www.cnblogs.com/sevck/p/4895589.html