注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

小木舟的博客

笔墨生活随想,记录似水年华。

 
 
 

日志

 
 

Java大作业代码  

2009-03-14 11:18:31|  分类: 编程学习 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
 
查看文章

 
Java大作业代码1
2008-12-13 15:57

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.border.*;

/**
*
* @author Administrator
*/
public class Calculator extends JFrame {
   
    

    //按钮的定义
    private JButton jButtonOk = new JButton("确定");
    private JButton jButtonJudge = new JButton("判断");
    private JButton jButtonNext = new JButton("下一题");
    private JButton jButtonExit = new JButton("退出");
    private JButton jButtonReset = new JButton("复位");
    private JButton jButtonAbout = new JButton("关于");
    private JButton jButtonHistory = new JButton("历史记录");
   
    //标签的定义
    private JLabel jLabelStart = new JLabel("点击此处开始做题");
    private JLabel jLabelEqual = new JLabel("=");
    private JLabel jLabelScore = new JLabel("总得分");
    private JLabel jLabelRight = new JLabel("目前正确率");
    private JLabel jLabelMedia = new JLabel("提示",new ImageIcon("image/ini.gif"),SwingConstants.CENTER);
    private JLabel jLabelRange = new JLabel("请输入正整数的范围上限");
    private JLabel jLabelNumber = new JLabel("请输入题目的数量");
    private JLabel jLabelTitle = new JLabel("小学生四则运算小游戏");
    private JLabel jLabelBackground = new JLabel(new ImageIcon("image/bj.jpg"));
   
   
   
    //文本域的定义
    private JTextField jTextFieldNumber = new JTextField(10);
    private JTextField jTextFieldDividend = new JTextField(10);
    private JTextField jTextFieldOperation = new JTextField();
    private JTextField jTextFieldDivisor = new JTextField(10);
    private JTextField jTextFieldAnswer = new JTextField(10);
    private JTextField jTextFieldScore = new JTextField(10);
    private JTextField jTextFieldRight = new JTextField(10);
    private JTextField jTextFieldRange = new JTextField(10);
    private JTextField jTextFieldMedia = new JTextField(40);
   
    //创建具有指定颜色和厚度的线边框
    Border lineBorder = new LineBorder(Color.BLACK, 1);
   
    long start;          //定义计算历时多少时间的变量
    long end;
    String time = " 非法操作,请按正确步骤操作!";
   
    int numberOfIssues = 0 ;
    int range = 0;
    int random1,random2;
    int rightAnswer = 0;
    static double accuracy = 0;
    static int haveDoneNumber = 0;
    static int correctNumber = 0;
    static int score = 0;
    String history = "";
    String result = "";
    String op = "";
   
    public Calculator()                   //构造函数
    {
    
    
     jTextFieldDividend.setEditable(false);            //将只起显示作用的文本域设为不可编辑
     jTextFieldOperation.setEditable(false);
     jTextFieldDivisor.setEditable(false);
     jTextFieldScore.setEditable(false);
     jTextFieldRight.setEditable(false);
     jTextFieldMedia.setEditable(false);
     jTextFieldAnswer.setEditable(false);
    
     jButtonJudge.setEnabled(false);
     jButtonNext.setEnabled(false);             //将按钮设为不可编辑
    
     jTextFieldNumber.setBorder(lineBorder);            //设置文本域的边框
     jTextFieldDividend.setBorder(lineBorder);
     jTextFieldOperation.setBorder(lineBorder);
     jTextFieldDivisor.setBorder(lineBorder);
     jTextFieldAnswer.setBorder(lineBorder);
     jTextFieldScore.setBorder(lineBorder);
     jTextFieldRight.setBorder(lineBorder);
     jTextFieldRange.setBorder(lineBorder);
     jTextFieldMedia.setBorder(lineBorder);
    
     //布局设置
     JPanel pFirst = new JPanel(new FlowLayout());
     pFirst.add(jLabelStart,BorderLayout.CENTER);
     pFirst.add(jButtonOk);
    
     JPanel pSecond = new JPanel(new FlowLayout());
     jTextFieldNumber.setHorizontalAlignment(SwingConstants.CENTER);
     jTextFieldNumber.setFont(new Font("Serif",Font.BOLD,16));
     jTextFieldRange.setHorizontalAlignment(SwingConstants.CENTER);
     jTextFieldRange.setFont(new Font("Serif",Font.BOLD,16));
     pSecond.add(jLabelNumber);
     pSecond.add(jTextFieldNumber);
     pSecond.add(jLabelRange,BorderLayout.WEST);
     pSecond.add(jTextFieldRange,BorderLayout.EAST);
    
     JPanel pSecondadd = new JPanel(new BorderLayout());
     jLabelTitle.setHorizontalAlignment(SwingConstants.CENTER);
     jLabelTitle.setForeground(Color.blue);
     jLabelTitle.setFont(new Font("Serif",Font.BOLD,20));
     pSecondadd.add(jLabelTitle,BorderLayout.NORTH);
     pSecondadd.add(pSecond,BorderLayout.SOUTH);
    
     JPanel pThird = new JPanel(new GridLayout(1,5,2,2));
     jTextFieldDividend.setHorizontalAlignment(SwingConstants.CENTER);
     jTextFieldDividend.setFont(new Font("Serif",Font.BOLD,16));
     jTextFieldOperation.setHorizontalAlignment(SwingConstants.CENTER);
     jTextFieldOperation.setFont(new Font("Serif",Font.BOLD,16));
     jTextFieldDivisor.setHorizontalAlignment(SwingConstants.CENTER);
     jTextFieldDivisor.setFont(new Font("Serif",Font.BOLD,16));
     jLabelEqual.setHorizontalAlignment(SwingConstants.CENTER);
     jLabelEqual.setFont(new Font("Serif",Font.BOLD,16));
     jTextFieldAnswer.setHorizontalAlignment(SwingConstants.CENTER);
     jTextFieldAnswer.setFont(new Font("Serif",Font.BOLD,16));
     pThird.add(jTextFieldDividend);
     pThird.add(jTextFieldOperation);
     pThird.add(jTextFieldDivisor);
     pThird.add(jLabelEqual);
     pThird.add(jTextFieldAnswer);
    
     JPanel pFouth = new JPanel(new GridLayout(1,6,5,3));
     pFouth.add(jButtonJudge);
     pFouth.add(jButtonNext);
     pFouth.add(jButtonHistory);
     pFouth.add(jButtonReset);
     pFouth.add(jButtonAbout);
     pFouth.add(jButtonExit);
    
     JPanel pFifth = new JPanel(new FlowLayout( FlowLayout.LEFT));
     jTextFieldScore.setHorizontalAlignment(SwingConstants.CENTER);
     jTextFieldScore.setFont(new Font("Serif",Font.BOLD,16));
     jTextFieldRight.setHorizontalAlignment(SwingConstants.CENTER);
     jTextFieldRight.setFont(new Font("Serif",Font.BOLD,16));
     pFifth.add(jLabelScore );
     pFifth.add(jTextFieldScore);
     pFifth.add(jLabelRight,BorderLayout.CENTER);
     pFifth.add(jTextFieldRight,BorderLayout.EAST );
    
     JPanel pSixth = new JPanel(new FlowLayout(FlowLayout.CENTER));        //多媒体显示设置
     jTextFieldMedia.setHorizontalAlignment(SwingConstants.CENTER);
     jTextFieldMedia.setFont(new Font("Serif",Font.BOLD,14));
     pSixth.add(jLabelMedia);
     pSixth.add(jTextFieldMedia);
   
     JPanel pSeventh = new JPanel(new BorderLayout( 5,10));
     pSeventh.add(pSecondadd,BorderLayout.NORTH);
     pSeventh.add(pFirst,BorderLayout.SOUTH);
    
     JPanel pEighth = new JPanel(new BorderLayout( 5,10));
     pEighth.add(pSeventh,BorderLayout.NORTH);
     pEighth.add(pThird,BorderLayout.SOUTH);
    
     JPanel pFouthadd = new JPanel(new BorderLayout( 5,10));
     pFouthadd.add(pEighth,BorderLayout.NORTH);
     pFouthadd.add(pFouth,BorderLayout.SOUTH);
  
     JPanel pNinth = new JPanel(new BorderLayout(5,10));
     pNinth.add(pFouthadd,BorderLayout.NORTH);
     pNinth.add(pFifth,BorderLayout.SOUTH);
    
   
    
     add(pNinth,BorderLayout.CENTER);
     add(pSixth,BorderLayout.SOUTH);
    
    
   
   
    //确定按钮的监听
    jButtonOk.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            numberOfIssues = Integer.parseInt(jTextFieldNumber.getText());       //题目的数量
            range = Integer.parseInt(jTextFieldRange.getText());      //整数范围的上限
           
            start = System.currentTimeMillis();
           
            jTextFieldAnswer.setEditable(true);
            jTextFieldNumber.setEditable(false);
            jTextFieldRange.setEditable(false);
            jButtonJudge.setEnabled(true);
            jButtonNext.setEnabled(true);
            jButtonOk.setEnabled(false);
    
            random1 = (int)(Math.random()*range);              //随机取数
            random2 = (int)(Math.random()*range);
            jTextFieldDividend.setText("" + random1);
            jTextFieldDivisor.setText("" + random2);
           
            int operation = (int)(Math.random()* 4) ;           //随机取运算符
            if(operation == 0) {
                jTextFieldOperation.setText("+");
                op = "+";
                rightAnswer = random1 + random2;
              
            }
            else if(operation == 1) {
                jTextFieldOperation.setText("-");
                op = "-";
                 rightAnswer = random1 - random2;
            }
            else if(operation == 2) {
                 jTextFieldOperation.setText("*");
                 op = "*";
                  rightAnswer = random1 * random2;
            }
            else
            {
                jTextFieldOperation.setText("/");
                op = "/";
                while(random2 == 0){
                    jTextFieldDivisor.setText("");
                    random2 = (int)(Math.random()*range);
                   
                }
                jTextFieldDivisor.setText("" + random2);   
                rightAnswer = random1 / random2;
           
            }
        }
    });
   
    //判断按钮的监听
    jButtonJudge.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
           
            jTextFieldAnswer.setEditable(false);
            jButtonJudge.setEnabled(false);
            int answer = Integer.parseInt(jTextFieldAnswer.getText());             //用户答案的提取
            if((int)(rightAnswer) == answer){
                correctNumber++;
                score = correctNumber * 10;       
                haveDoneNumber ++;
                jTextFieldMedia.setText("好棒!你做对了!O(∩_∩)O 继续加油!!!");   //多媒体提示
                jLabelMedia.setIcon(new ImageIcon("image/dui.gif"));
                history = history + random1 + op + random2 + "=" + answer + "…… is right.\n";
                time = "   ****你还未完成所有的题目***\n" + "请按 下一题 按钮继续完成剩下的题目";
                if(haveDoneNumber >= numberOfIssues ) {
                    jButtonJudge.setEnabled(false);         //若题目数量已经达到要求,则设置按钮为不可编辑
                    jButtonNext.setEnabled(false);
                    jButtonOk.setEnabled(false);
                    jTextFieldNumber.setEditable(false);
                    jTextFieldRange.setEditable(false);
                    jTextFieldAnswer.setEditable(false);
                   
                    end = System.currentTimeMillis();
                    time = "你完成本次做题游戏所花的时间是 :\n" + (double)(end - start)/1000 + "秒" ;
                   
                }
                jTextFieldScore.setText("" + score + " 分");         //显示分数
            }
            else
            {
                jTextFieldScore.setText("" + score + " 分");
                haveDoneNumber ++;
                jTextFieldMedia.setText("真不走运!你做错了!正确答案是 " + rightAnswer + " 哦~~~~(>_<)~~~~ 没关系,加油啦!");
                jLabelMedia.setIcon(new ImageIcon("image/cuo.gif"));
                history = history + random1 + op + random2 + "=" + answer + "…… is false.\n";
                time = "   ****你还未完成所有的题目***\n" + "请按 下一题 按钮继续完成剩下的题目";
                if(haveDoneNumber >= numberOfIssues ) {
                    jButtonJudge.setEnabled(false);
                    jButtonNext.setEnabled(false);
                    jButtonOk.setEnabled(false);
                    jTextFieldNumber.setEditable(false);
                    jTextFieldRange.setEditable(false);
                    jTextFieldAnswer.setEditable(false);
                   
                    end = System.currentTimeMillis();
                    time = "你完成本次做题游戏所花的时间是 :\n" + (double)(end - start)/1000 + "秒" ;
                }
            }
           
           accuracy =(int)( (((double)correctNumber) / ((double)haveDoneNumber))*100);
           jTextFieldRight.setText("" + accuracy + "%");            //显示做题的正确率
           if(accuracy >=90){
               result = "你的成绩是优秀!";
           }
           else if(accuracy >= 80) {
               result = "你的成绩是良好!";
           }
           else if(accuracy >= 70) {
               result = "你的成绩是一般!";
           }
           else if(accuracy >= 60) {
               result = "你的成绩是及格!";
           }
           else {
               result = "你的成绩是不及格!加把劲哦!";
           }
          
           
        }
       
    });
   
    //下一题按钮的监听
    jButtonNext.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            jTextFieldDividend.setText("");
            jTextFieldDivisor.setText("");
            jTextFieldAnswer.setText("");
            jTextFieldMedia.setText("");
            jTextFieldAnswer.setEditable(true);
            jButtonJudge.setEnabled(true);
            jLabelMedia.setIcon(new ImageIcon("image/ini.gif"));
            random1 = (int)(Math.random()*range);
            random2 = (int)(Math.random()*range);
            jTextFieldDividend.setText("" + random1);
            jTextFieldDivisor.setText("" + random2);
           
            int operation = (int)(Math.random()* 4) ;
            if(operation == 0) {
                jTextFieldOperation.setText("+");
                op = "+";
                rightAnswer = random1 + random2;
            }
            else if(operation == 1) {
                jTextFieldOperation.setText("-");
                op = "-";
                 rightAnswer = random1 - random2;
            }
            else if(operation == 2) {
                 jTextFieldOperation.setText("*");
                 op = "*";
                  rightAnswer = random1 * random2;
            }
            else
            {
                jTextFieldOperation.setText("/");
                op = "/";
                while(random2 == 0){
                    jTextFieldDivisor.setText("");
                    random2 = (int)(Math.random()*range);
                   
                }
                jTextFieldDivisor.setText("" + random2);   
                rightAnswer = random1 / random2;
           
            }
         }
        
    });
   
    //退出按钮的监听
    jButtonExit.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
             System.exit(0);
         }
    });
   
    //复位按钮的监听
    jButtonReset.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            jTextFieldNumber.setText("");               //将文本域设置为空
            jTextFieldRange.setText("");
            jTextFieldDividend.setText("");
            jTextFieldOperation.setText("");
            jTextFieldDivisor.setText("");
            jTextFieldAnswer.setText("");
            jTextFieldScore.setText("");
            jTextFieldRight.setText("");
            jTextFieldMedia.setText("");
           
           
            numberOfIssues = 0 ;                  //将各个变量设置为空
            range = 0;
            random1 = 0;
            random2 = 0;
            rightAnswer = 0;
            accuracy = 0;
            haveDoneNumber = 0;
            correctNumber = 0;
            score = 0;
            history = "";
            result = "";
            op = "";
            start = 0;
            end = 0;
            time = " 非法操作,请按正确步骤操作!";
           
            jLabelMedia.setIcon(new ImageIcon("image/ini.gif"));    //将多媒体标签设置为原始状态
           
            jButtonOk.setEnabled(true);        //将需要的按钮和文本域设置为可编辑
            jButtonNext.setEnabled(false);
            jButtonJudge.setEnabled(false);
            jTextFieldNumber.setEditable(true);
            jTextFieldRange.setEditable(true);
            jTextFieldAnswer.setEditable(false);
           
   
         }
    });
   
    //关于按钮的监听
    jButtonAbout.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            JFrame about = new About();
            about.setTitle("关于本程序");
            about.pack();
            about.setLocationRelativeTo(null);
            about.setVisible(true);
         }
    });
   
    //历史记录按钮的监听
    jButtonHistory.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
             JOptionPane.showMessageDialog(null, history + "\n" + "******************\n"
                     + result ,"历史记录",JOptionPane.INFORMATION_MESSAGE);
             JOptionPane.showMessageDialog(null, time ,"时间提示",JOptionPane.INFORMATION_MESSAGE);
            
         }
       
    });

}
   
    //Main函数
    public static void main(String[] args) {
        // TODO code application logic here
        JOptionPane.showMessageDialog(null,"            O(∩_∩)O \n" + "请在键盘上输入你的答案哦"
                    ,"提示",JOptionPane.INFORMATION_MESSAGE);
        Calculator frame = new Calculator();
        frame.setTitle("小学生四则运算小游戏");
        frame.setLocation(350, 200);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();         //自动调整框架大小
        frame.setVisible(true);
    }
     

}

 
查看文章

 
Java大作业代码2
2008-12-13 15:58

//About类的定义
class About extends JFrame{
    
     private JLabel jLabelauthor = new JLabel("作者");           //标签的的定义
     private JLabel jLabelContact = new JLabel("联系方式");
     private JLabel jLabelVersion = new JLabel("版本");
     private JLabel jLabelMyPage = new JLabel("个人主页");
     private JLabel jLabelup = new JLabel("小学生四则运算小游戏",new ImageIcon("image/up.jpg"),SwingConstants.CENTER);
     private JLabel jLabeldown = new JLabel("小木舟工作室",new ImageIcon("image/down.jpg"),SwingConstants.CENTER);
    
     private JTextField jTextFieldauthor = new JTextField("小木舟");       //文本域的定义
     private JTextField jTextFieldContact = new JTextField("QQ:*******");
     private JTextField jTextFieldVersion = new JTextField(" 1.0 ");
     private JTextField jTextFieldMyPage = new JTextField("http://juxuan.fu.blog.163.com");
    
     //创建具有指定颜色和厚度的线边框
    Border lineBorder = new LineBorder(Color.BLACK, 1);
    
     About() {           //构造函数
        
         jLabelauthor.setHorizontalAlignment(SwingConstants.CENTER);     //设置标签属性
         jLabelContact.setHorizontalAlignment(SwingConstants.CENTER);
         jLabelVersion.setHorizontalAlignment(SwingConstants.CENTER);
         jLabelMyPage.setHorizontalAlignment(SwingConstants.CENTER);
         jLabelup.setFont(new Font("Serif",Font.BOLD,20));
         jLabelup.setForeground(Color.BLUE);
         jLabeldown.setForeground(Color.LIGHT_GRAY);
        
         jTextFieldauthor.setEditable(false);             //设置文本域属性
         jTextFieldauthor.setBorder(lineBorder);
         jTextFieldauthor.setHorizontalAlignment(SwingConstants.CENTER);
         jTextFieldauthor.setFont(new Font("Serif",Font.BOLD,16));
         jTextFieldContact.setEditable(false);
         jTextFieldContact.setBorder(lineBorder);
         jTextFieldContact.setHorizontalAlignment(SwingConstants.CENTER);
         jTextFieldContact.setFont(new Font("Serif",Font.BOLD,16));
         jTextFieldVersion.setEditable(false);
         jTextFieldVersion.setBorder(lineBorder);
         jTextFieldVersion.setHorizontalAlignment(SwingConstants.CENTER);
         jTextFieldVersion.setFont(new Font("Serif",Font.BOLD,16));
         jTextFieldMyPage.setEditable(false);
         jTextFieldMyPage.setBorder(lineBorder);
         jTextFieldMyPage.setHorizontalAlignment(SwingConstants.CENTER);
         jTextFieldMyPage.setFont(new Font("Serif",Font.BOLD,16));
        
         JPanel p = new JPanel(new GridLayout(4,2,5,5));        
         p.add(jLabelauthor);
         p.add(jTextFieldauthor);
         p.add(jLabelContact);
         p.add(jTextFieldContact);
         p.add(jLabelVersion);
         p.add(jTextFieldVersion);
         p.add(jLabelMyPage);
         p.add(jTextFieldMyPage);
        
         JPanel panel = new JPanel(new BorderLayout( 5,10));
         panel.add(jLabelup,BorderLayout.NORTH);
         panel.add(p,BorderLayout.CENTER);
         panel.add(jLabeldown,BorderLayout.SOUTH);
        
         add(panel);
    }
   
  
}

  评论这张
 
阅读(795)| 评论(1)
推荐

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017