用java编写一个程序可以在d盘根目录下建立一个CTest目录,要求所有同学的试卷建立一个以学号为目录的文件夹,点击阅卷图形界面的“查看试卷”按钮,该同学的试卷就出现在界面的文本框中,按下“保存”按钮,该同学的试卷文件改名为x+学号
时间: 2023-11-22 14:56:36 浏览: 36
以下是一个示例代码,可以在Java中实现你的需求:
```java
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
public class TestPaperGrader extends JFrame {
private JPanel contentPane;
private JTextField textField;
private JTextArea textArea;
private File baseDir;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
TestPaperGrader frame = new TestPaperGrader();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public TestPaperGrader() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JPanel panel = new JPanel();
contentPane.add(panel, BorderLayout.NORTH);
JButton btnCreateDir = new JButton("Create CTest Directory");
btnCreateDir.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
createCTestDirectory();
}
});
panel.add(btnCreateDir);
textField = new JTextField();
panel.add(textField);
textField.setColumns(10);
JButton btnLoadFile = new JButton("Load File");
btnLoadFile.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
loadFile();
}
});
panel.add(btnLoadFile);
JButton btnSaveFile = new JButton("Save File");
btnSaveFile.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
saveFile();
}
});
panel.add(btnSaveFile);
JScrollPane scrollPane = new JScrollPane();
contentPane.add(scrollPane, BorderLayout.CENTER);
textArea = new JTextArea();
scrollPane.setViewportView(textArea);
}
private void createCTestDirectory() {
baseDir = new File("D:\\CTest");
if (!baseDir.exists()) {
baseDir.mkdir();
}
}
private void loadFile() {
JFileChooser fileChooser = new JFileChooser(baseDir);
if (fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
try (Scanner scanner = new Scanner(new FileInputStream(file))) {
StringBuilder sb = new StringBuilder();
while (scanner.hasNextLine()) {
sb.append(scanner.nextLine());
sb.append("\n");
}
textArea.setText(sb.toString());
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void saveFile() {
String studentId = textField.getText();
if (studentId != null && studentId.length() > 0) {
File dir = new File(baseDir, studentId);
if (!dir.exists()) {
dir.mkdir();
}
File file = new File(dir, "testpaper.txt");
try (FileOutputStream fos = new FileOutputStream(file)) {
byte[] data = textArea.getText().getBytes();
fos.write(data);
fos.flush();
} catch (IOException e) {
e.printStackTrace();
}
file.renameTo(new File(dir, "x" + studentId));
}
}
}
```
这个程序有一个界面,可以通过按钮实现创建CTest目录、加载试卷文件、保存试卷文件的功能。其中,创建CTest目录会在D盘根目录下建立一个CTest目录;加载试卷文件会弹出一个文件选择器,可以选择试卷文件并在界面的文本框中显示;保存试卷文件会将当前界面的文本框中的内容保存到对应学生的目录下,并将试卷文件重命名为x+学号。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)