这是比较简单的,就是有多个组件搭建的,就不细说了,不明白的可以参考之前的文章
首先是要给添加按钮添加点击事件的,然后就会弹出添加页面(这里需要继承JDialog),直接上添加页面的代码
public class AddStudentView extends JDialog {
// 声明一系列组件
JPanel jPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 10, 20));
JLabel nameLabel = new JLabel("姓名", JLabel.RIGHT); // 第二个参数表示水平的排列方式
JTextField nameTextField = new JTextField();
JLabel noLabel = new JLabel("学号", JLabel.RIGHT);
JTextField noTextField = new JTextField();
JLabel homeLabel = new JLabel("家乡", JLabel.RIGHT);
JTextField homeTextField = new JTextField();
JLabel chineseLabel = new JLabel("语文成绩", JLabel.RIGHT);
JTextField chineseTextField = new JTextField();
JLabel mathLabel = new JLabel("数学成绩", JLabel.RIGHT);
JTextField mathTextField = new JTextField();
JLabel englishLabel = new JLabel("英语成绩", JLabel.RIGHT);
JTextField englishTextField = new JTextField();
JButton addBtn = new JButton("添加");
public AddStudentView(MainView mainView) {
super(mainView, "添加学生信息",true);
addStudentViewHandler = new AddStudentViewHandler(this, mainView);
// 添加各组件到面板上
nameLabel.setPreferredSize(new Dimension(80, 30));
jPanel.add(nameLabel);
nameTextField.setPreferredSize(new Dimension(200, 30));
jPanel.add(nameTextField);
noLabel.setPreferredSize(new Dimension(80, 30));
jPanel.add(noLabel);
noTextField.setPreferredSize(new Dimension(200, 30));
jPanel.add(noTextField);
homeLabel.setPreferredSize(new Dimension(80, 30));
jPanel.add(homeLabel);
homeTextField.setPreferredSize(new Dimension(200, 30));
jPanel.add(homeTextField);
chineseLabel.setPreferredSize(new Dimension(80, 30));
jPanel.add(chineseLabel);
chineseTextField.setPreferredSize(new Dimension(200, 30));
jPanel.add(chineseTextField);
mathLabel.setPreferredSize(new Dimension(80, 30));
jPanel.add(mathLabel);
mathTextField.setPreferredSize(new Dimension(200, 30));
jPanel.add(mathTextField);
englishLabel.setPreferredSize(new Dimension(80, 30));
jPanel.add(englishLabel);
englishTextField.setPreferredSize(new Dimension(200, 30));
jPanel.add(englishTextField);
jPanel.add(addBtn);
addBtn.addActionListener(addStudentViewHandler);
Container contentPane = getContentPane();
contentPane.add(jPanel);
setSize(350, 500);
setLocationRelativeTo(null);
// DISPOSE_ON_CLOSE 只销毁当前窗体
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setResizable(false);
setVisible(true);
}
}
点击按钮时会触发addStudentViewHandler中的actionPerformed方法
addBtn.addActionListener(addStudentViewHandler);
在AddStudentView中实例化事件处理类对象,并将整个窗体this和mainView作为参数传过去,这边需要mainView,主要就是在数据添加成功后,需要调用mainView的reloadTable方法,重新加载表格。
AddStudentViewHandler addStudentViewHandler;
addStudentViewHandler = new AddStudentViewHandler(this, mainView);
先搭个框架,具体的添加方法还没写
package com.handler;
import com.entity.Student;
import com.service.Impl.StudentServiceImpl;
import com.student_view.AddStudentView;
import com.student_view.MainView;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
* @Author:xiexu
* @Date:2023/12/16 22:01
*/
public class AddStudentViewHandler implements ActionListener {
private AddStudentView addStudentView;
private MainView mainView;
public AddStudentViewHandler(AddStudentView addStudentView, MainView mainView){
this.addStudentView = addStudentView;
this.mainView = mainView;
}
@Override
public void actionPerformed(ActionEvent e) {
JButton jButton = (JButton) e.getSource();
String text = jButton.getText();
if ("添加".equals(text)) {
......
if () {
// 重新加载表格查到最新数据
mainView.reloadTable();
addStudentView.dispose();
} else {
// 添加错误,给出提示
JOptionPane.showMessageDialog(addStudentView, "添加失败");
}
}
}
}
首先获取表单中的内容,然后封装成一个对象
public Student builStudent() {
Student student = new Student();
student.setName(nameTextField.getText());
student.setNo(noTextField.getText());
student.setHomeTown(homeTextField.getText());
student.setChinese(Double.parseDouble(chineseTextField.getText()));
student.setMath(Double.parseDouble(mathTextField.getText()));
student.setEnglish(Double.parseDouble(englishTextField.getText()));
return student;
}
boolean add(Student student);
@Override
public boolean add(Student student) {
StringBuilder sql = new StringBuilder();
sql.append("insert into detail(name,no,homeTown,chinese,math,english)");
sql.append("values(?,?,?,?,?,?)");
// 执行
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
TableDTO tableDTO = new TableDTO(); // 返回的数据
try {
connection = DBUtil.getConnection();
preparedStatement = connection.prepareStatement(sql.toString());
preparedStatement.setString(1, student.getName());
preparedStatement.setString(2, student.getNo());
preparedStatement.setString(3, student.getHomeTown());
preparedStatement.setDouble(4, student.getChinese());
preparedStatement.setDouble(5, student.getMath());
preparedStatement.setDouble(6, student.getEnglish());
return preparedStatement.executeUpdate() == 1; // 执行查询返回结果集
}catch (Exception e) {
e.printStackTrace();
} finally {
DBUtil.closeRS(resultSet);
DBUtil.closePS(preparedStatement);
DBUtil.closeConnection(connection);
}
return false;
}