10月14日学习日志
今天改进了小学生出题的界面,可以进行选择出题范围和运算符。
代码为:
package math4; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Font; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextField; public class math4 extends JFrame implements ActionListener{ int max= 20 ; int MAX= 10 ; int TYPE= 1 ; JLabel jlquestion[]= new JLabel[20]; JLabel jlAnswer[]= new JLabel[20]; JLabel jlTotal= new JLabel( "共" +max+ "题" ); JLabel jlcorrect[]= new JLabel[20]; JTextField jtf[]= new JTextField[20]; JMenuBar jmb= new JMenuBar(); JMenu jm= new JMenu( "类型 " ); JMenuItem jmi1= new JMenuItem( "10以内加法" ); JMenuItem jmi2= new JMenuItem( "10以内减法" ); JMenuItem jmi3= new JMenuItem( "20以内加法" ); JMenuItem jmi4= new JMenuItem( "20以内减法" ); JMenuItem jmi5= new JMenuItem( "100以内加法" ); JMenuItem jmi6= new JMenuItem( "100以内减法" ); JMenuItem jmi7= new JMenuItem( "100以内乘法" ); JMenuItem jmi8= new JMenuItem( "100以内除法" ); JButton jb1= new JButton( "出题" ); JButton jb2= new JButton( "评卷" ); JButton jb3= new JButton( "答案" ); JPanel jp1= new JPanel(); JPanel jp2= new JPanel(new GridLayout(5,4)); String[] question= new String[max]; String [] answer= new String [max]; math4(){ super ( "小学数学测试" ); for(int i=0;i<20;i++) { jtf[i]=new JTextField(3); jlquestion[i]=new JLabel(""); jlAnswer[i]=new JLabel(""); jlcorrect[i]= new JLabel(); } jlTotal.setFont( new Font( null ,Font.PLAIN, 20 )); for(int i=0;i<20;i++) { jtf[i].setFont( new Font( null ,Font.PLAIN, 20 )); jlAnswer[i].setFont( new Font( null ,Font.PLAIN, 20 )); jlquestion[i].setFont( new Font( null ,Font.PLAIN, 20 )); jlcorrect[i].setFont( new Font( null ,Font.PLAIN, 20 )); jlcorrect[i].setForeground(Color.RED); } Question(MAX, TYPE); jb1.addActionListener( this ); jb2.addActionListener( this ); jb3.addActionListener( this ); jmi1.addActionListener( this ); jmi2.addActionListener( this ); jmi3.addActionListener( this ); jmi4.addActionListener( this ); jmi5.addActionListener( this ); jmi6.addActionListener( this ); jmi7.addActionListener( this ); jmi8.addActionListener( this ); jm.add(jmi1);jm.add(jmi2);jm.add(jmi3);jm.add(jmi4);jm.add(jmi5);jm.add(jmi6);jm.add(jmi7);jm.add(jmi8); jmb.add(jm); setJMenuBar(jmb); jp1.add(jlTotal);jp1.add(jb1);jp1.add(jb2);jp1.add(jb3); for(int i=0;i<20;i++) { jp2.add(jlquestion[i]);jp2.add(jtf[i]);jp2.add(jlcorrect[i]);jp2.add(jlAnswer[i]); } add(jp1,BorderLayout.NORTH); add(jp2,BorderLayout.CENTER); setSize(1500,500); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo( null ); setVisible( true ); } public void actionPerformed(ActionEvent ae) { if(ae.getSource()==jb1) { for(int i=0;i<20;i++) { jlquestion[i].setText(question[i]); jtf[i].setEnabled( true ); } } if (ae.getSource()==jb2){ for(int i=0;i<20;i++) { jlquestion[i].setText(question[i]); if(jtf[i].getText().trim().equals(answer[i])) { jlcorrect[i].setText("√"); } else jlcorrect[i].setText("×"); jtf[i].setEnabled( true ); } } if (ae.getSource()==jb3){ for(int i=0;i<20;i++) { jlquestion[i].setText(question[i]); jlAnswer[i].setText( "答案:"+answer[i] ); if(jtf[i].getText().trim().equals(answer[i])) { jlcorrect[i].setText("√"); } else jlcorrect[i].setText("×"); jtf[i].setEnabled( true ); } } if(ae.getSource()==jmi1){ for(int i=0;i<20;i++) { jlquestion[i].setText(""); jlAnswer[i].setText(""); jlcorrect[i].setText(""); jtf[i].setText(""); } MAX= 10 ;TYPE= 1 ;Question(MAX,TYPE); } if(ae.getSource()==jmi2){ for(int i=0;i<20;i++) { jlquestion[i].setText(""); jlAnswer[i].setText(""); jlcorrect[i].setText(""); jtf[i].setText(""); } MAX= 10 ;TYPE= 2 ;Question(MAX,TYPE); } if(ae.getSource()==jmi3){ for(int i=0;i<20;i++) { jlquestion[i].setText(""); jlAnswer[i].setText(""); jlcorrect[i].setText(""); jtf[i].setText(""); } MAX= 20 ;TYPE= 1 ;Question(MAX,TYPE); } if(ae.getSource()==jmi4){ for(int i=0;i<20;i++) { jlquestion[i].setText(""); jlAnswer[i].setText(""); jlcorrect[i].setText(""); jtf[i].setText(""); } MAX= 20 ;TYPE= 2 ;Question(MAX,TYPE); } if(ae.getSource()==jmi5){ for(int i=0;i<20;i++) { jlquestion[i].setText(""); jlAnswer[i].setText(""); jlcorrect[i].setText(""); jtf[i].setText(""); } MAX= 100 ;TYPE= 1 ;Question(MAX,TYPE); } if(ae.getSource()==jmi6){ for(int i=0;i<20;i++) { jlquestion[i].setText(""); jlAnswer[i].setText(""); jlcorrect[i].setText(""); jtf[i].setText(""); } MAX= 100 ;TYPE= 2 ;Question(MAX,TYPE); } if(ae.getSource()==jmi7){ for(int i=0;i<20;i++) { jlquestion[i].setText(""); jlAnswer[i].setText(""); jlcorrect[i].setText(""); jtf[i].setText(""); } MAX= 100 ;TYPE= 3 ;Question(MAX,TYPE); } if(ae.getSource()==jmi8){ for(int i=0;i<20;i++) { jlquestion[i].setText(""); jlAnswer[i].setText(""); jlcorrect[i].setText(""); jtf[i].setText(""); } MAX= 100 ;TYPE= 4 ;Question(MAX,TYPE); } } public static void main(String[] args) { new math4(); } public void Question( int MAX, int TYPE) { int a,b; for(int i=0;i<20;i++){ a=( int )(Math.random()*MAX+ 1 ); b=( int )(Math.random()*MAX+ 1 ); switch (TYPE){ case 1 :answer[i]=String.valueOf(a+b); break ; case 2 :answer[i]=String.valueOf(a-b); break ; case 3 :answer[i]=String.valueOf(a*b); break ; case 4 : while(b==0||a%b!=0) { a=( int )(Math.random()*MAX+ 1 ); b=( int )(Math.random()*MAX+ 1 ); } answer[i]=String.valueOf(a/b); break ; } switch (TYPE) { case 1:question[i]=String.valueOf(a)+"+"+String.valueOf(b)+"=";break; case 2:question[i]=String.valueOf(a)+"-"+String.valueOf(b)+"=";break; case 3:question[i]=String.valueOf(a)+"*"+String.valueOf(b)+"=";break; case 4:question[i]=String.valueOf(a)+"/"+String.valueOf(b)+"=";break; } } } }
运行结果为: