标签:界面 you ted 字体 equals snippet ane string lis
package operations_GUI; import java.awt.BorderLayout; import java.awt.Color; import java.awt.FlowLayout; 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.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextField; import operations.Oper; public class Snippet { static JFrame jf = new JFrame();//主界面 static JPanel pan1 = new JPanel();//主界面最上方面板 static JPanel pan2 = new JPanel();//主界面中间面板 static JPanel pan3 = new JPanel();//主界面最下方面板 static String str = "";//存储表达式 static JTextField[] jTextFields = new JTextField[10];//存放表达式的文本框的数组 static int i = 0;//用于记录写了几道题,十题为一组 static int right = 0;//用于记录写对几道题 public static void main(String[] args) { jf.setSize(800, 600); jf.setLayout(new BorderLayout()); jf.setLocationRelativeTo(null); pan1.setBackground(Color.WHITE); pan1.setSize(800, 150); pan1.setLayout(new FlowLayout()); JTextField jtf = new JTextField(20);//用于存储表达式 str = Oper.exercises();//将表达式赋值给str jtf.setText(str); jtf.setSize(400, 100); jtf.setEditable(false); jtf.setLocation(300, 25); jtf.setFont(new Font("微软雅黑", 1, 20)); pan1.add(jtf); JTextField jtf_result = new JTextField(5);//用于输入计算结果 jtf_result.setFont(new Font("微软雅黑", 1, 20)); pan1.add(jtf_result); JButton jb = new JButton("下一题");//按钮,生成下一个表达式 pan1.add(jb); pan2.setLayout(new GridLayout(5, 2)); for (int i = 0; i < 10; i++) { jTextFields[i] = new JTextField(); jTextFields[i].setFont(new Font("微软雅黑", 1, 20));//设置文本框字体 jTextFields[i].setEditable(false);//设置文本框不可编辑 pan2.add(jTextFields[i]);//循环生成文本框,并将文本框,并将文本框添加到中间面板中 } JTextField jt = new JTextField(40);//用于显示做题结果的文本框 jt.setFont(new Font("微软雅黑", 1, 15)); jt.setEditable(false);//设置文本框不可编辑 JButton reset = new JButton("再做一遍");//用于清空所有信息,重新做题 pan3.add(jt); pan3.add(reset); jb.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { if (jtf_result.getText().equals("")) { jTextFields[i].setText(str + jtf_result.getText() + " × 正确结果为:" + Oper.res()); } else if (Oper.res() == Double.parseDouble(jtf_result.getText())) { jTextFields[i].setText(str + jtf_result.getText() + " √"); right++; } else { jTextFields[i].setText(str + jtf_result.getText() + " × 正确结果为:" + Oper.res()); } i++; str = Oper.exercises(); jtf.setText(str); jtf_result.setText(""); jtf_result.requestFocus(); if (i == 10) { jb.setEnabled(false); jt.setText("你答对了" + right + "道题,答错了" + (10 - right) + "道题,正确率为:" + (right / 10.0)); if (right >= 8) { JOptionPane.showMessageDialog(null, "恭喜你,答对了" + right + "道题"); } } } }); reset.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { str = Oper.exercises(); jtf.setText(str); for (int i = 0; i < 10; i++) { jTextFields[i].setText(""); } jt.setText(""); i = 0; right = 0; jb.setEnabled(true); } }); jf.add(pan1, BorderLayout.NORTH); jf.add(pan2, BorderLayout.CENTER); jf.add(pan3, BorderLayout.SOUTH); jf.setVisible(true); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }
package operations; import java.text.DecimalFormat; import java.util.Random; public class Oper { static char[] cha = { ‘+‘, ‘-‘, ‘*‘, ‘/‘ };// 加减乘除数组 static Random random = new Random(); static double num1 = random.nextInt(100); static double num2 = random.nextInt(100); static char c = cha[random.nextInt(4)]; public static String exercises() { num1 = random.nextInt(100); num2 = random.nextInt(100); c = cha[random.nextInt(4)]; if (c == ‘/‘) { while (num2 == 0) { num2 = random.nextInt(100); } return (int) num1 + "" + c + "" + (int) num2 + "="; } return (int) num1 + "" + c + "" + (int) num2 + "="; } public static Double res() { double result = 0; switch (c) { case ‘+‘: result = num1 + num2; break; case ‘-‘: result = num1 - num2; break; case ‘*‘: result = num1 * num2; break; case ‘/‘: result = num1 / num2; break; default: break; } DecimalFormat df = new DecimalFormat("#.#");//除法运算小数位过多时保留一位小数 result = Double.valueOf(df.format(result));// 转换为一位小数后表达式会转变成字符串,将其转变为double类型 return result; } }
标签:界面 you ted 字体 equals snippet ane string lis
原文地址:https://www.cnblogs.com/bladepoint/p/14831880.html