java实现问卷设计并预览
时间: 2024-01-20 14:04:09 浏览: 112
java课程设计 问卷调查系统的设计与实现 .zip
5星 · 资源好评率100%
Java可以使用多种GUI框架来实现问卷设计并预览,例如Swing和JavaFX。以下是一个使用Swing实现的问卷设计及预览的简单示例:
```java
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class QuestionnaireDesigner extends JFrame implements ActionListener {
private List<Question> questions;
private JTextArea previewArea;
private JTextField questionField;
private List<JCheckBox> optionFields;
public QuestionnaireDesigner() {
super("Questionnaire Designer");
// Initialize questions list
questions = new ArrayList<Question>();
// Create UI components
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
JLabel questionLabel = new JLabel("Question:");
questionField = new JTextField(20);
JPanel optionPanel = new JPanel();
optionPanel.setLayout(new BoxLayout(optionPanel, BoxLayout.Y_AXIS));
optionPanel.setBorder(BorderFactory.createTitledBorder("Options"));
optionFields = new ArrayList<JCheckBox>();
for (int i = 0; i < 4; i++) {
JCheckBox optionField = new JCheckBox("Option " + (i + 1));
optionFields.add(optionField);
optionPanel.add(optionField);
}
JButton addButton = new JButton("Add Question");
addButton.addActionListener(this);
previewArea = new JTextArea();
previewArea.setEditable(false);
JScrollPane previewScrollPane = new JScrollPane(previewArea);
previewScrollPane.setPreferredSize(new Dimension(400, 200));
mainPanel.add(questionLabel);
mainPanel.add(Box.createRigidArea(new Dimension(0, 5)));
mainPanel.add(questionField);
mainPanel.add(Box.createRigidArea(new Dimension(0, 10)));
mainPanel.add(optionPanel);
mainPanel.add(Box.createRigidArea(new Dimension(0, 10)));
mainPanel.add(addButton);
mainPanel.add(Box.createRigidArea(new Dimension(0, 10)));
mainPanel.add(previewScrollPane);
getContentPane().add(mainPanel, BorderLayout.CENTER);
// Configure frame
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
// Create question object from UI components
String questionText = questionField.getText();
List<String> optionTexts = new ArrayList<String>();
for (JCheckBox optionField : optionFields) {
if (optionField.isSelected()) {
optionTexts.add(optionField.getText());
}
}
Question question = new Question(questionText, optionTexts.toArray(new String[0]));
// Add question to list and update preview area
questions.add(question);
previewArea.setText(getQuestionnaireText());
}
private String getQuestionnaireText() {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < questions.size(); i++) {
Question question = questions.get(i);
sb.append(i + 1);
sb.append(". ");
sb.append(question.getText());
sb.append("\n");
String[] options = question.getOptions();
for (int j = 0; j < options.length; j++) {
sb.append(" ");
sb.append((char) ('a' + j));
sb.append(": ");
sb.append(options[j]);
sb.append("\n");
}
sb.append("\n");
}
return sb.toString();
}
public static void main(String[] args) {
new QuestionnaireDesigner();
}
private static class Question {
private String text;
private String[] options;
public Question(String text, String[] options) {
this.text = text;
this.options = options;
}
public String getText() {
return text;
}
public String[] getOptions() {
return options;
}
}
}
```
这个示例使用了Swing的JFrame、JLabel、JTextField、JPanel、JCheckBox、JButton和JTextArea等组件,可以实现基本的问卷设计和预览功能。用户可以在界面中输入问题和选项,然后点击“Add Question”按钮添加问题到问卷中,同时预览区域会显示当前问卷的所有问题和选项。
阅读全文