自拟题目编写一个图形界面程序,;

项目要求:
自拟题目编写一个图形界面程序,要求:
1.至少使用两种以上控制组件;
2.至少使用两种以上容器组件和布局管理器;
3.能够实现简单交互功能;
4.要满足Java编程规范
5.能够稳定运行。
跪求啊 明早要交

按照你的要求编写的Java程序如下,这是一个35选7彩票模拟选号的Java程序,输入你选的几组号码,存入lottery.txt文件,看看你猜中了几个号码.

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("添加本组号码");
 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<a.length;i++){
      b[i]=Integer.parseInt(a[i].trim());
     }
     Arrays.sort(b);
     String s="";
     for(int i=0;i<b.length;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<a.length;i++){
      int count=0;
      String[]b=a[i].split(",");
      String tmp="";
      for(int j=0;j<b.length;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();
     JOptionPane.showMessageDialog(null, "保存完毕!");
     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<a.length;i++){
    if(tmp==a[i]){
     flag=true;
    }
   }
   if(flag==false){
    a[count]=tmp;
    count++;
   }
  }
  Arrays.sort(a);
  String s="";
  for(int i=0;i<a.length;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();
 }
}

运行结果

lottery.txt文件内容

本期开奖号码:03,06,26,29,30,33,35

01,05,12,14,25,29,32 - 1 

-1 表示猜中了一个号码29

温馨提示:答案为网友推荐,仅供参考
相似回答