📄 lottery.java
字号:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.sql.*;
//*********************************************************************
//* *
//* 彩票35选7具体实现 *
//* *
//*********************************************************************
class Lottery extends JFrame
{
Container c=getContentPane();
JLabel alb1=new JLabel("您选出的第1组号码为");
JLabel alb2=new JLabel("您选出的第2组号码为");
JLabel alb3=new JLabel("您选出的第3组号码为");
JLabel alb4=new JLabel("您选出的第4组号码为");
JLabel alb5=new JLabel("您选出的第5组号码为");
JLabel alb6=new JLabel(" 本期大奖号码为: ");
JTextField atf1=new JTextField(13);
JTextField atf2=new JTextField(13);
JTextField atf3=new JTextField(13);
JTextField atf4=new JTextField(13);
JTextField atf5=new JTextField(13);
JTextField atf6=new JTextField(13);
JLabel blb1=new JLabel("请选择要买的注数");
Choice bc=new Choice();
JButton bb1=new JButton("生成");
JButton bb2=new JButton("保存");
JButton bb3=new JButton("摇奖");
JButton bb4=new JButton("开奖");
JButton bb5=new JButton("退出");
JButton bb6=new JButton("保存DB");
String pp[];
//用来接收统计返回随机7个数的字符串
String ss=new String("");
//定义开奖号码的字符串
FileDialog save=new FileDialog(this,"保存文件话框",FileDialog.SAVE);
//save文件框
//---------------------------------------------------------------------------
public Lottery() //构造
{
super("彩票系统");
setLocation(250,150);
c.add(new Apanel(), BorderLayout.CENTER);
//A面板设计在中间
c.add(new Bpanel(), BorderLayout.EAST);
//B面板设计在右边
try
{
//UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
//获得当前系统外观
//UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
//获得windows外观
UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
//moti外观
//UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
//金属外观
SwingUtilities.updateComponentTreeUI(this);
//上传数据外观(型参为窗体)
}catch(Exception e8){System.out.print(e8);}
pack();
//让系统自定义窗体的大小
setVisible(true);
//显示主窗体
save.setVisible(false);
//让save文件框隐藏
}
//-----------------------------------------------------------------------------
class Apanel extends JPanel //A面板的内部类
{
public Apanel()
{
alb6.setBackground(Color.red); //设计背景颜色
alb6.setForeground(Color.yellow); //设计前景颜色
alb6.setOpaque(true);
//设计透明度(如果你想背景和前景都有颜色就必须设置它)
alb6.setFont(new Font("SansSerif", Font.BOLD+ Font.ITALIC, 15));
//设置字体和大小
atf6.setForeground(Color.yellow);
atf6.setBackground(Color.red);
//设计与开奖按钮相关组件的前景颜色和背景颜色以及字体和可否编辑
atf1.setEditable(false);
atf2.setEditable(false);
atf3.setEditable(false);
atf4.setEditable(false);
atf5.setEditable(false);
atf6.setEditable(false);
//让所有的文本不可编辑
setLayout(new GridLayout(6,2));
add(alb1);
add(atf1);
add(alb2);
add(atf2);
add(alb3);
add(atf3);
add(alb4);
add(atf4);
add(alb5);
add(atf5);
add(alb6);
add(atf6);
setBorder(BorderFactory.createTitledBorder("【A】面板"));
//为面板加上边框
}
}
//-----------------------------------------------------------------------------------------
class Bpanel extends JPanel implements ActionListener //B面板的内部类,实现监听接口
{
public Bpanel()
{
blb1.setFont(new Font("SansSerif", Font.BOLD, 15));
//设计标签的字体,大小
blb1.setForeground(Color.red);
bb1.setForeground(Color.blue);
bb2.setForeground(Color.red);
bb3.setForeground(Color.yellow);
bb3.setBackground(Color.red);
bb4.setForeground(Color.yellow);
bb4.setBackground(Color.red);
bb5.setForeground(Color.white);
bb6.setForeground(Color.red);
//设计按钮的前景颜色
GridBagLayout gridBagLayout1 = new GridBagLayout(); //网袋布局
setLayout(gridBagLayout1);
bc.addItem("1");
bc.addItem("2");
bc.addItem("3");
bc.addItem("4");
bc.addItem("5");
add(blb1,new GridBagConstraints(0,0, 1 , 1 , 0.0, 0.0
,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 50, 0, 0), 0, 0));
add(bc,new GridBagConstraints(0,1, 1 , 1 , 0.0, 0.0
,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, -50, 0, 0), 0, 0));
add(bb1,new GridBagConstraints(1,1, 1 , 1 , 0.0, 0.0
,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, -50, 0, 20), 0, 0));
add(bb2,new GridBagConstraints(0,2, 1 , 1 , 0.0, 0.0
,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 50, 0, 0), 0, 0));
add(bb3,new GridBagConstraints(0,3, 1 , 1 , 0.0, 0.0
,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, -50, 0, 0), 0, 0));
add(bb4,new GridBagConstraints(1,3, 1 , 1 , 0.0, 0.0
,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, -70, 0, 0), 0, 0));
add(bb6,new GridBagConstraints(0,4, 1 , 1 , 0.0, 0.0
,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 50, 0, 0), 0, 0));
add(bb5,new GridBagConstraints(0,5, 1 , 1 , 0.0, 0.0
,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 50, 0, 0), 0, 0));
bb1.addActionListener(this); //为5个按钮添加监听
bb2.addActionListener(this);
bb3.addActionListener(this);
bb4.addActionListener(this);
bb5.addActionListener(this);
bb6.addActionListener(this);
setBorder(BorderFactory.createTitledBorder("【B】面板"));
//为面板加上边框
}
//-----------------------------------------------------------------------------------------------
public void actionPerformed(ActionEvent e1) //监听类的实现
{
if(e1.getSource()==bb1) //如果是按的生成按钮
{
String str=bc.getSelectedItem(); //把下拉菜单所选中的字符串赋给临时字符串
int temp=Integer.parseInt(str); //转换成int型
String pp[]=algo.great(temp);
//调用自定义类下的静态方法返回随机数
//返回型是string型的
switch(temp) //判断下拉菜单的字符串数
{
case 1:atf1.setText(pp[0]);
atf2.setText("");
atf3.setText("");
atf4.setText("");
atf5.setText("");break;
//如果是1就把返回的随机数放在第1个textfield上
//然后把之后的清空
case 2:atf1.setText(pp[0]);
atf2.setText(pp[1]);
atf3.setText("");
atf4.setText("");
atf5.setText("");break;
//如果是2就把返回的随机数放在前2个textfield上
//然后把之后的清空
//以下同理
case 3:atf1.setText(pp[0]);
atf2.setText(pp[1]);
atf3.setText(pp[2]);
atf4.setText("");
atf5.setText("");break;
case 4:atf1.setText(pp[0]);
atf2.setText(pp[1]);
atf3.setText(pp[2]);
atf4.setText(pp[3]);
atf5.setText("");break;
default:atf1.setText(pp[0]);
atf2.setText(pp[1]);
atf3.setText(pp[2]);
atf4.setText(pp[3]);
atf5.setText(pp[4]);break;
}
}
if(e1.getSource()==bb2) //如果被激活的是保存按狃
{
String str2=atf1.getText()+"\r\n"+ //把所有文本中的字符串加到1个
atf2.getText()+"\r\n"+ //总的字符串上
atf3.getText()+"\r\n"+
atf4.getText()+"\r\n"+
atf5.getText()+"\r\n";
save.setVisible(true);
//让save文件框可见
log.write_log(str2,save);
//调用log类下的静态方法.把save文件框和要写入的字符串传过去
}
if(e1.getSource()==bb3) //如果被激活的是摇奖
{
bb3.setBorderPainted(false); //让摇奖钮变平
bb3.setEnabled(false); //让摇奖钮不可点
ss=algo.great1(); //调用algo类下的返回1组随机数的方法返回一组随机数
atf6.setText(ss); //把这组随机数加到textfield上,作为开奖号码
}
if(e1.getSource()==bb4) //如果被激活的是开奖按钮
{
String sq=log.read_log(ss);
//调用log类下的读取方法,(把开奖号码传过去做比较)
//经过函数处理,返回一个提示信息的字符串
JOptionPane.showMessageDialog(null,sq,"开奖提示",JOptionPane.ERROR_MESSAGE);
//把返回的提示信息输出
}
if(e1.getSource()==bb5) //如果被激活的是退出按钮
{
System.exit(0);
}
if(e1.getSource()==bb6) //如果被激活的是保存DB按钮
{
String str=bc.getSelectedItem(); //把下拉菜单所选中的字符串赋给临时字符串
int temp=Integer.parseInt(str); //转换成int型
switch(temp) //判断下拉菜单中的字符串
{
case 1:Storeroom.write(atf1.getText());break;
//如果是1,就调用自定以方法Storeroom下的静态方法
//把文本1中的信息传过去,通过这个方法把这行字符串写入数据库
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -