0% found this document useful (0 votes)
4 views

ndjffn

This document provides Java Swing code for a voting information management system, featuring user login, registration, voting, and statistics functionalities. User data is stored in a serialized HashMap within 'users.dat', while voting data is saved in 'votes.dat'. The system allows users to register, vote for candidates, and view voting results, with a hardcoded list of three candidates.

Uploaded by

niuyishou6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

ndjffn

This document provides Java Swing code for a voting information management system, featuring user login, registration, voting, and statistics functionalities. User data is stored in a serialized HashMap within 'users.dat', while voting data is saved in 'votes.dat'. The system allows users to register, vote for candidates, and view voting results, with a hardcoded list of three candidates.

Uploaded by

niuyishou6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

bsnddn

以下是使用 Java Swing 实现的投票信息管理系统代码,包含登录、注册、主界面、投票和统计功能:

```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.HashMap;

public class VotingSystem {


public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new LoginFrame().setVisible(true));
}
}

class LoginFrame extends JFrame {


private JTextField usernameField = new JTextField();
private JPasswordField passwordField = new JPasswordField();
private HashMap<String, String> users;

public LoginFrame() {
setTitle("用户登录");
setSize(350, 250);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
loadUsers();

JPanel panel = new JPanel(new GridLayout(4, 1, 10, 10));


panel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

JPanel inputPanel = new JPanel(new GridLayout(2, 2, 10, 10));


inputPanel.add(new JLabel("用户名:"));
inputPanel.add(usernameField);
inputPanel.add(new JLabel("密码:"));
inputPanel.add(passwordField);

JButton loginBtn = new JButton("登录");


JButton registerBtn = new JButton("注册");

loginBtn.addActionListener(e -> attemptLogin());


registerBtn.addActionListener(e -> {
new RegisterFrame().setVisible(true);
dispose();
});

panel.add(inputPanel);
panel.add(loginBtn);
panel.add(registerBtn);

add(panel);
}

private void attemptLogin() {


String username = usernameField.getText();
String password = new String(passwordField.getPassword());

if (users.containsKey(username) && users.get(username).equals(password)) {


new MainFrame().setVisible(true);
dispose();
} else {
JOptionPane.showMessageDialog(this, "无效的用户名或密码!");
}
}

@SuppressWarnings("unchecked")
private void loadUsers() {
try (ObjectInputStream ois = new ObjectInputStream(new
FileInputStream("users.dat"))) {
users = (HashMap<String, String>) ois.readObject();
} catch (FileNotFoundException e) {
users = new HashMap<>();
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
}

class RegisterFrame extends JFrame {


private JTextField usernameField = new JTextField();
private JPasswordField passwordField = new JPasswordField();
private JPasswordField confirmField = new JPasswordField();
private HashMap<String, String> users;

public RegisterFrame() {
setTitle("用户注册");
setSize(350, 300);
setLocationRelativeTo(null);
loadUsers();

JPanel panel = new JPanel(new GridLayout(5, 1, 10, 10));


panel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

JPanel inputPanel = new JPanel(new GridLayout(3, 2, 10, 10));


inputPanel.add(new JLabel("用户名:"));
inputPanel.add(usernameField);
inputPanel.add(new JLabel("密码:"));
inputPanel.add(passwordField);
inputPanel.add(new JLabel("确认密码:"));
inputPanel.add(confirmField);

JButton registerBtn = new JButton("注册");


JButton backBtn = new JButton("返回登录");

registerBtn.addActionListener(e -> attemptRegister());


backBtn.addActionListener(e -> {
new LoginFrame().setVisible(true);
dispose();
});

panel.add(inputPanel);
panel.add(registerBtn);
panel.add(backBtn);

add(panel);
}

private void attemptRegister() {


String username = usernameField.getText();
String password = new String(passwordField.getPassword());
String confirm = new String(confirmField.getPassword());

if (username.isEmpty() || password.isEmpty()) {
JOptionPane.showMessageDialog(this, "用户名和密码不能为空!");
return;
}

if (!password.equals(confirm)) {
JOptionPane.showMessageDialog(this, "两次输入的密码不一致!");
return;
}

if (users.containsKey(username)) {
JOptionPane.showMessageDialog(this, "用户名已存在!");
} else {
users.put(username, password);
saveUsers();
JOptionPane.showMessageDialog(this, "注册成功!");
new LoginFrame().setVisible(true);
dispose();
}
}

@SuppressWarnings("unchecked")
private void loadUsers() {
try (ObjectInputStream ois = new ObjectInputStream(new
FileInputStream("users.dat"))) {
users = (HashMap<String, String>) ois.readObject();
} catch (FileNotFoundException e) {
users = new HashMap<>();
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}

private void saveUsers() {


try (ObjectOutputStream oos = new ObjectOutputStream(new
FileOutputStream("users.dat"))) {
oos.writeObject(users);
} catch (IOException e) {
e.printStackTrace();
}
}
}

class MainFrame extends JFrame {


public MainFrame() {
setTitle("主界面");
setSize(300, 200);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel panel = new JPanel(new GridLayout(2, 1, 10, 10));


panel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

JButton voteBtn = new JButton("开始投票");


JButton statsBtn = new JButton("查看统计");
voteBtn.addActionListener(e -> new VoteFrame().setVisible(true));
statsBtn.addActionListener(e -> new StatsFrame().setVisible(true));

panel.add(voteBtn);
panel.add(statsBtn);

add(panel);
}
}

class VoteFrame extends JFrame {


private HashMap<String, Integer> votes;
private JComboBox<String> candidateBox;

public VoteFrame() {
setTitle("投票界面");
setSize(300, 150);
setLocationRelativeTo(null);
loadVotes();

JPanel panel = new JPanel(new BorderLayout(10, 10));


panel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

candidateBox = new JComboBox<>(new String[]{"候选人 A", "候选人 B", "候选人 C"});


JButton submitBtn = new JButton("提交投票");

submitBtn.addActionListener(e -> {
String candidate = (String) candidateBox.getSelectedItem();
votes.put(candidate, votes.getOrDefault(candidate, 0) + 1);
saveVotes();
JOptionPane.showMessageDialog(this, "投票成功!");
dispose();
});

panel.add(candidateBox, BorderLayout.CENTER);
panel.add(submitBtn, BorderLayout.SOUTH);

add(panel);
}

@SuppressWarnings("unchecked")
private void loadVotes() {
try (ObjectInputStream ois = new ObjectInputStream(new
FileInputStream("votes.dat"))) {
votes = (HashMap<String, Integer>) ois.readObject();
} catch (FileNotFoundException e) {
initializeVotes();
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}

private void initializeVotes() {


votes = new HashMap<>();
votes.put("候选人 A", 0);
votes.put("候选人 B", 0);
votes.put("候选人 C", 0);
}
private void saveVotes() {
try (ObjectOutputStream oos = new ObjectOutputStream(new
FileOutputStream("votes.dat"))) {
oos.writeObject(votes);
} catch (IOException e) {
e.printStackTrace();
}
}
}

class StatsFrame extends JFrame {


public StatsFrame() {
setTitle("统计界面");
setSize(300, 200);
setLocationRelativeTo(null);

JTextArea textArea = new JTextArea();


textArea.setEditable(false);

try (ObjectInputStream ois = new ObjectInputStream(new


FileInputStream("votes.dat"))) {
@SuppressWarnings("unchecked")
HashMap<String, Integer> votes = (HashMap<String, Integer>)
ois.readObject();
for (String candidate : votes.keySet()) {
textArea.append(candidate + ": " + votes.get(candidate) + "票\n");
}
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}

add(new JScrollPane(textArea));
}
}
```

代码说明:

1. 系统使用 Swing 组件实现 GUI 界面


2. 用户数据存储在 users.dat 文件中(序列化的 HashMap)
3. 投票数据存储在 votes.dat 文件中(序列化的 HashMap)
4. 包含以下主要功能:
- 用户登录/注册
- 投票界面(支持三个候选人)
- 统计界面显示投票结果
- 主界面导航

使用说明:
1. 首次运行会自动创建数据文件
2. 需要先注册用户才能登录
3. 投票结果会自动保存并在统计界面显示
4. 候选人名单硬编码为三个候选人,可根据需要修改

注意:运行时会在当前目录生成 users.dat 和 votes.dat 两个数据文件,请不要手动修改这两个文件。

You might also like