Java 图形界面会员管理系统:新增会员功能实现
/**
- 本类用于新增会员时显示相应的界面及实现相应的功能
- @author rhh */
package pages;
import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Font; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.Statement;
import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextArea;
import MainSystem.MainSystem;
public class JPAddMember extends JPanel { /** * */ private static final long serialVersionUID = 1L; private JLabel jlbPassword; private JLabel jlbID;
public JPAddMember() { JTextArea jtaTips = new JTextArea(); jtaTips.setFont(MainSystem.fontFirstPage); jtaTips.setText('\n\t 新增会员\n\n' + '1、点击新增会员按钮,系统会分配一个默认的用户名和密码,用户在获取用户名和密码后应尽快修改相关信息;\n\n' + '2、若会员已经记下默认的用户名和密码,请点击恢复默认按钮,系统将不再显示此会员的用户名和密码。'); jtaTips.setLineWrap(true); jtaTips.setWrapStyleWord(true); jtaTips.setEditable(false); jtaTips.setPreferredSize(new Dimension(500, 220)); jtaTips.setBackground(getBackground());
JPanel jpTips = new JPanel();
jpTips.add(jtaTips);
setLayout(new BorderLayout());
add(jpTips, BorderLayout.NORTH);
JButton jbtAddMember = new JButton('新增会员');
jbtAddMember.setFont(MainSystem.fontButton);
jbtAddMember.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String theID;
do {
theID = getID();
} while (isEqual(theID));
String thePassword = '' + (int) (100000 + Math.random() * 900000);
int choice =
JOptionPane.showConfirmDialog(null, '确认要增加一个新会员?', '确认', JOptionPane.OK_CANCEL_OPTION);
if (choice == JOptionPane.OK_OPTION) {
try {
Connection connect = MainSystem.connectToData();
PreparedStatement Statement =
connect.prepareStatement('INSERT INTO LogIn_Members VALUES(?,?,?)');
Statement.setString(1, thePassword);
Statement.setString(2, theID);
java.util.Date date = new java.util.Date();
java.sql.Date now = new java.sql.Date(date.getTime());
Statement.setDate(3, now);
Statement.executeUpdate();
connect.close();
} catch (Exception ex) {
System.out.print('新增会员写入数据出错');
ex.printStackTrace();
}
MainSystem.modelViewMembers.addRow(theID);
MainSystem.memberTable.revalidate();
JPFirstPage.jlbNumOfMembers.setText(Integer.parseInt(JPFirstPage.jlbNumOfMembers
.getText()) + 1 + '');
jlbID.setText(theID);
jlbPassword.setText(thePassword);
}
}
});
JButton jbtDefault = new JButton('恢复默认');
jbtDefault.setFont(MainSystem.fontButton);
jbtDefault.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jlbID.setText('————');
jlbPassword.setText('————');
}
});
JPanel jpButton = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 10));
jpButton.add(jbtAddMember);
jpButton.add(jbtDefault);
add(jpButton, BorderLayout.SOUTH);
JPanel jpContent = new JPanel();
GridBagLayout layoutAddBook = new GridBagLayout();
jpContent.setLayout(layoutAddBook);
GridBagConstraints styleAddBook = new GridBagConstraints();
styleAddBook.fill = GridBagConstraints.BOTH;
Font font = new Font('TimesRoman', Font.BOLD + Font.ITALIC, 20);
JLabel jlb1 = new JLabel('用户名: ');
jlb1.setFont(font);
jpContent.add(jlb1);
jlbID = new JLabel('————');
jlbID.setFont(font);
jlbID.setForeground(Color.MAGENTA);
jpContent.add(jlbID);
JPanel jpNull = new JPanel();
jpContent.add(jpNull);
JLabel jlb2 = new JLabel('密码: ');
jlb2.setFont(font);
jpContent.add(jlb2);
jlbPassword = new JLabel('————');
jlbPassword.setFont(font);
jlbPassword.setForeground(Color.MAGENTA);
jpContent.add(jlbPassword);
styleAddBook.gridwidth = 1;
styleAddBook.weightx = 0;
styleAddBook.weighty = 0;
layoutAddBook.setConstraints(jlb1, styleAddBook);
layoutAddBook.setConstraints(jlb2, styleAddBook);
styleAddBook.gridwidth = 0;
styleAddBook.weightx = 0;
styleAddBook.weighty = 0;
layoutAddBook.setConstraints(jlbID, styleAddBook);
layoutAddBook.setConstraints(jlbPassword, styleAddBook);
layoutAddBook.setConstraints(jpNull, styleAddBook);
add(jpContent, BorderLayout.CENTER);
}
@SuppressWarnings('finally') private boolean isEqual(String id) { Boolean isEqual = false;
try {
Connection connect = MainSystem.connectToData();
Statement stmt = connect.createStatement();
ResultSet rs = stmt.executeQuery('select * from login_members');
while (rs.next()) {
if (rs.getString('id').equals(id)) {
isEqual = true;
break;
}
}
connect.close();
} catch (Exception ex) {
System.out.print('新增会员获取数据出错');
ex.printStackTrace();
} finally {
return isEqual;
}
}
private String getID() { String theID = '';
for (int i = 0; i < 6; i++) {
int j = (int) (97 + Math.random() * 26);
theID += (char) j;
}
theID += (int) (100000 + Math.random() * 900000);
return theID;
} }
原文地址: https://www.cveoy.top/t/topic/ooMs 著作权归作者所有。请勿转载和采集!