题库管理系统的界面设计代码
时间: 2023-12-26 16:05:30 浏览: 23
题库管理系统的界面设计可以采用Java Swing或JavaFX等GUI库进行实现,以下是一个Java Swing的简单示例:
```
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
public class QuestionBankGUI extends JFrame {
private QuestionBank questionBank;
private JLabel idLabel;
private JTextField idField;
private JLabel contentLabel;
private JTextField contentField;
private JLabel answerLabel;
private JTextField answerField;
private JButton addButton;
private JButton deleteButton;
private JButton updateButton;
private JButton searchButton;
private JTextArea resultArea;
public QuestionBankGUI(QuestionBank questionBank) {
this.questionBank = questionBank;
// 设置窗口标题和大小
setTitle("题库管理系统");
setSize(500, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 创建界面元素
idLabel = new JLabel("题目编号");
idField = new JTextField(10);
contentLabel = new JLabel("题目内容");
contentField = new JTextField(30);
answerLabel = new JLabel("题目答案");
answerField = new JTextField(10);
addButton = new JButton("添加");
deleteButton = new JButton("删除");
updateButton = new JButton("修改");
searchButton = new JButton("搜索");
resultArea = new JTextArea(10, 40);
// 创建界面布局
JPanel inputPanel = new JPanel(new GridLayout(3, 2));
inputPanel.add(idLabel);
inputPanel.add(idField);
inputPanel.add(contentLabel);
inputPanel.add(contentField);
inputPanel.add(answerLabel);
inputPanel.add(answerField);
JPanel buttonPanel = new JPanel(new GridLayout(4, 1));
buttonPanel.add(addButton);
buttonPanel.add(deleteButton);
buttonPanel.add(updateButton);
buttonPanel.add(searchButton);
JPanel resultPanel = new JPanel(new BorderLayout());
resultPanel.add(new JScrollPane(resultArea), BorderLayout.CENTER);
JPanel mainPanel = new JPanel(new BorderLayout());
mainPanel.add(inputPanel, BorderLayout.NORTH);
mainPanel.add(buttonPanel, BorderLayout.WEST);
mainPanel.add(resultPanel, BorderLayout.CENTER);
// 添加事件监听器
addButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int id = Integer.parseInt(idField.getText());
String content = contentField.getText();
String answer = answerField.getText();
questionBank.addQuestion(new Question(id, content, answer));
resultArea.setText("添加成功!");
}
});
deleteButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int id = Integer.parseInt(idField.getText());
Question question = questionBank.findQuestionById(id);
if (question != null) {
questionBank.deleteQuestion(question);
resultArea.setText("删除成功!");
} else {
resultArea.setText("找不到指定的题目!");
}
}
});
updateButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int id = Integer.parseInt(idField.getText());
Question question = questionBank.findQuestionById(id);
if (question != null) {
String newContent = contentField.getText();
String newAnswer = answerField.getText();
question.setContent(newContent);
question.setAnswer(newAnswer);
resultArea.setText("修改成功!");
} else {
resultArea.setText("找不到指定的题目!");
}
}
});
searchButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String keyword = contentField.getText();
List<Question> questions = questionBank.getQuestions();
StringBuilder sb = new StringBuilder();
for (Question question : questions) {
if (question.getContent().contains(keyword)) {
sb.append("Question " + question.getId() + ": " + question.getContent() + "\n");
sb.append("Answer: " + question.getAnswer() + "\n\n");
}
}
if (sb.length() > 0) {
resultArea.setText(sb.toString());
} else {
resultArea.setText("找不到符合条件的题目!");
}
}
});
// 添加界面到窗口中
setContentPane(mainPanel);
setVisible(true);
}
public static void main(String[] args) {
QuestionBank questionBank = new QuestionBank();
questionBank.addQuestion(new Question(1, "2+2=?", "4"));
questionBank.addQuestion(new Question(2, "1+1=?", "2"));
new QuestionBankGUI(questionBank);
}
}
```
这个例子创建了一个继承自JFrame的QuestionBankGUI类,其中包含了题目编号、题目内容、题目答案、添加按钮、删除按钮、修改按钮、搜索按钮和结果区域等界面元素。在添加、删除、修改、搜索按钮上添加了事件监听器,通过调用QuestionBank类中的方法来实现增删改查的功能。需要注意的是,实际的题库管理系统需要根据具体需求进行界面设计和实现。