闲着没事写着完呢
package com.yjl.daletou.util;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.*;
import java.util.Arrays;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class SwingIndexExample {
private static JTextField inputText, redText, blueText, kjText, tzText, resultsText;
private static JTextArea outputText;
public static void main(String[] args) {
// 创建 JFrame 实例
JFrame frame = new JFrame("体彩大乐透");
// Setting the width and height of frame
frame.setSize(540, 600);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
/* 创建面板,这个类似于 HTML 的 div 标签
* 我们可以创建多个面板并在 JFrame 中指定位置
* 面板中我们可以添加文本字段,按钮及其他组件。
*/
JPanel panel = new JPanel();
// 添加面板
frame.add(panel);
//调用用户定义的方法并添加组件到面板
placeComponents(panel);
// 设置界面可见
frame.setVisible(true);
}
private static void placeComponents(final JPanel panel) {
//布局部分我们这边不多做介绍。这边设置布局为 null
panel.setLayout(null);
//创建 JLabel
JLabel headerLabel = new JLabel("体彩大乐透机选投注");
headerLabel.setBounds(175,20,190,25);
headerLabel.setFont(new Font("楷体",Font.BOLD,20));
panel.add(headerLabel);
JLabel inputLabel = new JLabel("机选注数:");
inputLabel.setFont(new Font("楷体",Font.BOLD,16));
/* 这个方法定义了组件的位置。
* setBounds(x, y, width, height)
* x 和 y 指定左上角的新位置,由 width 和 height 指定新的大小。
*/
inputLabel.setBounds(50,70,90,25);
panel.add(inputLabel);
/*
* 创建文本框用于输入机选注数。
*/
inputText = new JTextField(20);
inputText.setFont(new Font("楷体",Font.BOLD,16));
inputText.setText("5");
inputText.setBounds(130,70,40,25);
panel.add(inputText);
/*
* 前区号码球数量
*/
JLabel redLabel = new JLabel("前:");
redLabel.setFont(new Font("楷体",Font.BOLD,16));
redLabel.setBounds(180,70,30,25);
panel.add(redLabel);
redText = new JTextField(20);
redText.setFont(new Font("楷体",Font.BOLD,16));
redText.setBounds(210,70,20,25);
redText.setText("5");
panel.add(redText);
/*
* 后区号码球数量
*/
JLabel blueLabel = new JLabel("后:");
blueLabel.setFont(new Font("楷体",Font.BOLD,16));
blueLabel.setBounds(230,70,30,25);
panel.add(blueLabel);
blueText = new JTextField(20);
blueText.setFont(new Font("楷体",Font.BOLD,16));
blueText.setBounds(260,70,20,25);
blueText.setText("2");
panel.add(blueText);
//机选后注数显示文本域
JLabel outputLabel = new JLabel("机选号码:");
outputLabel.setFont(new Font("楷体",Font.BOLD,16));
outputLabel.setBounds(50,100,90,25);
panel.add(outputLabel);
//注数显示文本域
outputText = new JTextArea(10,20);
outputText.setLineWrap(true); //设置文本域中的文本为自动换行
outputText.setForeground(Color.BLACK); //设置组件的背景色
outputText.setFont(new Font("楷体",Font.BOLD,14)); //修改字体样式
JScrollPane jsp = new JScrollPane(outputText); //将文本域放入滚动窗口
//Dimension size=outputText.getPreferredSize(); //获得文本域的首选大小
jsp.setBounds(130,100,330,200);
panel.add(jsp); //将JScrollPane添加到JPanel容器中
//开始按钮
final JButton actionButton = new JButton("机选");
actionButton.setFont(new Font("楷体",Font.BOLD,14)); //修改字体样式
actionButton.setBounds(290, 70, 80, 25);
panel.add(actionButton);
actionButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
if(inputText.getText().equals("")){
JOptionPane.showMessageDialog(panel, "请输入机选注数!", "提示",JOptionPane.WARNING_MESSAGE);
return;
}
if(redText.getText().equals("")){
JOptionPane.showMessageDialog(panel, "前区球数不可小于5!", "提示",JOptionPane.WARNING_MESSAGE);
return;
}
if(blueText.getText().equals("")){
JOptionPane.showMessageDialog(panel, "后区球数不可小于2!", "提示",JOptionPane.WARNING_MESSAGE);
return;
}
int n = Integer.parseInt(inputText.getText());
int r = Integer.parseInt(redText.getText());
int b = Integer.parseInt(blueText.getText());
if(n > 100){
JOptionPane.showMessageDialog(panel, "最大可机选100注!", "提示",JOptionPane.WARNING_MESSAGE);
}else if(n < 1){
JOptionPane.showMessageDialog(panel, "最少机选1注!", "提示",JOptionPane.WARNING_MESSAGE);
}else if(r < 5){
JOptionPane.showMessageDialog(panel, "前区球数不可小于5!", "提示",JOptionPane.WARNING_MESSAGE);
}else if(b < 2){
JOptionPane.showMessageDialog(panel, "后区球数不可小于2!", "提示",JOptionPane.WARNING_MESSAGE);
}else{
for (int i = 0; i < n; i++) {
daLeTou(r,b);
}
}
}
});
//重置按钮
final JButton resetButton = new JButton("重置");
resetButton.setFont(new Font("楷体",Font.BOLD,14)); //修改字体样式
resetButton.setBounds(380, 70, 80, 25);
panel.add(resetButton);
resetButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
inputText.setText("");
outputText.setText("");
}
});
//********************************************************************************
JLabel queryLabel = new JLabel("中奖查询");
queryLabel.setFont(new Font("楷体",Font.BOLD,20));
queryLabel.setBounds(225,310,90,25);
panel.add(queryLabel);
JLabel kjLabel = new JLabel("开奖号码:");
kjLabel.setFont(new Font("楷体",Font.BOLD,16));
kjLabel.setBounds(50,350,90,25);
panel.add(kjLabel);
kjText = new JTextField(20);
kjText.setFont(new Font("楷体",Font.BOLD,16));
kjText.setBounds(130,350,330,25);
panel.add(kjText);
JLabel sampleLabel = new JLabel("示例:01,02,03,04,05|01,02");//示例
sampleLabel.setFont(new Font("楷体",Font.BOLD,14));
sampleLabel.setBounds(130,370,250,20);
panel.add(sampleLabel);
JLabel tzLabel = new JLabel("投注号码:");
tzLabel.setFont(new Font("楷体",Font.BOLD,16));
tzLabel.setBounds(50,390,90,25);
panel.add(tzLabel);
tzText = new JTextField(20);
tzText.setFont(new Font("楷体",Font.BOLD,16));
tzText.setBounds(130,390,330,25);
panel.add(tzText);
//开奖重置按钮
final JButton kjResetButton = new JButton("开奖重置");
kjResetButton.setFont(new Font("楷体",Font.BOLD,14)); //修改字体样式
kjResetButton.setBounds(110, 420, 100, 25);
panel.add(kjResetButton);
kjResetButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
kjText.setText("");
}
});
//投注重置按钮
final JButton tzResetButton = new JButton("投注重置");
tzResetButton.setFont(new Font("楷体",Font.BOLD,14)); //修改字体样式
tzResetButton.setBounds(220, 420, 100, 25);
panel.add(tzResetButton);
tzResetButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
tzText.setText("");
}
});
//查询按钮
final JButton queryButton = new JButton("查询");
queryButton.setFont(new Font("楷体",Font.BOLD,14)); //修改字体样式
queryButton.setBounds(330, 420, 100, 25);
panel.add(queryButton);
queryButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
String kjBall = kjText.getText();
if(kjBall.equals("")){
JOptionPane.showMessageDialog(panel, "请输入开奖号码!", "提示",JOptionPane.WARNING_MESSAGE);
return;
}
kjBall.replaceAll(",", ",");
kjBall.replaceAll("|", "|");
String tzBall = tzText.getText();
if(tzBall.equals("")){
JOptionPane.showMessageDialog(panel, "请输入投注号码!", "提示",JOptionPane.WARNING_MESSAGE);
return;
}
tzBall.replaceAll(",", ",");
tzBall.replaceAll("|", "|");
queryCompare(kjBall, tzBall);
}
});
//结果
JLabel resultsLabel = new JLabel("中奖结果:");
resultsLabel.setFont(new Font("楷体",Font.BOLD,16));
resultsLabel.setBounds(50,460,90,25);
panel.add(resultsLabel);
resultsText = new JTextField(20);
resultsText.setFont(new Font("楷体",Font.BOLD,16));
resultsText.setBounds(130,460,330,25);
panel.add(resultsText);
//命中规则
String gz = "一等奖:5+2 " +
"二等奖:5+1 " +
"三等奖:5+0 " +
"四等奖:4+2
"+
"五等奖:4+1 " +
"六等奖:3+2 " +
"七等奖:4+0
" +
"八等奖:3+1,2+2 " +
"九等奖:3+0,2+1,1+2,0+2 ";
JLabel gzLabel = new JLabel("");
gzLabel.setFont(new Font("楷体",Font.BOLD,12));
gzLabel.setText("" + gz + "");
gzLabel.setBounds(110,490,320,50);
panel.add(gzLabel);
}
/**
* 大乐透业务
* 摇球
*/
public static String[] shake(String[] ball, int arrLength){
String random = "";
String[] rb;//红蓝球
rb = ball;
//摇球50次
for(int y = 0; y < 50; y++){
//System.out.println("摇前"+Arrays.toString(rb));
String[] rb1 = new String[arrLength];//临时红篮球
for (int i = 0; i < rb1.length; i++) {
int index = (int) (Math.random() * rb.length);
random = rb[index];
rb1[i] = random;
rb[index] = rb[rb.length-1];
//数组缩容
rb = Arrays.copyOf(rb, rb.length-1);
}
rb = new String[arrLength];
for (int i=0;i(int)arraysecond[i]){
return true;
}else if((int)arrayfirst[i] < (int)arraysecond[i]){
return false;
}
}
if(arrayfirst.length>arraysecond.length){
return true;
}else {
return false;
}
}
/**
* 结果查询比较中奖球数
*/
public static void queryCompare(String kjBall, String tzBall){
//开奖球
String kaijianghao = kjBall;
String[] kj = kaijianghao.split("\\|");
String k_red = kj[0];
String k_blue = kj[1];
//投注球
String touzhuhao = tzBall;
String[] tz = touzhuhao.split("\\|");
String t_red = tz[0];
String t_blue = tz[1];
String[] k_red1 = k_red.split("\\,");//前区开奖号码
String[] t_red1 = t_red.split("\\,");//前区投注号码
int r = 0;//前区中奖球数
for(int i = 0; i < k_red1.length; i ++){
for(int k = 0; k