展开全部
按照你的要求编写的Java程序如下:import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class CCG extends JFrame implements ActionListener{
JLabel jl1=new JLabel("35选7",JLabel.LEFT);
JLabel jl2=new JLabel("输入选号:");
JLabel jl3=new JLabel("当前选号:");
JLabel jl4=new JLabel();
JTextField jtf=new JTextField(5);
JTextArea jta=new JTextArea(5,20);
JButton jb1=new JButton("添加本62616964757a686964616fe58685e5aeb931333361303639组号码");
JButton jb2=new JButton("验证并保存到文件");
JPanel jp1=new JPanel();
JPanel jp2=new JPanel();
JPanel jp3=new JPanel();
JPanel jp4=new JPanel();
JPanel jp5=new JPanel();
JPanel jp6=new JPanel();
JPanel jp7=new JPanel();
boolean saveFlag=false;
CCG(){
super("Lottery");
jtf.addActionListener(this);
jb1.addActionListener(this);
jb2.addActionListener(this);
jl1.setFont(new Font(null,Font.BOLD,20));
jp1.setLayout(new FlowLayout(FlowLayout.LEFT));
jp1.add(jl1);
jp1.setBorder(BorderFactory.createEtchedBorder ());
jp4.setLayout(new FlowLayout(FlowLayout.LEFT));
jp4.add(jl2);jp4.add(jtf);
jp5.setLayout(new FlowLayout(FlowLayout.LEFT));
jp5.add(jl3);jp5.add(jl4);
jp6.setLayout(new FlowLayout(FlowLayout.LEFT));
jp6.add(jb1);
jp2.setLayout(new GridLayout(3,1));
jp2.add(jp4);jp2.add(jp5);jp2.add(jp6);
jp2.setBorder(BorderFactory.createEtchedBorder ());
jp3.setLayout(new FlowLayout(FlowLayout.LEFT));
jp3.add(jta);jp3.add(jb2);
jp3.setBorder(BorderFactory.createEtchedBorder ());
jp7.setLayout(new GridLayout(2,1));
jp7.add(jp2);
jp7.add(jp3);
add(jp1,BorderLayout.NORTH);
add(jp7,BorderLayout.CENTER);
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent ae) {
if(ae.getSource()==jtf){
if(jtf.getText().trim().equals("")){
jl4.setText("");
}else{
int n=Integer.parseInt(jtf.getText().trim());
String str="";
if(n<10)str="0"+n;
else str=n+"";
if(jl4.getText().trim().contains(str)){
JOptionPane.showMessageDialog(null, "输入的数重复!");
jtf.requestFocus();
return;
}
int num=Integer.parseInt(str);
if(num<1 || num>35){
JOptionPane.showMessageDialog(null, "输入的数需要在1-35之间!");
jtf.requestFocus();
return;
}
if(jl4.getText().trim().equals("")){
int t=Integer.parseInt(jtf.getText().trim());
if(t<10) jl4.setText("0"+t);
else jl4.setText(""+t);
}else{
String[]a=(jl4.getText().trim()+","+jtf.getText().trim()).split(",");
if(a.length>7){
JOptionPane.showMessageDialog(null, "输入的数超过7个!");
jtf.requestFocus();
return;
}
int []b=new int[a.length];
for(int i=0;i
b[i]=Integer.parseInt(a[i].trim());
}
Arrays.sort(b);
String s="";
for(int i=0;i
if(i==b.length-1)
if(b[i]<10)s=s+"0"+b[i];
else s=s+b[i];
else
if(b[i]<10)s=s+"0"+b[i]+",";
else s=s+b[i]+",";
}
jl4.setText(s);
}
}
}
if(ae.getSource()==jb1){
jta.append(jl4.getText().trim()+"\n");
saveFlag=true;
}
if(ae.getSource()==jb2){
if(saveFlag==true){
BufferedWriter bw = null;
try {
bw=new BufferedWriter(new FileWriter("lottery.txt",true));
String s=random();
bw.append("本期开奖号码:"+s);
bw.newLine();
String[]a=jta.getText().split("\n");
for(int i=0;i
int count=0;
String[]b=a[i].split(",");
String tmp="";
for(int j=0;j
if(s.contains(b[j].trim()))count++;
if(j==b.length-1){
tmp=tmp+b[j].trim();
}else{
tmp=tmp+b[j].trim()+",";
}
}
String tmpwrite=tmp+" - "+count;
bw.append(tmpwrite);
bw.newLine();
}
bw.flush();
System.out.println("保存完毕!");
saveFlag=false;
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
public String random() {
int []a=new int[7];
int count=0;
while(count<7){
boolean flag=false;
int tmp=(int)(Math.random()*35)+1;
for(int i=0;i
if(tmp==a[i]){
flag=true;
}
}
if(flag==false){
a[count]=tmp;
count++;
}
}
Arrays.sort(a);
String s="";
for(int i=0;i
if(i==a.length-1){
if(a[i]<10){
s=s+"0"+a[i];
}else{
s=s+a[i];
}
}else{
if(a[i]<10){
s=s+"0"+a[i]+",";
}else{
s=s+a[i]+",";
}
}
}
return s;
}
public static void main(String[] args) {
new CCG();
}
}
运行结果: