5.1作业5 四则运算 测试与封装

  这次作业是我们小组成员我(21林庆光)和同伴(13卢盛添)的结伴作业,按照老师的要求我们换了同伴组队来完成这次的作业。这次的作业是在上次作业四则运算的基础上完成的,实现了对其计算功能类的封装,完成代码如下:
import
java.awt.BorderLayout; import java.awt.Color; import java.awt.Component; import java.awt.Container; import java.awt.Dimension; import java.awt.Font; import java.awt.Panel; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import javax.swing.ButtonGroup; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JColorChooser; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JPasswordField; import javax.swing.JPopupMenu; import javax.swing.JRadioButtonMenuItem; import javax.swing.JTextField; /** * 倒计时 * */ public class Counter { public static void main(String[] args) { Xiao xiao=new Xiao(); xiao.visiable(); } public void repaint() { // TODO 自动生成的方法存根 } public Component getContentPane() { // TODO 自动生成的方法存根 return null; } public void visiable() { } } /* 构造 实现界面的开发 GUI */ class Xiao extends JFrame implements ActionListener{ public static double daan=0; public static double sum=0; public static int a=0; public static int b=0; public static int ch=0; public static int n=0; public static double i=0 ; public static double j=0 ; public static double m=0 ; public JFrame jf = new JFrame("小学生四则运算管理系统"); public Container con = jf.getContentPane(); Panel P1=new Panel(); Panel P2=new Panel(); Panel P3=new Panel(); public Toolkit toolkit = Toolkit.getDefaultToolkit(); public Dimension sc = toolkit.getScreenSize(); public JLabel name1 = new JLabel("请输入答案"); public JLabel pass1 = new JLabel("信息提醒"); public JLabel pass2 = new JLabel("所剩时间"); public JLabel pass3 = new JLabel("更换皮肤"); public JLabel pass4 = new JLabel("时间提醒"); public JTextField textName = new JTextField(); public JTextField textPs = new JTextField(); public JTextField text1 = new JTextField(); public JTextField text2 = new JTextField(); public JTextField text3 = new JTextField(); public JButton button1 = new JButton("随机获取题目"); public JButton button2 = new JButton("确定"); public JButton button3 = new JButton("重置"); public JButton button4 = new JButton("黄色"); public JButton button5 = new JButton("绿色"); public JButton button6 = new JButton("粉色"); public Font font1 = new Font("宋体",1,14); public Font font2 = new Font("宋体",0,12); public Xiao() { JPanel jp = new JPanel(); con.setLayout(null); name1.setFont(font1); name1.setLocation(52,25); name1.setSize(80,180); name1.setForeground(Color.black); pass1.setLocation(55,90); pass1.setSize(80,250); pass1.setForeground(Color.black); pass1.setFont(font1); pass2.setLocation(55,110); pass2.setSize(100,300); pass2.setForeground(Color.black); pass2.setFont(font1); pass3.setLocation(55,160); pass3.setSize(100,305); pass3.setForeground(Color.black); pass3.setFont(font1); pass4.setLocation(280,110); pass4.setSize(100,300); pass4.setForeground(Color.black); pass4.setFont(font1); textName.setSize(140, 20); textName.setLocation(170, 70); textPs.setSize(140, 20); textPs.setLocation(170, 105); text1.setSize(140, 20); text1.setLocation(170, 205); text2.setSize(100, 20); text2.setLocation(170, 250); text3.setSize(100, 20); text3.setLocation(350, 250); button1.setSize(110,30); button1.setLocation(40,63 ); button1.setFont(font2); button2.setSize(60,25); button2.setLocation(320, 100); button2.setFont(font2); button3.setSize(60,25); button3.setLocation(400, 100); button3.setFont(font2); button4.setSize(60,25); button4.setLocation(170, 300); button4.setFont(font2); button5.setSize(60,25); button5.setLocation(240, 300); button5.setFont(font2); button6.setSize(60,25); button6.setLocation(310, 300); button6.setFont(font2); con.add( button4, BorderLayout.NORTH); con.add(name1); con.add(pass1); con.add(pass2); con.add(pass3); con.add(pass4); con.add(textName); con.add(textPs); con.add(text1); con.add(text2); con.add(text3); con.add(button1); con.add(button2); con.add(button3); con.add(button4); con.add(button5); con.add(button6); con.setBackground(Color.orange); jf.setResizable(false); jf.add(jp); jf.setVisible(true); jf.setLocation(300, 300); jf.setSize(500, 400); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); textName.addActionListener(this); textPs.addActionListener(this); text1.addActionListener(this); text2.addActionListener(this); button1.addActionListener( this); button2.addActionListener( this); button3.addActionListener( this); button3.addActionListener( this); button4.addActionListener( this); button5.addActionListener( this); button6.addActionListener( this); long time = 1 * 120; // 自定义倒计时时间 long hour = 0; long minute = 0; long seconds = 0; while (time >= 0) { minute = (time - hour * 3600) / 60; seconds = time - hour * 3600 - minute * 60; text2.setText(hour + "时"+minute + "分"+seconds + "秒"); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } time--; if(time==0){ text3.setText("答题时间结束!!"); } } } void visiable() { this.setVisible(true); } public void actionPerformed(ActionEvent e) { if (e.getSource() == button1) { Jsuan deleinfo =new Jsuan(); deleinfo.visiable(); } else if(e.getSource()==button2) { daan=Integer.parseInt(textPs.getText()); if(daan==sum) { text1.setText("回答正确!你得到10分!"); textPs.setText(null); i=i+10; } else{ text1.setText("回答错误,扣掉10分!" ); i=i-10; textPs.setText(null); } } else if(e.getSource()== button3){ textPs.setText(null); } else if(e.getSource()== button4){ con.setBackground(Color.yellow); //设置窗口背景 repaint(); //重绘窗口 return; } else if(e.getSource()== button5){ con.setBackground(Color.green); //设置窗口背景 repaint(); //重绘窗口 return; } else if(e.getSource()== button6){ con.setBackground(Color.pink); //设置窗口背景 repaint(); //重绘窗口 return; } } class Jsuan extends JFrame implements ActionListener{ public Jsuan(){ a=(int)(Math.random()*(-200))+100; b=(int)(Math.random()*(-200))+100; ch=(int)(Math.random()*5)+1; n=(int)(Math.random()*5)+1; if(a<0 && b>0){ switch(ch){ case 1: { sum=a+b; textName.setText("("+a+")"+"+"+b+"=?"); break; } case 2: { sum=a-b; textName.setText("("+a+")"+"-"+b+"=?"); break; } case 3: { sum=a*b; textName.setText("("+a+")"+"*"+b+"=?"); break; } case 4: { sum=a/b; textName.setText("("+a+")"+"/"+b+"=?"); break; } case 5: //6.阶乘算法,n为随机生成数 { sum=1; for(j=1;j<=n;i++) sum=sum*i; textName.setText(n+"!"+"=?"); break; } } } else if(a>0 && b<0){ switch(ch){ case 1: { sum=a+b; textName.setText(a+"+"+"("+b+")"+"=?"); break; } case 2: { sum=a-b; textName.setText(a+"-"+"("+b+")"+"=?"); break; } case 3: { sum=a*b; textName.setText(a+"*"+"("+b+")"+"=?"); break; } case 4: { sum=a/b; textName.setText(a+"/"+"("+b+")"+"=?"); break; } case 5: //6.阶乘算法,n为随机生成数 { sum=1; for(j=1;j<=n;i++) sum=sum*i; textName.setText(n+"!"+"=?"); break; } } } else if(a<0 && b<0){ switch(ch){ case 1: { sum=a+b; textName.setText("("+a+")"+"+"+"("+b+")"+"=?"); break; } case 2: { sum=a-b; textName.setText("("+a+")"+"-"+"("+b+")"+"=?"); break; } case 3: { sum=a*b; textName.setText("("+a+")"+"*"+"("+b+")"+"=?"); break; } case 4: { sum=a/b; textName.setText("("+a+")"+"/"+"("+b+")"+"=?"); break; } case 5: //6.阶乘算法,n为随机生成数 { sum=1; for(j=1;j<=n;i++) sum=sum*i; textName.setText(n+"!"+"=?"); break; } } } else{ switch(ch){ case 1: { sum=a+b; textName.setText(a+"+"+b+"=?"); break; } case 2: { sum=a-b; textName.setText(a+"-"+b+"=?"); break; } case 3: { sum=a*b; textName.setText(a+"*"+b+"=?"); break; } case 4: { sum=a-b; textName.setText(a+"/"+b+"=?"); break; } case 5: //6.阶乘算法,n为随机生成数 { sum=1; for(j=1;j<=n;i++) sum=sum*i; textName.setText(n+"!"+"=?"); break; } } } } public void visiable() { // TODO 自动生成的方法存根 } @Override public void actionPerformed(ActionEvent arg0) { // TODO 自动生成的方法存根 } } }
下面有程序截图,这次作业是由我和我的同伴14卢盛添共同完成,不过他没开通博客,还望老师理解。

这两张截图是程序的封装和调用。

原文地址:https://www.cnblogs.com/guangge/p/4468759.html