⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 simplemaths.java

📁 本程序用Java语言描述了一个普通加减法练习
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
			panel1.add(lblhistory1b);
			panel2.add(lblhistory2b);
			panel3.add(lblhistory3b);
			panel4.add(lblhistory4b);
			panel5.add(lblhistory5b);
			panel6.add(lblhistory6b);
			panel7.add(lblhistory7b);
			panel8.add(lblhistory8b);
			panel9.add(lblhistory9b);
			panel10.add(lblhistory10b);
			
			con.add(lblhistory);
			con.add(panel0);
			con.add(panel1);
			con.add(panel2);
			con.add(panel3);
			con.add(panel4);
			con.add(panel5);
			con.add(panel6);
			con.add(panel7);
			con.add(panel8);
			con.add(panel9);
			con.add(panel10);
			con.add(Ok);
			
			historyList.setSize(320, 630);
			historyList.setLocation(screenWidth/2-160,screenHeight/2-315);
			historyList.setResizable(false);//不可更改对话框大小
			historyList.setVisible(true);
		}
	}
	
	public static String Questions(int range){//得到算术问题
		int x;
		int y;
		int z = (int)(Math.random() * 100); //随机得到运算方式
		String a = "";
		if(z % 2 == 0){
			while(true){
				x = (int)(Math.random() * range);
				y = (int)(Math.random() * range);
				if(x + y <= range) break;
			}
			a = x + " + " + y + "=";
			keys = x + y;
		}
		else{
			while(true){
				x = (int)(Math.random() * range);
				y = (int)(Math.random() * range);
				if(x >= y) break;
			}
			a = x + " - " + y + "=";
			keys = x - y;
		}
		return a;
	}
	
	public void Trainmode(int num,int range){//练习模式,不计分,有答案提示
		rangeOfQuestion = range;
		numOfItem = 1;
		numOfQuestion = 1;
		sumOfQuestion = num;
		question = new String[num];
		answer = new String[num];
		key = new int[num];
		exerciseMode = new JDialog(this,"练习模式",false);
		Container con = exerciseMode.getContentPane();
		con.setLayout(new FlowLayout(FlowLayout.LEFT));
		
		lblNum = new JLabel("第"+ numOfItem +"题");
		Font numFont = new Font("", Font.BOLD, 40);
		lblNum.setFont(numFont);
		lblNum.setPreferredSize(new Dimension(180, 50));
		
		question[numOfQuestion - 1] = Questions(rangeOfQuestion);
		key[numOfItem - 1] = keys;
		lblQuestion = new JLabel(question[numOfQuestion - 1]);
		Font questionFont = new Font("", Font.BOLD, 60);
		lblQuestion.setFont(questionFont);
		lblQuestion.setPreferredSize(new Dimension(360, 60));
		
		Answer = new JTextField();
		Answer.setPreferredSize(new Dimension(50, 30));
		
		NextQuestion = new JButton("下一题");
		FrontQuestion = new JButton("上一题");
		SearchKey = new JButton("看答案");
		Complete = new JButton("退出");
		SearchKey.setPreferredSize(new Dimension(120, 22));
		NextQuestion.setPreferredSize(new Dimension(130, 30));
		FrontQuestion.setPreferredSize(new Dimension(130, 30));
		Complete.setPreferredSize(new Dimension(130, 30));
		FrontQuestion.setEnabled(false);
        //相应按键事件监听
		NextQuestion.addActionListener(new ActionListener(){//下个问题
			public void actionPerformed(ActionEvent e){
				if(numOfItem == numOfQuestion){
					answer[numOfItem - 1] = Answer.getText();
					numOfItem ++;
					numOfQuestion ++;
					lblNum.setText("第"+ numOfItem +"题");
					Answer.setText("");
					question[numOfQuestion - 1] = Questions(rangeOfQuestion);
					lblQuestion.setText(question[numOfQuestion - 1]);
					key[numOfItem - 1] = keys;
				}
				else{
					answer[numOfItem - 1] = Answer.getText();
					numOfItem ++;
					lblNum.setText("第"+ numOfItem +"题");
					lblQuestion.setText(question[numOfItem - 1]);
					Answer.setText(answer[numOfItem - 1]);
				}
				if(numOfItem != 1) FrontQuestion.setEnabled(true);
				if(numOfItem == sumOfQuestion) NextQuestion.setEnabled(false);
				lblKey.setText("");
			}
		});
		FrontQuestion.addActionListener(new ActionListener(){//前一个问题
			public void actionPerformed(ActionEvent e){
				NextQuestion.setEnabled(true);
				answer[numOfItem - 1] = Answer.getText();
			    numOfItem --;
				lblNum.setText("第"+ numOfItem +"题");
				lblQuestion.setText(question[numOfItem - 1]);
				Answer.setText(answer[numOfItem - 1]);
				if(numOfItem == 1) FrontQuestion.setEnabled(false);
				lblKey.setText("");
			}
		});
		SearchKey.addActionListener(new ActionListener(){//查看答案
			public void actionPerformed(ActionEvent e){
				String Key = String.valueOf(key[numOfItem - 1]);
				String Instru;
				if(Answer.getText().equals(Key)) Instru = "太棒了,答对了!   参考答案:";
				else Instru = "很遗憾,答错了!   参考答案:";
				lblKey.setText(Instru + Key);
			}
		});
		Complete.addActionListener(new ActionListener(){//完成
			public void actionPerformed(ActionEvent e){
				exerciseMode.dispose();
				mSearch_History.setEnabled(true);
				exercise.stop();
				exerciseFocus = false;
				if(mMusic_BackMusic.getState()) audioClip.loop();
			}
		});
		exerciseMode.addWindowListener(new WindowAdapter(){//关闭窗口时的音乐判断
			public void windowClosing(WindowEvent evt){
				mSearch_History.setEnabled(true);
				exercise.stop();
				exerciseFocus = false;
				if(mMusic_BackMusic.getState()) audioClip.loop();
			}
		});
		
		JPanel Panel1 = new JPanel();
		JPanel Panel2 = new JPanel();
		JPanel Panel3 = new JPanel();
		JPanel Panel4 = new JPanel();
		
		Panel1.add(lblNum);
		Panel2.add(lblQuestion);
		Panel2.add(Answer);
		Panel3.add(SearchKey);
		Panel3.add(lblKey);
		Panel4.add(NextQuestion);
		Panel4.add(FrontQuestion);
		Panel4.add(Complete);
		
		con.add(Panel1);
		con.add(Panel2);
		con.add(Panel3);
		con.add(Panel4);
		
		exerciseMode.setSize(440, 270);
		exerciseMode.setResizable(false);//不可变大小
		exerciseMode.setLocation(screenWidth/2-220, screenHeight/2-135);
		exerciseMode.setVisible(true);
		exerciseFocus = true;
		mSearch_History.setEnabled(false);
	}
	
	public void Exammode(int num,int range){//测验模式,完成后有计分,并选择保存
		rangeOfQuestion = range;
		numOfItem = 1;
		numOfQuestion = 1;
		sumOfQuestion = num;
		question = new String[num];
		answer = new String[num];
		key = new int[num];
		examMode = new JDialog(this,"测验模式",false);
		Container con = examMode.getContentPane();
		con.setLayout(new FlowLayout(FlowLayout.LEFT));
		
		lblNum = new JLabel("第"+ numOfItem +"题");
		Font numFont = new Font("", Font.BOLD, 40);
		lblNum.setFont(numFont);
		lblNum.setPreferredSize(new Dimension(180, 50));
		
		question[numOfQuestion - 1] = Questions(rangeOfQuestion);
		key[numOfItem - 1] = keys;
		lblQuestion = new JLabel(question[numOfQuestion - 1]);
		Font questionFont = new Font("", Font.BOLD, 60);
		lblQuestion.setFont(questionFont);
		lblQuestion.setPreferredSize(new Dimension(360, 60));
		
		Answer = new JTextField();
		Answer.setPreferredSize(new Dimension(50, 30));
		
		NextQuestion = new JButton("下一题");
		FrontQuestion = new JButton("上一题");
		Complete = new JButton("完成");
		NextQuestion.setPreferredSize(new Dimension(130, 30));
		FrontQuestion.setPreferredSize(new Dimension(130, 30));
		Complete.setPreferredSize(new Dimension(130, 30));
		FrontQuestion.setEnabled(false);
		//相应按键监听
		NextQuestion.addActionListener(new ActionListener(){//下个问题
			public void actionPerformed(ActionEvent e){
				if(numOfItem == numOfQuestion){
					answer[numOfItem - 1] = Answer.getText();
					numOfItem ++;
					numOfQuestion ++;
					lblNum.setText("第"+ numOfItem +"题");
					Answer.setText("");
					question[numOfQuestion - 1] = Questions(rangeOfQuestion);
					lblQuestion.setText(question[numOfQuestion - 1]);
					key[numOfItem - 1] = keys;
				}
				else{
					answer[numOfItem - 1] = Answer.getText();
					numOfItem ++;
					lblNum.setText("第"+ numOfItem +"题");
					lblQuestion.setText(question[numOfItem - 1]);
					Answer.setText(answer[numOfItem - 1]);
				}
				if(numOfItem != 1) FrontQuestion.setEnabled(true);
				if(numOfItem == sumOfQuestion) NextQuestion.setEnabled(false);
			}
		});
		FrontQuestion.addActionListener(new ActionListener(){//前一个问题
			public void actionPerformed(ActionEvent e){
				NextQuestion.setEnabled(true);
				answer[numOfItem - 1] = Answer.getText();
			    numOfItem --;
				lblNum.setText("第"+ numOfItem +"题");
				lblQuestion.setText(question[numOfItem - 1]);
				Answer.setText(answer[numOfItem - 1]);
				if(numOfItem == 1) FrontQuestion.setEnabled(false);
			}
		});
		Complete.addActionListener(new ActionListener(){//完成
			public void actionPerformed(ActionEvent e){
				answer[numOfItem - 1] = Answer.getText();
				checklist();//调用方法得到分数等
			}
		});
		examMode.addWindowListener(new WindowAdapter(){//关闭窗口时的判断
			public void windowClosing(WindowEvent evt){
				exam.stop();
				examFocus = false;
				if(mMusic_BackMusic.getState()) audioClip.loop();
			}
		});
		
		JPanel Panel1 = new JPanel();
		JPanel Panel2 = new JPanel();
		JPanel Panel3 = new JPanel();
		
		Panel1.add(lblNum);
		Panel2.add(lblQuestion);
		Panel2.add(Answer);
		Panel3.add(NextQuestion);
		Panel3.add(FrontQuestion);
		Panel3.add(Complete);
		
		con.add(Panel1);
		con.add(Panel2);
		con.add(Panel3);
		
		examMode.setSize(440, 240);
		examMode.setResizable(false);//不可变大小
		examMode.setLocation(screenWidth/2-220,screenHeight/2-120);
		examMode.setVisible(true);
		examFocus = true;
		mSearch_History.setEnabled(false);
	}
	
	public void actionPerformed(ActionEvent e){//主窗口事件监听
		if(e.getSource() == mFile_NewTurn){//新开始时弹出选择窗口
			final JDialog choiceDialog = new JDialog(this, "模式选择", true);
			Container con = choiceDialog.getContentPane();
			con.setLayout(new FlowLayout(FlowLayout.LEFT));
			ButtonGroup bGroup = new ButtonGroup();
			final String[] numOfList = {"10","15","20","25","30","35","40","45","50","55","60","65",
					"70","75","80","85","90","95","100"};
			final String[] rangeOfList = {"10","20","40","50","100","200","500","1000","2000","5000"};
			JLabel lblNum = new JLabel("题目数量:");
			JLabel lblRange = new JLabel("计算范围:");
			lblNum.setPreferredSize(new Dimension(60, 22));
			lblRange.setPreferredSize(new Dimension(60, 22));
			final JRadioButton Training = new JRadioButton("训练模式");
			final JRadioButton Exam = new JRadioButton("测验模式");
			Training.setSelected(true);
			bGroup.add(Training);
			bGroup.add(Exam);
			final JComboBox numList = new JComboBox(numOfList);
			final JComboBox rangeList = new JComboBox(rangeOfList);
			numList.setSelectedIndex(0);
			rangeList.setSelectedIndex(1);
			JButton OK = new JButton("确定");
			JButton back = new JButton("返回");
			OK.setPreferredSize(new Dimension(110, 22));
			back.setPreferredSize(new Dimension(110, 22));
			numList.setPreferredSize(new Dimension(160, 22));
			rangeList.setPreferredSize(new Dimension(160, 22));
            //按键监听
			OK.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					audioClip.stop();
					int n = Integer.parseInt(numOfList[numList.getSelectedIndex()]);
					int r = Integer.parseInt(rangeOfList[rangeList.getSelectedIndex()]);
					if(Training.isSelected()){
						if(mMusic_BackMusic.getState()) exercise.loop();
						Trainmode(n,r);
						choiceDialog.dispose();
					}
					else{
						if(mMusic_BackMusic.getState()) exam.loop();
						Exammode(n,r);
						choiceDialog.dispose();
					}
				}
			});
			back.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					choiceDialog.dispose();
				}
			});
			
			JPanel bottomPanel = new JPanel();
			JPanel centerPanel1 = new JPanel();
			JPanel centerPanel2 = new JPanel();
			JPanel topPanel = new JPanel();
			JPanel mode = new JPanel();
			mode.setBorder(BorderFactory.createTitledBorder("模式 "));
			mode.add(Training);
			mode.add(Exam);
			mode.setPreferredSize(new Dimension(220, 60));
			
			topPanel.add(mode);
			centerPanel1.add(lblNum);
			centerPanel1.add(numList);
			centerPanel2.add(lblRange);
			centerPanel2.add(rangeList);
			bottomPanel.add(OK);
			bottomPanel.add(back);
			
			con.add(topPanel);
			con.add(centerPanel1);
			con.add(centerPanel2);
			con.add(bottomPanel);
			
			choiceDialog.setSize(250, 260);
			choiceDialog.setResizable(false);
			choiceDialog.setLocation(screenWidth/2-125,screenHeight/2-130);
			choiceDialog.setVisible(true);
			
			
		}
		else if(e.getSource() == mSearch_History){//显示历史记录
			showhistory();
			if(mMusic_BackMusic.getState()&&historyFocus){
				audioClip.stop();
				goodhistory.loop();
			}
		}
		else if(e.getSource() == mMusic_BackMusic){//音乐开关
			if(mMusic_BackMusic.getState()){
				if(examFocus) exam.loop();
				else if(exerciseFocus) exercise.loop();
				else audioClip.loop();
			}
			else{
				exam.stop();
				exercise.stop();
				goodhistory.stop();
				badhistory.stop();
				audioClip.stop();
			}
		}
		else if(e.getSource() == mHelp_HelpTopics){//作品帮助
			JOptionPane.showMessageDialog(this,"简单的加减法算术题,数学初学者适用", "帮助主题", JOptionPane.INFORMATION_MESSAGE);
		}
		else if(e.getSource() == mHelp_About){//作者关于
			JOptionPane.showMessageDialog(this,"小人物制作!作者:韦伟\n"+"软件3班 学号200730556438", "作品关于", JOptionPane.INFORMATION_MESSAGE);
		}
		else if(e.getSource() == mFile_Exit){//退出程序
			System.exit(0);
		}
	}
	
	public static void main(String[] args){
		new SimpleMaths();//实例化主窗口
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -