做这个软件的时候 ,我应该从用户的角度考虑问题,而不是简单实现了老师交代的功能。 于是我把功能的选择变成了年级的选择 ,
选择哪一个年级的,然后填写出题的数量,按下出题按钮,然后在下面的文本框中就会出现对应年级的难度的题目,这样简化的用户的选择。
开始的时候我将数字生成单独写在一个类里边的,但是后来做界面的时候,本想调用实例化对象,但是出现了线程的问题,尝试了解决方法但是没有奏效
于是我又将生成数字的方法直接写在一个类里边直接就是利用这一个形成了出题的界面和处理。
当然本软件是为小学生准备的 于是我去百度了小学各个年级数学题出题范围 ,于是发现自己写的方法没有调用上,是因为小学难度还没有达到那个水平 ,但是
方法我已经写出了,如果要用的话,到时候可以调用就可以了。
源代码如下:
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import java.util.Random;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class Jiemian extends JFrame{
public JPanel Panel1;
public JLabel graselect;//年纪选择
public JLabel questnum;//题目数量
public ButtonGroup buttonGroup;
public JTextField textnum;//添加题目数量
public JButton chuti;
public JButton chongzhi;
public JTextArea quest;//出题的地方
public JRadioButton gradetwo;
public JRadioButton gradethree;
public JRadioButton gradefour;
public JRadioButton gradefive;
public static void main(String[] args) {
try{
Jiemian jm=new Jiemian();
jm.setVisible(true);
}
catch(Exception e){
e.printStackTrace();
}
}
public Jiemian(){
super();
setTitle("随机出题系统");
setBounds(0,0,400,500);
getContentPane().setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//年级选择
graselect=new JLabel();
graselect.setBounds(0, 30, 50, 30);
graselect.setText("年级:");
getContentPane().add(graselect);
//二年级
buttonGroup=new ButtonGroup();
gradetwo =new JRadioButton();
buttonGroup.add(gradetwo);
gradetwo.setText("二年级");
gradetwo.setBounds(60, 30, 80, 30);
gradetwo.setSelected(true);
getContentPane().add(gradetwo);
//三年级
gradethree =new JRadioButton();
buttonGroup.add(gradethree);
gradethree.setText("三年级");
gradethree.setBounds(140, 30, 80, 30);
//gradetwo.setSelected(false);
getContentPane().add(gradethree);
//四年级
gradefour =new JRadioButton();
buttonGroup.add(gradefour);
gradefour.setText("四年级");
gradefour.setBounds(220, 30, 80, 30);
//gradetwo.setSelected(false);
getContentPane().add(gradefour);
//五年级
gradefive =new JRadioButton();
buttonGroup.add(gradefive);
gradefive.setText("五年级");
gradefive.setBounds(300, 30, 80, 30);
//gradetwo.setSelected(false);
getContentPane().add(gradefive);
questnum=new JLabel();
questnum.setText("题目数量:");
questnum.setBounds(0, 80, 80, 40);
getContentPane().add(questnum);
//题目数量
textnum=new JTextField();
textnum.setBounds(90,90, 80, 20);
getContentPane().add(textnum);
//出题的按钮
chuti= new JButton();
chuti.setText("出题");
chuti.setBounds(60, 150, 60, 30);
chuti.addActionListener( new chutiListener());
getContentPane().add(chuti);
//重置的按钮
chongzhi =new JButton();
chongzhi.setText("重置");
chongzhi.setBounds(240, 150, 60, 30);
chongzhi.addActionListener(new chongzhiListener());
getContentPane().add(chongzhi);
//出题的方框
quest=new JTextArea();
quest.setColumns(15);
quest.setRows(3);
quest.setLineWrap(true);
JScrollPane scrollPane=new JScrollPane();
scrollPane.setViewportView(quest);
Dimension dime=quest.getPreferredSize();
scrollPane.setBounds(0, 200, 400, 200);
getContentPane().add(scrollPane);
}
//生成数的方法
public int shenchengnum(int a){
Random rand=new Random();
int k=0;
int j=0;
for(int i=0;i<a;i++)
{
j=rand.nextInt(9)+1;
k=k*10+j;
}
return k;
}
public char shengchenchar(boolean zhfu){
Random rand=new Random();
char c=' ';
if(zhfu){
int j=rand.nextInt()%2;
if(j==0)
{
c='-';
}
}
return c;
}
public void fenshunum(int a,int b,int c){
a=shenchengnum(c);
b=shenchengnum(c);
}
public String xiaoshusc(){
Random rand=new Random();
DecimalFormat dcmFmt = new DecimalFormat("0.00");
float a=rand.nextFloat()*100;
String b=dcmFmt.format(a);
return b;
}
public char yunsuanchar(boolean szyf){
Random rand=new Random();
int j=0;
char c=' ';
if(szyf){
j=rand.nextInt(2);
if(j==0){
c='/';
} else if(j==1){
c='*';
}
}
else{
j=rand.nextInt()%2;
if(j==0){
c='+';
}
else {
c='-';
}
}
return c;
}
//生成数的方法
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String num=textnum.getText();
int qtnum=Integer.parseInt(num);
if(gradetwo.isSelected()){
for(int i=0;i<qtnum;i++){
Random rand=new Random();
int k=rand.nextInt()%2;
if(k==0){
String fnum=String.valueOf(shenchengnum(4));
String snum=String.valueOf(shenchengnum(4));
String ch=String.valueOf(yunsuanchar(false));
quest.append(fnum+" "+" "+ch+" "+snum+"=");
quest.append("
");
}else{
String fnum=String.valueOf(shenchengnum(2));
String snum=String.valueOf(shenchengnum(1));
String ch=String.valueOf(yunsuanchar(true));
quest.append(fnum+" "+" "+ch+" "+snum+"=");
quest.append("
");
}
}
}
else if(gradethree.isSelected()){
for(int i=0;i<qtnum;i++){
Random rand=new Random();
int k=rand.nextInt()%2;
if(k==0){
String fnum=String.valueOf(shenchengnum(4));
String snum=String.valueOf(shenchengnum(4));
String ch=String.valueOf(yunsuanchar(false));
quest.append(fnum+" "+" "+ch+" "+snum+"=");
quest.append("
");
}else{
String fnum=String.valueOf(shenchengnum(4));
String snum=String.valueOf(shenchengnum(1));
String ch=String.valueOf(yunsuanchar(true));
quest.append(fnum+" "+" "+ch+" "+snum+"=");
quest.append("
");
}
}
}else if(gradefour.isSelected()){
for(int i=0;i<qtnum;i++){
String fnum=String.valueOf(shenchengnum(3));
String snum=String.valueOf(shenchengnum(2));
String ch=String.valueOf(yunsuanchar(true));
quest.append(fnum+" "+" "+ch+" "+snum+"=");
quest.append("
");
}
}
else if (gradefive.isSelected()){
for(int i=0;i<qtnum;i++){
String fnum=xiaoshusc();
String snum=xiaoshusc();
String ch=String.valueOf(yunsuanchar(true));
quest.append(fnum+" "+" "+ch+" "+snum+"=");
quest.append("
");
}
}
}
} public class chongzhiListener implements ActionListener{
@Override public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
textnum.setText(null);
quest.setText(null);
}
}
}