项目Github地址:https://github.com/vanyongqi/HonorOfKingsSystem
最近在忙活java大作业,各种学习钻研,疯狂抄袭改造,博百家精华,剔处处糟粕(吹b了);
时间比较赶,也很仓促,昨天熬了熬夜,今天加了加班。不得不说连我自己都被我这种发烧写代码的精神感动了,哇,真的写代码写爽了。不想和任何人说话,不想吃饭(主要是没吃的),伴随着寝室悦耳的咳嗽声(集体传染),不断前进。突然觉得写代码是一件很舒服的事情。
废话不多说了,先亮出思路吧。
拖得有点久,四月十号到七月十号当真是很久的故事了。 稍微梳理梳理就开始工作吧。
由于系统分工比较细致,主要是避免耦合性过高,尽量将功能、实现、和视图分离开来,没有系统的了解过MVC设计模式,不过还是腆着脸写,毕竟设计模式更重要的让用户能够清晰的了解代码,而非生搬硬套,只要达到那个意义就行,不必拘泥于形。下面用了思维导图来描述系统各个.java文件的作用,共计15个java文件。
就我个人而言,关于项目日后拓展方面,要比别的管理系统容易的多得多。写代码要从工程的角度去考量分析,维护性高,可靠性强的软件项目才有存活下去的意义,不然就只是单纯地在制造01流的垃圾。
个人觉得比较不错的地方:
一共是三段代码,第一段将GUI与数据库连接写在了一起,实际上,是十分不妥当的一种做法。这样写,在实际过程中容易出现混乱,后来在网上看到别人的写法,觉得将其分离才是更为合适的方法。
import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
public class AddInfor extends JDialog implements ActionListener {
JButton ADDROLE;
JButton CONCLE;
JTextField nameInput;
JTextField stageNameInput;
JTextField gameLocationInput;
JTextField gameAdvantageInput;
JTextField rolePropertyInput;
JTextField rolePriceInput;
JTextField roleSkillInput;
public AddInfor(Frame owner, String title, boolean modal) {
super(owner, title, modal);
JPanel LABELAREA = new JPanel();
JPanel TEXTAREA = new JPanel();
JPanel BUTTONAREA = new JPanel();
JLabel roleName = new JLabel("英雄");
JLabel roleStageName = new JLabel("称号");
JLabel gameLocation = new JLabel("位置");
JLabel roleAdvantage = new JLabel("特长");
JLabel roleProperty = new JLabel("属性");
JLabel rolePrice = new JLabel("价格");
JLabel roleSkill = new JLabel("技能");
nameInput = new JTextField(10);
stageNameInput = new JTextField(10);
gameLocationInput = new JTextField(10);
gameAdvantageInput = new JTextField(10);
rolePropertyInput = new JTextField(10);
rolePriceInput = new JTextField(10);
roleSkillInput = new JTextField(10);
ADDROLE = new JButton("增加角色");
ADDROLE.addActionListener(this);
CONCLE = new JButton("取消");
CONCLE.addActionListener(this);
// design LAYOUT
LABELAREA.setLayout(new GridLayout(6, 1));
LABELAREA.add(roleName);
LABELAREA.add(roleStageName);
LABELAREA.add(gameLocation);
LABELAREA.add(roleAdvantage);
LABELAREA.add(roleProperty);
LABELAREA.add(rolePrice);
LABELAREA.add(roleSkill);
TEXTAREA.setLayout(new GridLayout(6, 1));
TEXTAREA.add(nameInput);
TEXTAREA.add(stageNameInput);
TEXTAREA.add(gameLocationInput);
TEXTAREA.add(gameAdvantageInput);
TEXTAREA.add(rolePropertyInput);
TEXTAREA.add(rolePriceInput);
TEXTAREA.add(roleSkillInput);
BUTTONAREA.setLayout(new GridLayout(1, 2));
BUTTONAREA.add(ADDROLE);
BUTTONAREA.add(CONCLE);
this.add(LABELAREA, BorderLayout.WEST);
this.add(TEXTAREA, BorderLayout.CENTER);
this.add(BUTTONAREA, BorderLayout.SOUTH);
this.setLocation(450,350);
this.setSize(300, 200);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == ADDROLE) {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
System.out.println("修改完成!!!");
try {
Class.forName("com.mysql.cj.jdbc.Driver");
System.out.println("数据库加载观测 正常");
String url = "jdbc:mysql://localhost:3306/hero";
String user = "root";
String PINcode = "012357";
conn = DriverManager.getConnection(url, user, PINcode);
String sql = "insert into hero values(?,?,?,?,?,?,?)";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, nameInput.getText());
pstmt.setString(2, stageNameInput.getText());
pstmt.setString(3, gameLocationInput.getText());
pstmt.setString(4, gameAdvantageInput.getText());
pstmt.setString(5, rolePropertyInput.getText());
pstmt.setString(6, " "+rolePriceInput.getText());
pstmt.setString(7, roleSkillInput.getText());
pstmt.executeUpdate();
this.dispose();
} catch (Exception e1) {
e1.printStackTrace();
} finally {
try {
if (rs != null) {
rs.close();
rs = null;
}
if (pstmt != null) {
pstmt.close();
pstmt = null;
}
/*if (conn != null) {
conn.close();
conn = null;
}*/
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
}
}
//注册页面
import javax.swing.JFrame;
import java.awt.FlowLayout;
import javax.swing.*;
public class Register extends JFrame{
JLabel accountLabel,nameLabel;
JButton okButton,resetButton;
JTextField accountText,nameText;
Box baseBox1,baseBox2, box1,box2,box3; //Box布局方式;
//JPanel pane1,pane2;
Register_DB regist;
Register(){
init();
}
void init(){
setLayout(new FlowLayout());
accountLabel = new JLabel("账号");
nameLabel= new JLabel("密码");
accountText = new JTextField(10);
nameText = new JTextField(20);
okButton = new JButton("确定");
resetButton = new JButton("重置");
regist = new Register_DB();
// lab = new JLabel("用户注册页面");
box1 = Box.createVerticalBox();
box1.add(accountLabel);
box1.add(Box.createVerticalStrut(8));
box1.add(nameLabel);
box2 = Box.createVerticalBox();
box2.add(accountText);
box2.add(Box.createVerticalStrut(8));
box2.add(nameText);
box3 = Box.createHorizontalBox();
box3.add(okButton);
box3.add(Box.createHorizontalStrut(15));
box3.add(resetButton);
baseBox1 = Box.createHorizontalBox();
baseBox1.add(box1);
baseBox1.add(Box.createHorizontalStrut(8));
baseBox1.add(box2);
baseBox2 = Box.createVerticalBox();
baseBox2.add(baseBox1);
baseBox2.add(Box.createVerticalStrut(10));
baseBox2.add(box3);
add(baseBox2);
okButton.addActionListener(regist);
resetButton.addActionListener(regist);
regist.setaccountField(accountText);
regist.setnameField(nameText);
regist.setokButton(okButton);
regist.setresetButton(resetButton);
setBounds(200,200,400,300);
setVisible(true);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setTitle("用户注册界面");
}
}
//注册的数据库操作
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.*;
public class Register_DB extends Conn_DB implements ActionListener{
JTextField textacc,textname;
JButton okButton,resetButton;
Statement stmt;
ResultSet rs;
String name;
// double acc;
// String name;
// Connection con = null;
public void setaccountField(JTextField a){
textacc = a;
}
public void setnameField(JTextField n){
textname = n;
}
public void setokButton(JButton b1){
okButton = b1;
}
public void setresetButton(JButton b2){
resetButton = b2;
}
public void actionPerformed(ActionEvent e){
if(e.getSource() == okButton){
String code = textname.getText();
if(textacc.getText().equals("")) //判断用户输入是否为空;
JOptionPane.showMessageDialog(null, "请输入账号","警告对话框",JOptionPane.WARNING_MESSAGE);
else if(code.equals(""))
JOptionPane.showMessageDialog(null,"请输入姓名","警告对话框",JOptionPane.WARNING_MESSAGE);
else{
String acc = textacc.getText();
/************************************************************************/
String name = code;
/************************************************************************/
/**********************************************************************************************************/
/**********************************************************************************************************/
String mdCode= new MD5().encryption(code);
/**********************************************************************************************************/
/**********************************************************************************************************/
/****************************************************************************/
try {
connection();
writeInSql(acc,mdCode);
} catch (Exception e1) {
System.out.println("插入失败");
e1.printStackTrace();
}
}
}
else if(e.getSource() == resetButton){
textacc.setText("");
textname.setText("");
}
}
void writeInSql(String acc,String name) throws Exception{
String sql;
Connection con = super.con;
Statement stmt = con.createStatement();
//创建并检查数据库表,若没有"my"表,则创建表并执行插入操作,若表已存在,则直接执行操作,插入数据
sql = "create table if not exists my(account varchar(10),name varchar(40))";
stmt.executeUpdate(sql);
// System.out.println(sql); //查错;
System.out.println("创建表成功");
//插入存文本框中获取的数据;
sql = "insert into my(account,name) values('"+acc+"','"+name+"')";
int rw = stmt.executeUpdate(sql);
// System.out.println(sql);
if(rw <= 0){ //判断数据是否插入成功
JOptionPane.showMessageDialog(null,"注册失败");
}
else{
JOptionPane.showMessageDialog(null, "注册成功");
}
}
}
/**********************Login 部分*********************************/
regist = new Register_DB();
/***************************************************************/
regist.setaccountField(accountText);
regist.setnameField(nameText);
regist.setokButton(okButton);
regist.setresetButton(resetButton);
public void setaccountField(JTextField a){
textacc = a;
}
public void setnameField(JTextField n){
textname = n;
}
public void setokButton(JButton b1){
okButton = b1;
}
public void setresetButton(JButton b2){
resetButton = b2;
}
参数的传递,在格式上看起来更加赏心悦目。不得不说,这种写法确实很棒。