首页>代码>java swing开发心理健康测评系统的设计与实现>/MentalHealth/src/GeneratingTestPaper.java
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumnModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

public class GeneratingTestPaper extends JFrame {
    private ArrayList<String[]> list = new ArrayList<>();
    private JTable table;
    private JLabel jLabel;
    private String name;
    private String fileName;
    private Container container;

    public GeneratingTestPaper(String name, Container container) throws IOException {
        this.name = name;
        this.container = container;
        setLayout(null);
        readBank();
        setTitle("生成试卷");
        setSize(1000, 500);
        setResizable(false);
        setLocationRelativeTo(getOwner());
        JScrollPane scrollPane = new JScrollPane();   //支持滚动
        scrollPane.setBounds(0, 0, 1000, 420);
        add(scrollPane);
        String[] columnNames = {"题目", "选项A", "分数", "选项B", "分数", "选项C", "分数", "选项D", "分数"};
        table = new JTable();   //自定义的表格
        DefaultTableModel defaultTable = (DefaultTableModel) table.getModel();
        for (String columnName : columnNames) {
            defaultTable.addColumn(columnName);
        }
        for (String[] lists : list) {
            defaultTable.addRow(lists);
        }
        adjustColumnWidth();
        scrollPane.setViewportView(table);
        JPanel buttonPanel = new JPanel(null);   //按钮面板
        add(buttonPanel);
        buttonPanel.setBounds(0, 420, 1000, 80);
        JButton selectAllButton = new JButton("全部选择");
        selectAllButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                table.selectAll();    //选中所有的行
            }
        });
        jLabel = new JLabel("友情提示!CTRL+鼠标左键单击可多选!");
        selectAllButton.setBounds(300, 5, 120, 30);
        buttonPanel.add(selectAllButton);
        JButton clearSelectionButton = new JButton("取消选择");
        clearSelectionButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                table.clearSelection();  //取消选择
            }
        });
        JButton determineGenerated = new JButton("确定生成");
        determineGenerated.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (table.getSelectedRowCount() > 20) {
                    JOptionPane.showMessageDialog(null, "最多选择20道题");
                } else {
                    if (createFile() && createTestPaper()) {
                        try {
                            writeTest();
                        } catch (IOException ioException) {
                            ioException.printStackTrace();
                        }
                        JOptionPane.showMessageDialog(null, "生成成功!");
                        dispose();
                        Examination.arrayList.clear();
                        Examination.names.clear();
                        container.setVisible(false);
                        new TeacherMenu(name);
                    } else {
                        JOptionPane.showMessageDialog(null, "生成失败!");
                    }
                }
            }
        });
        clearSelectionButton.setBounds(450, 5, 120, 30);
        buttonPanel.add(clearSelectionButton);
        jLabel.setForeground(Color.red);
        jLabel.setBounds(10, 5, 250, 30);
        buttonPanel.add(jLabel);
        determineGenerated.setBounds(600, 5, 120, 30);
        buttonPanel.add(determineGenerated);
    }

    public void readBank() throws IOException {
        FileInputStream fileInputStream = new FileInputStream("./lib/QuestionBank.txt");
        BufferedReader br = new BufferedReader(new InputStreamReader(fileInputStream));
        String line;
        while ((line = br.readLine()) != null) {
            String[] arr = line.split("\\s+");
            String[] a = new String[9];
            int j = 0;
            for (int i = 0; i < arr.length; i++) {
                if (i % 3 == 1) {
                    continue;
                } else {
                    a[j] = arr[i];
                    j++;
                }
            }
            list.add(a);
        }
        br.close();
        fileInputStream.close();
    }

    public boolean createFile() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
        Date date = new Date(System.currentTimeMillis());
        fileName = "心理健康测试_" + name + "_" + simpleDateFormat.format(date);
        File file = new File("./lib/Examination/" + fileName);
        if (!file.exists()) {
            if (file.mkdir()) {
                return true;
            }
        }
        return false;
    }

    public boolean createTestPaper() {
        File file = new File("./lib/TestPaper/" + fileName + ".txt");
        if (!file.exists()) {
            try {
                file.createNewFile();
                return true;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    public void writeTest() throws IOException {
        File file = new File("./lib/TestPaper/" + fileName + ".txt");
        FileOutputStream fileOutputStream = new FileOutputStream(file);
        OutputStreamWriter writer = new OutputStreamWriter(fileOutputStream, "UTF-8");//指定以UTF-8格式写入文件
        int[] indexRow = table.getSelectedRows();
        for (int i : indexRow) {
            for (int j = 0; j < table.getColumnCount(); j++) {
                writer.write((String) table.getValueAt(i, j)+" ");
            }
            writer.write("\n");
        }
        writer.close();
    }

    public void adjustColumnWidth() {
        TableColumnModel tableColumnModel = table.getColumnModel();
        tableColumnModel.getColumn(0).setPreferredWidth(200);
        tableColumnModel.getColumn(1).setPreferredWidth(150);
        tableColumnModel.getColumn(2).setPreferredWidth(25);
        tableColumnModel.getColumn(3).setPreferredWidth(150);
        tableColumnModel.getColumn(4).setPreferredWidth(25);
        tableColumnModel.getColumn(5).setPreferredWidth(150);
        tableColumnModel.getColumn(6).setPreferredWidth(25);
        tableColumnModel.getColumn(7).setPreferredWidth(150);
        tableColumnModel.getColumn(8).setPreferredWidth(25);
    }

}
最近下载更多
wanglinddad  LV55 3月9日
fenghuijun  LV26 1月13日
计算机暴龙战士  LV19 1月5日
305865088  LV7 2023年12月15日
Seaskye  LV14 2023年11月28日
2410068425  LV23 2023年11月27日
最代码官方  LV167 2023年11月26日
最近浏览更多
泓鼎168  LV19 4月30日
kenhomeliu  LV29 4月30日
getset  LV8 4月24日
pangzhihui  LV13 4月15日
zolscy  LV12 4月5日
zzz9985688  LV10 4月2日
小小可爱 3月17日
暂无贡献等级
a318888331  LV13 3月10日
wanglinddad  LV55 3月9日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友