分享一次小成果

心情 2017-05-10

最近呢,在写java程序,天天写,没什么时间来发布文章,今天给大家分享一款用java写的小游戏!!

务必安装java环境才能正常运行该游戏!!

代码如下:

package game;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.ImageIcon;
import javax.swing.JTextField;
import java.awt.Font;
import java.awt.Color;
import javax.swing.JRadioButton;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.SwingConstants;

public class Game extends JFrame {

private JPanel contentPane;
private JTextField text_moneg;
private JTextField text_xiazhu;
private JLabel lblNewLabel;

// private String str="大小";

private int t=0;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Game frame = new Game();
                frame.setVisible(true);
                frame.setLocationRelativeTo(null);
                frame.setTitle("赌博小游戏");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public Game() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 642, 495);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);
    
    lblNewLabel = new JLabel("");
    lblNewLabel.setIcon(new ImageIcon(Game.class.getResource("/game/m.gif")));
    lblNewLabel.setBounds(22, 63, 289, 349);
    contentPane.add(lblNewLabel);
    
    JLabel nowmoney = new JLabel("\u73B0\u5728\u6709\u91D1\u989D\uFF1A");
    nowmoney.setBounds(373, 63, 90, 15);
    contentPane.add(nowmoney);
    
    text_moneg = new JTextField();
    text_moneg.setText("1000");
    text_moneg.setForeground(Color.RED);
    text_moneg.setEditable(false);
    text_moneg.setBounds(447, 60, 66, 21);
    contentPane.add(text_moneg);
    text_moneg.setColumns(10);
    
    JLabel label = new JLabel("\u5143");
    label.setBounds(523, 63, 54, 15);
    contentPane.add(label);
    
    JLabel label_1 = new JLabel("\u5C0F          \u6E38          \u620F");
    label_1.setForeground(Color.RED);
    label_1.setFont(new Font("宋体", Font.PLAIN, 21));
    label_1.setBounds(174, 10, 308, 32);
    contentPane.add(label_1);
    
    JLabel label_2 = new JLabel("\u8BF7\u538B\u5927\u5C0F\uFF1A");
    label_2.setBounds(373, 113, 72, 21);
    contentPane.add(label_2);
    
    JRadioButton radioda = new JRadioButton("\u5927");
    radioda.setBounds(447, 112, 43, 23);
    contentPane.add(radioda);
    
    JRadioButton radioxiao = new JRadioButton("\u5C0F");
    radioxiao.setBounds(511, 112, 54, 23);
    contentPane.add(radioxiao);
    
    JLabel label_3 = new JLabel("\u8BF7\u4E0B\u6CE8\uFF1A");
    label_3.setBounds(373, 169, 54, 21);
    contentPane.add(label_3);
    
    text_xiazhu = new JTextField();
    text_xiazhu.setText("0");
    text_xiazhu.setBounds(447, 169, 66, 21);
    contentPane.add(text_xiazhu);
    text_xiazhu.setColumns(10);
    
    JButton kaishi = new JButton("\u5F00\u59CB");
    kaishi.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            int moeng=Integer.parseInt(text_moneg.getText());
             if(moeng<=0){
                    JOptionPane.showConfirmDialog(null, "你没钱了,请点重进!");
                }
            else if(Integer.parseInt(text_xiazhu.getText())>moeng ||Integer.parseInt(text_xiazhu.getText())<=0){
                JOptionPane.showConfirmDialog(null,"请你正确下注!");
            }else if(radioda.isSelected()==false && radioxiao.isSelected()==false){
                JOptionPane.showConfirmDialog(null, "请压大小!");
            }else{
                int i=(int)(Math.random()*6+1);
                t=(int)(Math.random()*6+1);
                lblNewLabel.setIcon(new ImageIcon(Game.class.getResource("/game/"+i+".jpg")));
                if(i<=3){
                    if(i>=t){
                        JOptionPane.showConfirmDialog(null, "你"+i+"点大,系统"+t+"点"+",你赢了");
                        text_moneg.setText((Integer.parseInt(text_moneg.getText())+Integer.parseInt(text_xiazhu.getText())+""));
                        radioda.setSelected(false);    
                        radioxiao.setSelected(false);
                        text_xiazhu.setText("0");
                        kaishi.setEnabled(false);
                    }else{
                        JOptionPane.showConfirmDialog(null, "你"+i+"点小,系统"+t+"点"+",你输了");
                        text_moneg.setText((Integer.parseInt(text_moneg.getText())-Integer.parseInt(text_xiazhu.getText())+""));
                        radioda.setSelected(false);    
                        radioxiao.setSelected(false);
                        text_xiazhu.setText("0");
                        kaishi.setEnabled(false);
                    }
                }else if(i>=4){
                    if(i>=t){
                        JOptionPane.showConfirmDialog(null, "你"+i+"点大,系统"+t+"点"+",你赢了");
                        text_moneg.setText((Integer.parseInt(text_moneg.getText())+Integer.parseInt(text_xiazhu.getText())+""));
                        radioda.setSelected(false);    
                        radioxiao.setSelected(false);
                        text_xiazhu.setText("0");
                        kaishi.setEnabled(false);
                    }else{
                        JOptionPane.showConfirmDialog(null, "你"+i+"点小,系统"+t+"点"+",你输了");
                        text_moneg.setText((Integer.parseInt(text_moneg.getText())-Integer.parseInt(text_xiazhu.getText())+""));
                        radioda.setSelected(false);    
                        radioxiao.setSelected(false);
                        text_xiazhu.setText("0");
                        kaishi.setEnabled(false);
                    }
                }
            }
        }
    });
    kaishi.setBounds(370, 229, 93, 23);
    contentPane.add(kaishi);
    
    JButton suoha = new JButton("\u68AD\u54C8");
    suoha.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            text_xiazhu.setText(text_moneg.getText());
        }
    });
    suoha.setBounds(472, 229, 93, 23);
    contentPane.add(suoha);
    
    JButton zailai = new JButton("\u518D\u6765");
    zailai.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if(text_moneg.getText()=="0"){
                JOptionPane.showConfirmDialog(null, "你没钱了,点击重进");
            }else{
                lblNewLabel.setIcon(new ImageIcon(Game.class.getResource("/game/m.gif")));
                kaishi.setEnabled(true);
            }
        }
    });
    zailai.setBounds(373, 276, 93, 23);
    contentPane.add(zailai);
    
    JButton chongjin = new JButton("\u91CD\u8FDB");
    chongjin.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if(Integer.parseInt(text_moneg.getText())>0){
                JOptionPane.showConfirmDialog(null, "你当前还有钱未使用完");
            }else{
            text_moneg.setText("1000");
            radioda.setSelected(false);    
            radioxiao.setSelected(false);
            text_xiazhu.setText(null);
            }
        }
    });
    chongjin.setBounds(472, 276, 93, 23);
    contentPane.add(chongjin);
    
    JButton tuichu = new JButton("\u9000\u51FA");
    tuichu.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.exit(0);
        }
    });
    tuichu.setBounds(373, 328, 192, 23);
    contentPane.add(tuichu);
}

}

游戏界面:

1.png

下载地址:http://7xil9l.com1.z0.glb.clouddn.com/%E5%B0%8F%E6%B8%B8%E6%88%8F.jar


本文由 小 鹏 创作,采用 知识共享署名 3.0,可自由转载、引用,但需署名作者且注明文章出处。

还不快抢沙发

添加新评论