package rjgz.suanTi; import rjgz.BinaryOperation; import rjgz.Exercise; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import static rjgz.suanTi.JDBC.add; import static rjgz.suanTi.JDBC.selectByTiKu; public class testFile { public static void main(String[] args) { // 创建一个顶层容器 JFrame JFrame frame = new JFrame("Swing Example"); // 设置窗口的大小和位置 frame.setBounds(100, 100, 900, 600); // 设置窗口关闭时的行为 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 创建一个面板 JPanel,并设置 BoxLayout 布局管理器 JPanel panel = new JPanel(); panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); Exercise exercise=shenChen(); int len=exercise.list.size()/5; // 添加文本框和输入框 JTextField[][] textFields = new JTextField[exercise.list.size()][5]; JLabel[][] labels=new JLabel[exercise.list.size()][5]; for (int i = 0; i < len; i++) { JPanel rowPanel = new JPanel(new FlowLayout(FlowLayout.LEFT)); for (int j = 0; j < 5; j++) { if (j != 4) { BinaryOperation b = exercise.list.get(i * 5 + j); String equation = b.getLeft_operand() + " " + b.getOperator() + " " + b.getRight_operand() + " " + b.getOperator2() + " " + b.getThird_operand() + "="; JLabel label = new JLabel(equation); rowPanel.add(label); labels[i][j]=label; JTextField textField = new JTextField(10); rowPanel.add(textField); textFields[i][j] = textField; } else { JLabel label = new JLabel("第" + (i + 1) + "行:"); rowPanel.add(label); } } panel.add(rowPanel); } JPanel rowPanel3 = new JPanel(new FlowLayout(FlowLayout.LEFT)); JLabel jLabel=new JLabel("正确率为"+""+"%"); rowPanel3.add(jLabel); JLabel TiKu=new JLabel("输入题库名"); rowPanel3.add(TiKu); JTextField ShuRu=new JTextField(); ShuRu.setColumns(10); rowPanel3.add(ShuRu); JButton extract= new JButton("刷题库"); rowPanel3.add(extract); panel.add(rowPanel3); // 创建一个按钮 JButton,用于刷新数据 JPanel rowPanel2 = new JPanel(new FlowLayout(FlowLayout.LEFT)); JButton refreshButton = new JButton("刷新"); rowPanel2.add(refreshButton); JButton submitButton= new JButton("提交"); rowPanel2.add(submitButton); JButton deposit= new JButton("存入题库"); rowPanel2.add(deposit); panel.add(rowPanel2); // 为按钮添加事件监听器 refreshButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { Exercise exercise=shenChen(); // 修改文本框中的内容 for (int i = 0; i < len; i++) { for (int j = 0; j < 5; j++) { if (j != 4) { JTextField inputField = textFields[i][j]; inputField.setText(""); BinaryOperation b = exercise.list.get(i * 5 + j); String equation = b.getLeft_operand() + " " + b.getOperator() + " " + b.getRight_operand() + " " + b.getOperator2() + " " + b.getThird_operand() + "="; labels[i][j].setText(equation); } } } } }); submitButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { for (int i = 0; i < len; i++) { for (int j = 0; j < 5; j++) { if (j != 4) { JTextField inputField = textFields[i][j]; int UserInput= Integer.parseInt(inputField.getText()); exercise.list.get(i*5+j).setUserValue(UserInput); } } } jLabel.setText("正确率为: "+exercise.ReturnAccuracy()*100+"%"); } }); deposit.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { for (BinaryOperation b: exercise.list) { add(b); } } }); extract.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { int UserTiKu= Integer.parseInt(ShuRu.getText()); exercise.list=selectByTiKu(UserTiKu); // 修改文本框中的内容 for (int i = 0; i < len; i++) { for (int j = 0; j < 5; j++) { if (j != 4) { JTextField inputField = textFields[i][j]; inputField.setText(""); BinaryOperation b = exercise.list.get(i * 5 + j); String equation = b.getLeft_operand() + " " + b.getOperator() + " " + b.getRight_operand() + " " + b.getOperator2() + " " + b.getThird_operand() + "="; labels[i][j].setText(equation); } } } } }); // 将面板添加到窗口中 frame.getContentPane().add(panel); // 显示窗口 frame.setVisible(true); } public static Exercise shenChen(){ int fanWei=1; int fanWei2=5; Exercise exercise=new Exercise(); for (int i = 0; i < 10; i++) { BinaryOperation b=new BinaryOperation(fanWei2,fanWei); while (!exercise.Check(b)&&b.getValue()<0&&b.getValue()>100){ b=new BinaryOperation(fanWei2,fanWei); } exercise.list.add(b); } return exercise; } }
标签:11,int,list,28,add,2023,new,JLabel,exercise From: https://www.cnblogs.com/wllovelmbforever/p/17862853.html