Compare commits
2 Commits
7b874c3a49
...
6cb9a8f864
| Author | SHA1 | Date | |
|---|---|---|---|
| 6cb9a8f864 | |||
| 9aa97edb0e |
@@ -82,6 +82,9 @@ public class QuestionsDao {
|
||||
stmt.setString(1, question.getName());
|
||||
stmt.setString(2, question.getDescription());
|
||||
stmt.setString(3, question.getAnswer());
|
||||
if(question.getFrequency()==null)
|
||||
stmt.setInt(4, 0);
|
||||
else
|
||||
stmt.setInt(4, question.getFrequency());
|
||||
row = stmt.executeUpdate();
|
||||
stmt.close();
|
||||
@@ -114,19 +117,22 @@ public class QuestionsDao {
|
||||
}
|
||||
return row > 0;
|
||||
}
|
||||
public boolean updata(Questions question){
|
||||
public boolean update(Questions question){
|
||||
int row = 0;
|
||||
//1. 从连接池中拿到连接
|
||||
Connection conn = DBConnectionPool.getConnection();
|
||||
if(conn==null)
|
||||
return false;
|
||||
//2. 创建Statement对象
|
||||
String sql = "updata questions set name = ?,description = ?,answer = ?,frequency=? where id = ?";
|
||||
String sql = "update questions set name = ?,description = ?,answer = ?,frequency=? where id = ?";
|
||||
try{
|
||||
PreparedStatement stmt = conn.prepareStatement(sql);
|
||||
stmt.setString(1, question.getName());
|
||||
stmt.setString(2, question.getDescription());
|
||||
stmt.setString(3, question.getAnswer());
|
||||
if(question.getFrequency()==null)
|
||||
stmt.setInt(4, 0);
|
||||
else
|
||||
stmt.setInt(4, question.getFrequency());
|
||||
stmt.setInt(5, question.getId());
|
||||
row = stmt.executeUpdate();
|
||||
|
||||
@@ -5,7 +5,7 @@ public class Questions {
|
||||
private String name;
|
||||
private String description;
|
||||
private String answer;
|
||||
private int frequency;
|
||||
private Integer frequency;
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
@@ -39,11 +39,11 @@ public class Questions {
|
||||
this.answer = answer;
|
||||
}
|
||||
|
||||
public int getFrequency() {
|
||||
public Integer getFrequency() {
|
||||
return frequency;
|
||||
}
|
||||
|
||||
public void setFrequency(int frequency) {
|
||||
public void setFrequency(Integer frequency) {
|
||||
this.frequency = frequency;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,8 +4,16 @@ import java.util.*;
|
||||
public class GlobalData {
|
||||
public static final Map<Integer,String> FREQUENCY_MAP = new HashMap<>();
|
||||
static{
|
||||
FREQUENCY_MAP.put(0,"未指定");
|
||||
FREQUENCY_MAP.put(1,"低频");
|
||||
FREQUENCY_MAP.put(2,"中频");
|
||||
FREQUENCY_MAP.put(3,"高频");
|
||||
}
|
||||
public static Integer getFrequencyByValue(String value){
|
||||
for(Map.Entry<Integer,String> entry:FREQUENCY_MAP.entrySet()) {
|
||||
if (entry.getValue().equals(value))
|
||||
return entry.getKey();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,4 +45,7 @@ public class QuestionsService {
|
||||
public void delete(Integer id){
|
||||
questionsDao.delete(id);
|
||||
}
|
||||
public boolean update(Questions question){
|
||||
return questionsDao.update(question);
|
||||
}
|
||||
}
|
||||
|
||||
109
src/view/EditQuestionDialog.java
Normal file
109
src/view/EditQuestionDialog.java
Normal file
@@ -0,0 +1,109 @@
|
||||
package view;
|
||||
|
||||
import backend.model.Questions;
|
||||
import backend.service.impl.QuestionsService;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
|
||||
public class EditQuestionDialog extends JDialog{
|
||||
private JTextField nameTextField;
|
||||
private JTextField descriptionTextField;
|
||||
private JTextField answerTextField;
|
||||
private JComboBox frequencyCombobox;
|
||||
private JButton saveButton;
|
||||
private JButton cancelButton;
|
||||
private int type;
|
||||
private Questions question;
|
||||
private QuestionsService questionsService = new QuestionsService();
|
||||
|
||||
public EditQuestionDialog(int type, Questions question) {
|
||||
this.type = type;
|
||||
this.question = question;
|
||||
initView();
|
||||
}
|
||||
|
||||
public void initListener(){
|
||||
saveButton.addActionListener(e->{
|
||||
String name = nameTextField.getText().trim();
|
||||
String description = descriptionTextField.getText().trim();
|
||||
String answer = answerTextField.getText().trim();
|
||||
if(name.equals("")|| description.equals("")|| answer.equals("")) {
|
||||
JOptionPane.showMessageDialog(this, "请填写完整!");
|
||||
return;
|
||||
}
|
||||
Questions questionsPo = new Questions();
|
||||
questionsPo.setName(name);
|
||||
questionsPo.setDescription(description);
|
||||
questionsPo.setAnswer(answer);
|
||||
Integer index=frequencyCombobox.getSelectedIndex();
|
||||
questionsPo.setFrequency(index==0 ? null:index);
|
||||
String message;
|
||||
boolean res;
|
||||
if(type==0) {
|
||||
res = questionsService.insert(questionsPo);
|
||||
message = "添加";
|
||||
}
|
||||
else{
|
||||
questionsPo.setId(question.getId());
|
||||
res = questionsService.update(questionsPo);
|
||||
message = "修改";
|
||||
}
|
||||
if(res)
|
||||
dispose();
|
||||
else
|
||||
JOptionPane.showMessageDialog(this, message+"失败");
|
||||
});
|
||||
cancelButton.addActionListener(e->{
|
||||
dispose();
|
||||
});
|
||||
}
|
||||
private void initView(){
|
||||
setSize(400,300);
|
||||
setLocationRelativeTo(null);//居中
|
||||
add(initPanel());
|
||||
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
|
||||
setModal(true);//设置模态
|
||||
if(type==0)
|
||||
setTitle("添加问题");
|
||||
else{
|
||||
setTitle("修改问题");
|
||||
nameTextField.setText(question.getName());
|
||||
descriptionTextField.setText(question.getDescription());
|
||||
answerTextField.setText(question.getAnswer());
|
||||
frequencyCombobox.setSelectedIndex(question.getFrequency() == null? 0:question.getFrequency());
|
||||
}
|
||||
//细节:要先初始化监听器,再初始化界面
|
||||
//初始化监听器
|
||||
initListener();
|
||||
//显示界面
|
||||
setVisible(true);
|
||||
}
|
||||
private JPanel initPanel(){
|
||||
JPanel mainPanel = new JPanel();
|
||||
mainPanel.setLayout(new GridLayout(5,2));
|
||||
//第一行 单词
|
||||
mainPanel.add(new JLabel("问题"));
|
||||
nameTextField = new JTextField();
|
||||
mainPanel.add(nameTextField);
|
||||
//第二行 定义
|
||||
mainPanel.add(new JLabel("描述"));
|
||||
descriptionTextField = new JTextField();
|
||||
mainPanel.add(descriptionTextField);
|
||||
//第三行 例句
|
||||
mainPanel.add(new JLabel("答案"));
|
||||
answerTextField = new JTextField();
|
||||
mainPanel.add(answerTextField);
|
||||
//第四行 频率
|
||||
mainPanel.add(new JLabel("频率"));
|
||||
frequencyCombobox = new JComboBox<>(new String[]{"未指定","低频","中频","高频"});
|
||||
mainPanel.add(frequencyCombobox);
|
||||
//第五行 按钮
|
||||
saveButton = new JButton("保存");
|
||||
cancelButton = new JButton("取消");
|
||||
mainPanel.add(saveButton);
|
||||
mainPanel.add(cancelButton);
|
||||
return mainPanel;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -2,9 +2,13 @@ package view;
|
||||
import javax.swing.*;
|
||||
import javax.swing.table.DefaultTableModel;
|
||||
import java.awt.*;
|
||||
import java.awt.event.MouseAdapter;
|
||||
import java.awt.event.MouseEvent;
|
||||
import java.util.Vector;
|
||||
|
||||
import backend.model.Questions;
|
||||
import backend.service.impl.QuestionsService;
|
||||
import backend.model.enums.GlobalData;
|
||||
|
||||
public class QuestionsPanel extends JPanel{
|
||||
private QuestionsService questionsService = new QuestionsService();
|
||||
@@ -30,7 +34,7 @@ public class QuestionsPanel extends JPanel{
|
||||
}
|
||||
public void initListener(){
|
||||
queryButton.addActionListener(e->{
|
||||
int frequency = frequencyComboBox.getSelectedIndex()==0?null:frequencyComboBox.getSelectedIndex();
|
||||
Integer frequency = frequencyComboBox.getSelectedIndex()==0?null:frequencyComboBox.getSelectedIndex();
|
||||
String key = keyTextField.getText();
|
||||
//清楚已有数据
|
||||
model.setRowCount(0);
|
||||
@@ -51,6 +55,44 @@ public class QuestionsPanel extends JPanel{
|
||||
}
|
||||
}
|
||||
});
|
||||
//添加事件:打开对话框
|
||||
addButton.addActionListener(e->{
|
||||
new EditQuestionDialog(0,null);
|
||||
});
|
||||
editButton.addActionListener(e->{
|
||||
int row = table.getSelectedRow();
|
||||
if(row>=0)
|
||||
openEditDialog(row);
|
||||
|
||||
});
|
||||
//双击某行弹出修改对话框
|
||||
table.addMouseListener(new MouseAdapter() {
|
||||
@Override
|
||||
public void mouseClicked(MouseEvent e) {
|
||||
if(e.getClickCount()==2){
|
||||
int row = table.getSelectedRow();
|
||||
openEditDialog(row);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void openEditDialog(int row){
|
||||
Vector<String> data = tableData.get(row);
|
||||
Questions questions = new Questions();
|
||||
questions.setId(Integer.parseInt(data.get(0)));
|
||||
questions.setName(data.get(1));
|
||||
questions.setDescription(data.get(2));
|
||||
questions.setAnswer(data.get(3));
|
||||
questions.setFrequency(GlobalData.getFrequencyByValue(data.get(4)));
|
||||
new EditQuestionDialog(1, questions);
|
||||
//修改成功后刷新并定位修改位置
|
||||
tableData = questionsService.getAll();
|
||||
refreshData();
|
||||
table.setRowSelectionInterval(row,row);
|
||||
scrollPane.getVerticalScrollBar().setValue(row*table.getRowHeight());
|
||||
scrollPane.getHorizontalScrollBar().setValue(0);
|
||||
scrollPane.revalidate();
|
||||
}
|
||||
public QuestionsPanel(){
|
||||
initView();
|
||||
|
||||
Reference in New Issue
Block a user