项目名称:背单词软件
完整代码链接:完整代码+txt文件
提取码:i6gd
有注释,看懂没什么问题
问题描述:
用户登录软件后可选择多个功能,最重要的功能:背单词、查单词、修改用户信息。
背单词:点击背单词的按钮之后,会让用户选择记忆模式,随后进行背单词。
查单词:查单词允许用户增删改查单词并同步到txt文件。
修改用户信息:修改用户信息允许用户改密码、忘记密码。
另外软件登录页面和主页面需要有背景图片。
解决方案:
1、界面设计
界面用Swing组件,对组件的点击以及滑动用AWT的事件处理
2、文本文件的输入输出
用File类、PrintWriter类、Scanner类解决
在此碰到了一个更新文本文件的问题的解决方案:更新文本文件的数据
3、弹窗的设计
让此窗口消失,弹窗设为可见
主要的功能和面板实现:
1、登录界面
package exercise;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class LoginFrame {
public LoginFrame(User user){
JFrame jFrame = new JFrame("Login");
jFrame.setLayout(new FlowLayout());
JLabel accountLabel = new JLabel("Account:");
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(2,1));
JPanel panel1 = new JPanel();
JTextField accountTextField = new JTextField(18);
JLabel passwordLabel = new JLabel("Password:");
JPasswordField passwordTextField = new JPasswordField(18) ;
JButton enterButton = new JButton("Enter");
JButton registeredButton = new JButton("Registered");
//给登录界面窗口添加背景图片
ImageIcon imageIcon = new ImageIcon("src\\222.png");
JLabel jLabel = new JLabel(imageIcon);
jFrame.getLayeredPane().add(jLabel,new Integer(Integer.MIN_VALUE));
jLabel.setBounds(20,0,imageIcon.getIconWidth(),imageIcon.getIconHeight());
Container container = jFrame.getContentPane();
((JPanel)container).setOpaque(false);
//点击登录界面的确定按钮,若密码正确则将进入主页面,否则重新输入
enterButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(user.getPassword().equals(passwordTextField.getText()) && user.getAccount().equals(accountTextField.getText())){
new Menu(user);
jFrame.setVisible(false);
}
else{
new PasswordErrorFrame();
}
}
});
//点击注册按钮进入到注册页面,此页面消失
registeredButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new RegisteredFrame(user);
jFrame.setVisible(false);
}
});
panel.add(accountLabel);
panel.add(accountTextField);
panel.add(passwordLabel);
panel.add(passwordTextField);
panel1.add(enterButton);
panel1.add(registeredButton);
jFrame.add(panel);
jFrame.add(panel1);
jFrame.setBounds(550,250,400,150);
jFrame.setVisible(true);
}
}
2、注册界面
package exercise;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.security.cert.PolicyNode;
public class RegisteredFrame {
public RegisteredFrame(User user){
JFrame jFrame = new JFrame("Registered");
jFrame.setLayout(new GridLayout(4,1));
JPanel jPanel1 = new JPanel(new FlowLayout());
JPanel jPanel2 = new JPanel(new FlowLayout());
JPanel jPanel3 = new JPanel(new FlowLayout());
JPanel jPanel4 = new JPanel(new FlowLayout());
JLabel accountLabel = new JLabel("Account:");
JTextField accountTextField = new JTextField(18);
JLabel passwordLabel = new JLabel("Password:");
JPasswordField passwordTextField = new JPasswordField(18);
JLabel checkPasswordLabel = new JLabel("Re-check password:");
JPasswordField checkPasswordTextField = new JPasswordField(18);
JButton enterButton = new JButton("Enter");
JButton returnButton = new JButton("Return");
//输入完信息后点击确定按钮,检测两次输入的密码是否一致,若一致则注册成功,弹窗注册成功信息,否则弹出密码不一致窗口
enterButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(checkPasswordTextField.getText().equals(passwordTextField.getText()) && (passwordTextField.getText().length()!=0 && checkPasswordTextField.getText().length()!=0) ){
//注册成功之后,同步更改用户名和密码
user.setAccount(accountTextField.getText());
user.setPassword(passwordTextField.getText());
//弹出注册成功的窗口
new RegisteredState(user,true,jFrame);
}
else{
//弹出密码不一致窗口
new RegisteredState(user,false,jFrame);
}
}
});
//点击返回键,返回到登录界面,此页面消失
returnButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jFrame.setVisible(false);
new LoginFrame(user);
}
});
jPanel1.add(accountLabel);
jPanel1.add(accountTextField);
jPanel2.add(passwordLabel);
jPanel2.add(passwordTextField);
jPanel3.add(checkPasswordLabel);
jPanel3.add(checkPasswordTextField);
jPanel4.add(enterButton);
jPanel4.add(returnButton);
jFrame.add(jPanel1);
jFrame.add(jPanel2);
jFrame.add(jPanel3);
jFrame.add(jPanel4);
jFrame.setVisible(true);
jFrame.setBounds(550,250,400,150);
}
}
3. 软件的主菜单界面
package exercise;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
public class Menu {
public Menu(User user) {
JFrame frame = new JFrame("背单词软件");
frame.setLayout(new FlowLayout());
JPanel panel = new JPanel(new GridLayout(1,4));
JButton wordListButton = new JButton("WordList");
JButton userButton = new JButton("User");
JButton button = new JButton("Memorize");
JButton returnButton = new JButton("Rreturn Login");
//为主页面添加背景
ImageIcon imageIcon = new ImageIcon("src\\2.gif");
JLabel jLabel = new JLabel(imageIcon);
frame.getLayeredPane().add(jLabel,new Integer(Integer.MIN_VALUE));
jLabel.setBounds(0,0,imageIcon.getIconWidth(),imageIcon.getIconHeight());
Container container = frame.getContentPane();
((JPanel)container).setOpaque(false);
//设置主页面的三个板块的字体颜色和字体样式
wordListButton.setForeground(Color.blue);
userButton.setForeground(Color.blue);
button.setForeground(Color.blue);
returnButton.setForeground(Color.blue);
wordListButton.setFont(new Font("微软雅黑",Font.BOLD,16));
userButton.setFont(new Font("微软雅黑",Font.BOLD,16));
button.setFont(new Font("微软雅黑",Font.BOLD,16));
returnButton.setFont(new Font("微软雅黑",Font.BOLD,16));
/*WordList界面
点击WordList,进入到单词表的窗口,此页面消失
*/
wordListButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try{
new WordList(user);
frame.setVisible(false);
}
catch (Exception exception){
exception.printStackTrace();
}
}
});
returnButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new LoginFrame(user);
frame.setVisible(false);
}
});
/*User界面
点击User,进入到单词表的窗口,此页面消失
*/
userButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new UserFrame(user);
frame.setVisible(false);
}
});
/*背单词
点击Memorize,进入到单词表的窗口,此页面消失
*/
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
frame.setVisible(false);
new SelectMemorizeModel(user,frame);
}
});
panel.add(wordListButton);
frame.add(button);
panel.add(userButton);
frame.add(panel);
frame.add(returnButton);
frame.setVisible(true);
// frame.setSize(700,800);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(650,250,300,300);
}
}
4. 软件的单词表界面
package exercise;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
public class WordList {
public WordList(User user) throws IOException {
JFrame jFrame = new JFrame("User`s WordList");
jFrame.setLayout(new FlowLayout());
JList list = new JList();
Panel panel = new Panel();
JButton returnButton = new JButton("return menu");
JButton addButton = new JButton("add new word");
JScrollPane jScrollPane = new JScrollPane(list);
//设置下滑组件的属性
jScrollPane.setSize(350,300);
jScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
jScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
//给单词表添加背景
ImageIcon imageIcon = new ImageIcon("src\\wordlist.webp");
JLabel jLabel = new JLabel(imageIcon);
jFrame.getLayeredPane().add(jLabel,new Integer(Integer.MIN_VALUE));
jLabel.setBounds(0,0,imageIcon.getIconWidth(),imageIcon.getIconHeight());
Container container = jFrame.getContentPane();
((JPanel)container).setOpaque(false);
//点击return按钮返回到主页面
returnButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new Menu(user);
jFrame.setVisible(false);
}
});
//点击add new word按钮添加单词
addButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new AddFrame(user,jFrame);
}
});
//将文件中的单词显示到界面上
String[] wordList = new String[300000];
list.setListData(StoreWordList(wordList));
//设置选中时候的颜色宽度
list.setPreferredSize(new Dimension(350,300));
//设置列表可以多选
list.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
//点击列表中的一个word弹出删、改、取消操作窗口
list.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
if(!e.getValueIsAdjusting()){
ListModel<String> listModel = list.getModel();
new Operation(user,listModel.getElementAt(list.getSelectedIndex()),jFrame);
}
}
});
panel.add(jScrollPane);
jFrame.add(returnButton,BorderLayout.NORTH);
jFrame.add(panel,BorderLayout.CENTER);
jFrame.add(addButton,BorderLayout.SOUTH);
jFrame.setBounds(600,200,400,300);
jFrame.setVisible(true);
}
//将文件中的单词储存在字符串数组中并且返回
private String[] StoreWordList(String[] wordList) throws IOException{
int count = 0;
File file = new File("WordList.txt");
Scanner scanner = new Scanner(file);
while(scanner.hasNext()){
wordList[count ++] = scanner.nextLine();
}
scanner.close();
return wordList;
}
}
5.软件的选择记忆模式界面
package exercise;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
/*
Introduction:
此类为选择记忆模式类,逻辑如下:
一个单选框,选择随机还是顺序,再点击确认进入到背单词的界面中
*/
public class SelectMemorizeModel {
public SelectMemorizeModel(User user,JFrame superFrame){
JFrame jFrame = new JFrame("select model");
jFrame.setLayout(new GridLayout(2,1));
Panel panel1 = new Panel(new FlowLayout());
Panel panel2 = new Panel(new FlowLayout());
JButton button1 = new JButton("Enter");
JRadioButton jButton1 = new JRadioButton("random");
JRadioButton jButton2 = new JRadioButton("ordered");
JButton button2 = new JButton("Cancel");
JLabel jLabel = new JLabel("select model:");
ButtonGroup gp = new ButtonGroup();
//回到主页面
button2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
superFrame.setVisible(true);
jFrame.setVisible(false);
}
});
//将两个单选按钮加入到一个按钮组
gp.add(jButton1);
gp.add(jButton2);
//进入到背单词页面
button1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(jButton1.isSelected()){
try {
new Memorize(user,true);
} catch (FileNotFoundException | InterruptedException fileNotFoundException) {
fileNotFoundException.printStackTrace();
}
}
else{
try {
new Memorize(user,false);
} catch (FileNotFoundException | InterruptedException fileNotFoundException) {
fileNotFoundException.printStackTrace();
}
}
jFrame.setVisible(false);
superFrame.setVisible(false);
}
});
panel1.add(jLabel);
panel1.add(jButton1);
panel1.add(jButton2);
panel2.add(button1);
panel2.add(button2);
jFrame.add(panel1);
jFrame.add(panel2);
jFrame.setVisible(true);
jFrame.setBounds(650,250,250,250);
}
}
6.软件的背单词界面
package exercise;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Random;
import java.util.Scanner;
/*
Introduction:
两个参数,一个user,一个flag
user:用户对象
flag:true为随机,false为顺序
此类为背单词类,逻辑如下:
顺序:从第一个到最后一个从文本中一个一个读
随机:创建一个随机数,指定文本的第几个单词
*/
public class Memorize {
//按照顺序背,现在是第 count 个单词
static int count = 0;
//flag为true就是选中random
public Memorize(User user,boolean flag) throws FileNotFoundException, InterruptedException {
JFrame jFrame = new JFrame("Memorize");
JButton enterButton = new JButton("Enter");
JButton pauseButton = new JButton("pause");
JPanel jPanel1 = new JPanel(new FlowLayout());
JPanel jPanel2 = new JPanel();
JPanel jPanel3 = new JPanel(new FlowLayout());
jFrame.setLayout(new GridLayout(4,1));
JLabel engilsh = new JLabel("Word:");
JLabel chinese = new JLabel("Chinese mean:");
JTextField jTextField = new JTextField(8);
enterButton.setVisible(true);
String answer = new String();
//顺序
if(flag == false) {
File file = new File("WordList.txt");
Scanner scanner = new Scanner(file);
for (int i = 0; i < Memorize.count; i++) {
if (scanner.hasNext()) {
scanner.nextLine();
}
}
if (scanner.hasNext()) {
String eg = scanner.next();
engilsh.setText("Word:" + eg);
} else {
new AllIsOkDialog(user, jFrame, flag);
}
String ch = scanner.next();
answer = ch;
//点击确认,系统判定并弹窗是否回答正确
enterButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(jTextField.getText().equals(ch)){
new YesDialog(user,jFrame,flag);
}
else{
new NoDialog(user,jFrame,flag);
}
}
});
}
//随机
else{
File file = new File("WordList.txt");
Scanner scanner = new Scanner(file);
int id =(int) ((Math.random())* (user.wordListNumber) + 1);
for(int i = 0; i < id - 1; i++){
if (scanner.hasNext()) {
scanner.nextLine();
}
}
if (scanner.hasNext()) {
String eg = scanner.next();
engilsh.setText("Word:" + eg);
} else {
new AllIsOkDialog(user, jFrame, flag);
}
//answer
String ch = scanner.next();
answer = ch;
//点击确认,系统判定并弹窗是否回答正确
enterButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(jTextField.getText().equals(ch)){
new YesDialog(user,jFrame,flag);
}
else{
new NoDialog(user,jFrame,flag);
}
}
});
}
//点击终止返回到主页面
pauseButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new Menu(user);
jFrame.setVisible(false);
}
});
jPanel1.add(engilsh);
jPanel3.add(chinese);
jPanel3.add(jTextField);
jPanel2.add(enterButton);
jPanel2.add(pauseButton);
jFrame.add(jPanel1);
jFrame.add(jPanel3);
jFrame.add(jPanel2);
jFrame.setVisible(true);
jFrame.setBounds(650,250,250,250);
}
}
7.软件的用户信息界面
package exercise;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class UserFrame {
public UserFrame(User user){
JFrame jFrame = new JFrame("User Information");
Panel panel = new Panel(new FlowLayout());
JLabel accountLabel = new JLabel("account:" + user.getAccount());
JLabel boundPhoneLabel = new JLabel("boundphone:" + user.getBoundPhone());
JButton button = new JButton("return");
JButton button1 = new JButton("change password");
JButton button2 = new JButton("forget password");
//返回到主页面
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new Menu(user);
jFrame.setVisible(false);
}
});
//进入修改密码界面
button1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jFrame.setVisible(false);
new changePassword(user);
}
});
//进入忘记密码界面
button2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new ForgetPasswordFrame(user);
jFrame.setVisible(false);
}
});
/*
ImageIcon imageIcon = new ImageIcon("src\\2.gif");
JLabel jLabel = new JLabel(imageIcon);
jFrame.getLayeredPane().add(jLabel,new Integer(Integer.MIN_VALUE));
jLabel.setBounds(0,0,imageIcon.getIconWidth(),imageIcon.getIconHeight());
Container container = jFrame.getContentPane();
((JPanel)container).setOpaque(false);
*/
panel.add(accountLabel);
panel.add(boundPhoneLabel);
panel.add(button1);
panel.add(button2);
panel.add(button);
jFrame.add(panel);
jFrame.setBounds(600,250,400,100);
jFrame.setVisible(true);
}
}