package com;

import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
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.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

/**
 * 随机出题
 * @author cyw
 *
 */
@SuppressWarnings("serial")
public class Rdom extends JFrame{
	Container c=getContentPane();
	JPanel jp1,jp2,jp3,jp4,jp5,jp6,jp7;
	Random r=new Random();
	ButtonGroup bg=new ButtonGroup();
	JTextField one,second,result;
	JRadioButton jia,jian,cheng,chu;
	JButton chuti,check,answer,alert,clear,Xd;
	JLabel deng;
	JLabel jiu=new JLabel();
	JLabel x=new JLabel();
	JTextArea jta=new JTextArea(8, 25);
	JScrollPane js=new JScrollPane(jta);
	int i=100;
	int b=10;
	//菜单栏 
	JMenuBar mb=new JMenuBar();
	JMenu function=new JMenu("功能(G)");
	JMenuItem clock=new JMenuItem("解锁");
	JMenu introduce=new JMenu("说明(I)");
	JMenuItem address=new JMenuItem("保存路径说明");
	JMenuItem clockintroduce=new JMenuItem("“解锁”说明");
	JMenuItem checkintroduce=new JMenuItem("“提交答案”说明");
	JMenuItem chutiintroduce=new JMenuItem("“出题”说明");
	JMenuItem answerintroduce=new JMenuItem("“查看答案”说明");
	JMenuItem alertintroduce=new JMenuItem("“修改出题范围”说明");
	JMenuItem clearintroduce=new JMenuItem("“清除”说明");
	JMenuItem xdintroduce=new JMenuItem("“小数点位数”说明");
	JMenu about=new JMenu("关于(E)");
	JMenu editor=new JMenu("作者:CYW");
	JTextField qq=new JTextField("QQ:756398953");
	JTextField Wechat=new JTextField("微信:wave756398953");
	JLabel e=new JLabel("                      作者:CYW");
	int t=0;
	public Rdom(String s){
		super(s);
		this.setBounds(500, 120, 303, 487);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setResizable(false);
		menu();
		init();
		images();
		this.setVisible(true);
	}

	private void images() {
		//创建图标
		try {
			this.setIconImage(ImageIO.read(new File("images\\1.jpg")));
		} catch (IOException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
	}

	/**标题栏部署*/
	private void menu() {
		mb.add(function);
		mb.add(introduce);
		mb.add(about);
		mb.add(e);
		setJMenuBar(mb);
		function.add(clock);
		function.setMnemonic('G');
		introduce.add(address);
		introduce.setMnemonic('I');
		address.setBackground(Color.red);
		address.setForeground(Color.yellow);
		introduce.add(clockintroduce);
		introduce.addSeparator();
		introduce.add(checkintroduce);
		introduce.add(chutiintroduce);
		introduce.add(answerintroduce);
		introduce.add(alertintroduce);
		introduce.add(clearintroduce);
		introduce.add(xdintroduce);
		about.add(editor);
		about.setMnemonic('E');
		editor.add(qq);
		editor.addSeparator();
		editor.add(Wechat);
		qq.setEditable(false);
		Wechat.setEditable(false);
		qq.setBorder(BorderFactory.createEtchedBorder( Color.cyan, Color.pink));
		Wechat.setBorder(BorderFactory.createEtchedBorder( Color.cyan, Color.pink));
		qq.setBackground(Color.cyan);
		Wechat.setBackground(Color.pink);
		
		//添加监听器
		MenuAction ma=new MenuAction();
		clock.addActionListener(ma);
		clockintroduce.addActionListener(ma);
		address.addActionListener(ma);
		checkintroduce.addActionListener(ma);
		chutiintroduce.addActionListener(ma);
		answerintroduce.addActionListener(ma);
		alertintroduce.addActionListener(ma);
		clearintroduce.addActionListener(ma);
		xdintroduce.addActionListener(ma);
		
		//作者标志通过线程修改颜色
		color c=new color();
		c.start();
	}

	/**界面部署*/
	private void init() {
		c.setLayout(null);//取消布局
		jp1=new JPanel(new FlowLayout());
		jp1.setBounds(20, 20, 250, 30);
		jp2=new JPanel(new FlowLayout());
		jp2.setBounds(20, 50, 250, 30);
		jp3=new JPanel(new FlowLayout());
		jp3.setBounds(20, 80, 250, 30);
		jp4=new JPanel(new FlowLayout());
		jp4.setBounds(20, 110, 250, 20);
		jp5=new JPanel(new FlowLayout());
		jp5.setBounds(20, 130, 250, 30);
		jp6=new JPanel(new FlowLayout());
		jp6.setBounds(0, 170, 300, 70);
		jp7=new JPanel(new FlowLayout());
		jp7.setBounds(0, 250, 300, 250);
	    one=new JTextField(12);
	    jia=new JRadioButton("+",true);
	    jian=new JRadioButton("-");
	    cheng=new JRadioButton("×");
	    chu=new JRadioButton("÷");
	    second=new JTextField(12);
	    deng=new JLabel("=");
	    result=new JTextField(12);
	    chuti=new JButton("出题");
	    check=new JButton("提交答案");
	    answer=new JButton("查看答案");
	    alert=new JButton("修改出题范围");
	    clear=new JButton("清除");
	    //按钮监听
	    clear.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				one.setText("");
				second.setText("");
				result.setText("");
				result.requestFocus();
			}
		});
	    Xd=new JButton("小数点位数");
	    Xd.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				try {
					int c=0;
					while(c<=(b-1)){
						c++;
					}
					String ab=c+"";
					int a1=0;
					while(a1<=(ab.length()-2)){
						a1++;
					}
					Integer i=new Integer(JOptionPane.showInputDialog(null,"现小数点后保留"+a1+"位数,改为:","小数点",JOptionPane.PLAIN_MESSAGE));
					x.setText("1");
					for(int j=0;j<=(i-1);j++){
						String a="0";
						x.setText(x.getText()+a);
					}
					Integer i1=new Integer(x.getText());
					b=i1;
					String ac=b+"";
					int a2=0;
					while(a2<=(ac.length()-2)){
						a2++;
					}
					jta.setText(jta.getText()+"\n已改为小数点后保留"+a2+"位小数:");
				} catch (NumberFormatException e2) {
				}
				result.requestFocus();
				 if(b==1)
					 i=i+1;
				 else
					 i=i-1;
			}
		});
	    jp1.add(one);
	    jp2.add(jia);
	    jp2.add(jian);
	    jp2.add(cheng);
	    jp2.add(chu);
	    jp3.add(second);
	    jp4.add(deng);
	    jp5.add(result);
	    bg.add(jia);
	    bg.add(jian);
	    bg.add(cheng);
	    bg.add(chu);
	    c.add(jp1);
	    c.add(jp2);
	    c.add(jp3);
	    c.add(jp4);
	    c.add(jp5);
	    c.add(jp6);
	    jp6.add(check);
	    jp6.add(chuti);
	    jp6.add(answer);
	    jp6.add(alert);
	    jp6.add(clear);
	    jp6.add(Xd);
	    check.addActionListener(new A());
	    answer.addActionListener(new A());
	    chuti.addActionListener(new B());
	    alert.addActionListener(new B());
	    one.addKeyListener(new key());
	    second.addKeyListener(new key());
	    result.addKeyListener(new key());
	    one.setHorizontalAlignment(JTextField.CENTER);
	    second.setHorizontalAlignment(JTextField.CENTER);
	    result.setHorizontalAlignment(JTextField.CENTER);
	    B b=new B();
	    one.addActionListener(b);
	    second.addActionListener(b);
	    jia.addActionListener(b);
	    jian.addActionListener(b);
	    cheng.addActionListener(b);
	    chu.addActionListener(b);
	    jp7.add(js);
	    c.add(jp7);
	    one.setEditable(false);
	    second.setEditable(false);
	    jta.setEditable(false);
	    jta.setBorder(BorderFactory.createTitledBorder("出题记录文本域"));
	    addWindowListener(new C());
	}
	class A implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			if(e.getSource()==check){
				if(result.getText().equals("")){
					JOptionPane.showMessageDialog(null, "请输入答案");
				}else{
					double re=Double.parseDouble(result.getText());
					double o=Double.parseDouble(one.getText());
					double s=Double.parseDouble(second.getText());
					BigDecimal i= new BigDecimal(Double.toString(o));//使小数点运算更精密
					BigDecimal j= new BigDecimal(Double.toString(s));//使小数点运算更精密
					double res=0;
					if(jia.isSelected()){
						res=i.add(j).doubleValue();
					}
					if(jian.isSelected()){
						res=i.subtract(j).doubleValue();
					}
					if(cheng.isSelected()){
						res=i.multiply(j).doubleValue();
					}
					if(chu.isSelected()){
						if(s==0){
							JOptionPane.showMessageDialog(null, "分母不可为0!!!","警告",JOptionPane.ERROR_MESSAGE);
							jta.setText(jta.getText()+"该题智障(分母不可为0)");
						}
						else{
							try {
								res=i.divide(j).doubleValue();
							} catch (ArithmeticException e2) {
								JOptionPane.showMessageDialog(null, "不做计算");
								jta.setText(jta.getText()+"该题不做计算");
							}
						}
					}
					if(re==res){
						JOptionPane.showMessageDialog(null, "正确","正确",JOptionPane.PLAIN_MESSAGE);
						jta.setText(jta.getText()+result.getText()+"   正确");
					}else{
						JOptionPane.showMessageDialog(null, "错误","错误",JOptionPane.ERROR_MESSAGE);
						jta.setText(jta.getText()+result.getText()+"   错误       ");
						result.setText("");
				    }
			}
			}
			if(e.getSource()==answer){
				double o=Double.parseDouble(one.getText());
				double s=Double.parseDouble(second.getText());
				BigDecimal i= new BigDecimal(Double.toString(o));//使小数点运算更精密
				BigDecimal j= new BigDecimal(Double.toString(s));//使小数点运算更精密
				double res=0;
				if(jia.isSelected()){
					res=i.add(j).doubleValue();
					JOptionPane.showMessageDialog(null, "<html><font size=25>答案:"+res+"</font></html>");
					jta.setText(jta.getText()+" 查过答案!  ");
				}
				if(jian.isSelected()){
					res=i.subtract(j).doubleValue();
					JOptionPane.showMessageDialog(null, "<html><font size=25>答案:"+res+"</font></html>");
					jta.setText(jta.getText()+" 查过答案!  ");
				}
				if(cheng.isSelected()){
					res=i.multiply(j).doubleValue();
					JOptionPane.showMessageDialog(null, "<html><font size=25>答案:"+res+"</font></html>");
					jta.setText(jta.getText()+" 查过答案!  ");
				}
				if(chu.isSelected()){
					if(s==0){
						JOptionPane.showMessageDialog(null, "分母不可为0!!!","警告",JOptionPane.ERROR_MESSAGE);
						jta.setText(jta.getText()+"该题智障(分母不可为0)");
					}
					else{
						try {
							res=i.divide(j).doubleValue();
							JOptionPane.showMessageDialog(null, "<html><font size=25>答案:"+res+"</font></html>");
							jta.setText(jta.getText()+" 查过答案!  ");
						} catch (ArithmeticException e2) {
							JOptionPane.showMessageDialog(null, "不做计算");
							jta.setText(jta.getText()+"该题不做计算");
						}
					}
				}
			}
			result.requestFocus();
		}
	}
	class B implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			if(e.getSource()==alert){
				result.requestFocus();
				String a="";
				try {
					if(b==1){
						a=JOptionPane.showInputDialog(null,"输入出题范围(现为0~"+(i-1)+",最大随机数:"+(i-1)+"):\n0~","输入",JOptionPane.PLAIN_MESSAGE);
						Integer j=new Integer(a);
						i=j+1;
						jta.setText(jta.getText()+"\n"+"修改出题范围(现为0~"+(i-1)+"):");
					}else{
						a=JOptionPane.showInputDialog(null,"输入出题范围(现为0~"+i+",最大随机数:"+(i-1)+"."+(b-1)+"):\n0~","输入",JOptionPane.PLAIN_MESSAGE);
						Integer j=new Integer(a);
						i=j;
						jta.setText(jta.getText()+"\n"+"修改出题范围(现为0~"+i+"):"); 
					}
				} catch (NumberFormatException e2) {
				}
				
			}
			 if(e.getSource()==chuti){
				 if(b==1){
					 one.setText(r.nextInt(i)+"");
					 second.setText(r.nextInt(i)+"");
				 }else{
					 one.setText(r.nextInt(i)+"."+r.nextInt(b));
					 second.setText(r.nextInt(i)+"."+r.nextInt(b));
				 }
				result.setText("");
				if(jia.isSelected()){
					jta.setText(jta.getText()+"\n"+one.getText()+jia.getText()+second.getText()+"=");
				}else if(jian.isSelected()){
					jta.setText(jta.getText()+"\n"+one.getText()+jian.getText()+second.getText()+"=");
				}else if(cheng.isSelected()){
					jta.setText(jta.getText()+"\n"+one.getText()+cheng.getText()+second.getText()+"=");
				}else if(chu.isSelected()){
					jta.setText(jta.getText()+"\n"+one.getText()+chu.getText()+second.getText()+"=");
				}
				result.requestFocus();
			}
			 if(e.getSource()==one){
				 second.requestFocus();
			 }else if(e.getSource()==jia){
				 second.requestFocus();
				 one.setText("");
				 second.setText("");
				 jta.setText(jta.getText()+"\n修改运算符号:"+jia.getText());
			   	 result.setText("");
			 }else if(e.getSource()==jian){
				 second.requestFocus();
				 one.setText("");
				 second.setText("");
				 jta.setText(jta.getText()+"\n修改运算符号:"+jian.getText());
			   	 result.setText("");
			 }else if(e.getSource()==cheng){
				 second.requestFocus();
				 one.setText("");
				 second.setText("");
				 jta.setText(jta.getText()+"\n修改运算符号:"+cheng.getText());
			   	 result.setText("");
			 }else if(e.getSource()==chu){
				 second.requestFocus();
				 one.setText("");
				 second.setText("");
				 jta.setText(jta.getText()+"\n修改运算符号:"+chu.getText());
			   	 result.setText("");
			 }else if(e.getSource()==second){
				 result.requestFocus();
			 }
			
		}
		
	}
	//限制文本域只可输入数字
		class key extends KeyAdapter{
		public void keyTyped(KeyEvent e) {
			int s=(e.getKeyChar());
			if(!(s>=KeyEvent.VK_0&&s<=KeyEvent.VK_9||s==KeyEvent.VK_PERIOD||s==KeyEvent.VK_MINUS))
				e.consume();
			if(e.getSource()==result){
				if(s==KeyEvent.VK_ENTER){
					 check.doClick();
				}
			}
			if(s==KeyEvent.VK_SPACE)
				chuti.doClick();
		}
	}
	class C extends WindowAdapter{
		 public void windowOpened(WindowEvent e) {
					Date time=new Date();
					DateFormat date=new SimpleDateFormat("YYYY年MM月dd日 HH:mm:ss");
					String s1=date.format(time);
					jta.setText("----------------"+s1+"-----------------");
				}
		 public void windowClosing(java.awt.event.WindowEvent arg0){
			   BufferedReader br;
			try {
				br = new BufferedReader(new FileReader("D:\\出题.txt"));
				String s="";
				while((s=br.readLine())!=null){
					jiu.setText(jiu.getText()+s+"\n");
				}
				br.close();
			} catch (FileNotFoundException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			} catch (IOException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
			 try {
				 //保存记录
				BufferedWriter bw=new BufferedWriter(new FileWriter("D:\\出题.txt"));
				bw.write(jiu.getText().replaceAll("\n", "\r\n")+jta.getText().replaceAll("\n", "\r\n"));
				if(bw!=null){
					bw.close();
				}
			} catch (IOException e1) {
			}
		 }
	}
	class MenuAction implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
				if(e.getSource().equals(clock)){
					if(clock.getText().equals("解锁")){
						one.setEditable(true);
						second.setEditable(true);
						one.setText("");
						second.setText("");
						result.setText("");
						clock.setText("上锁");
						one.requestFocus();
					}else{
						one.setEditable(false);
						second.setEditable(false);
						one.setText("");
						second.setText("");
						result.setText("");
						clock.setText("解锁");
						result.requestFocus();
					}
				}else if(e.getSource()==clockintroduce){
					JOptionPane.showMessageDialog(null, "<html><font size=6><center>“解锁”</center></font>"
							+ "<p>该功能可将不可编辑的文本框变为可以编辑,并将清空文本框的数,以方便用户做简单计算。</p>"
							+ "<p>但编辑内容不记录在文本中!!!</p></html>","“解锁”",JOptionPane.PLAIN_MESSAGE);
				}else if(e.getSource()==address){
					JOptionPane.showMessageDialog(null, "<html><font size=6><center>保存路径</center></font>"
							+ "当您关闭程序后,文本域中的所有信息将保存到</p><p><font color=red >D:\\出题.txt</font>。</p></html>"
						,"保存路径",JOptionPane.PLAIN_MESSAGE);
				}else if(e.getSource()==checkintroduce){
					JOptionPane.showMessageDialog(null, 
							"<html><font size=6><center>“提交答案”</center></font>"
							+ "凡是点了“提交答案”,可对用户所做的答案进行判断(正确或错误),并记录到文本域中,并将保存于</p><p><font color=red >D:\\出题.txt</font>。</p></html>"
						,"“提交答案”",JOptionPane.PLAIN_MESSAGE);
				}else if(e.getSource()==chutiintroduce){
					JOptionPane.showMessageDialog(null, 
							"<html><font size=6><center>“出题”</center></font>"
							+ "凡是点了“出题”,可得到随机题目,并记录到文本域中,并将保存于</p><p><font color=red >D:\\出题.txt</font>。</p></html>"
						,"“出题”",JOptionPane.PLAIN_MESSAGE);
				}else if(e.getSource()==answerintroduce){
					JOptionPane.showMessageDialog(null, 
							"<html><font size=6><center>“查看答案”</center></font>"
							+ "凡是点了“查看答案”,可得到随机题目或用户输入的数目计算的大难,并在文本域中显示“查过答案”字眼,并将保存于</p><p><font color=red >D:\\出题.txt</font>。</p></html>"
						,"“查看答案”",JOptionPane.PLAIN_MESSAGE);
				}else if(e.getSource()==alertintroduce){
					JOptionPane.showMessageDialog(null, 
							"<html><font size=6><center>“修改出题范围”</center></font>"
							+ "凡是点了“修改出题范围”,可修改随机出题的范围大小,默认为0~99.9,并将修改内容记录到文本域中,且将保存于</p><p><font color=red >D:\\出题.txt</font>。</p></html>"
						,"“修改出题范围”",JOptionPane.PLAIN_MESSAGE);
				}else if(e.getSource()==clearintroduce){
					JOptionPane.showMessageDialog(null, 
							"<html><font size=6><center>“清除”</center></font>"
							+ "凡是点了“清除”,可清除文本框内所有内容,不记录到文本域中。</p></html>"
						,"“清除”",JOptionPane.PLAIN_MESSAGE);
				}else if(e.getSource()==xdintroduce){
					JOptionPane.showMessageDialog(null, 
							"<html><font size=6><center>“小数点位数”</center></font>"
							+ "凡是点了“小数点位数”,可修改随机出题的小数点保留位数,默认为0.1,并将修改内容记录到文本域中,且将保存于</p><p><font color=red >D:\\出题.txt</font>。</p></html>"
						,"“小数点位数”",JOptionPane.PLAIN_MESSAGE);
				}
		}
		
	}
	public static void main(String[] args) {
		new Rdom("随机出题&简单计算!");
	}
	class color extends Thread{
		@Override
		public void run() {
			while (true) {
				if (t == 0)
					e.setForeground(Color.red);
				else if (t == 1)
					e.setForeground(Color.pink);
				else if (t == 2)
					e.setForeground(Color.blue);
				else if (t == 3)
					e.setForeground(Color.cyan);
				else if (t == 4)
					e.setForeground(Color.green);
				try {
					Thread.sleep(500);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				t++;
				t = t % 5;
			}

		}
	}

}
最近下载更多
weishen  LV1 2022年11月28日
153251174  LV1 2022年6月29日
Demo1111  LV30 2021年12月12日
zzzzxff  LV1 2021年9月30日
Cyprus  LV1 2021年5月24日
whynot  LV1 2021年4月10日
suger1  LV1 2020年12月13日
网名多了记不住  LV3 2020年12月8日
1736133424  LV1 2020年12月6日
william8090  LV1 2020年7月2日
最近浏览更多
sswert  LV2 9月12日
lshlsh 2023年12月25日
暂无贡献等级
xingxing1234  LV10 2023年3月22日
weishen  LV1 2022年11月28日
陈小灏  LV15 2022年7月4日
153251174  LV1 2022年6月29日
wyf123wyf 2022年6月27日
暂无贡献等级
liys1234  LV9 2022年4月22日
年年有风  LV1 2022年3月7日
Demo1111  LV30 2021年12月12日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友