java随机数抽奖程序界面设计多用户输入
时间: 2023-12-10 13:41:45 浏览: 86
这里给出一个简单的 Java 随机数抽奖程序的界面设计,包含多个用户输入功能。
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Random;
public class LuckyDraw extends JFrame implements ActionListener {
private JButton btnAddUser, btnDeleteUser, btnClearUsers, btnStartDraw;
private JTextArea txtUsers, txtResult;
private ArrayList<String> userList = new ArrayList<>();
public LuckyDraw() {
super("Lucky Draw");
// add user button
btnAddUser = new JButton("Add User");
btnAddUser.addActionListener(this);
// delete user button
btnDeleteUser = new JButton("Delete User");
btnDeleteUser.addActionListener(this);
// clear users button
btnClearUsers = new JButton("Clear Users");
btnClearUsers.addActionListener(this);
// start draw button
btnStartDraw = new JButton("Start Draw");
btnStartDraw.addActionListener(this);
// user list text area
txtUsers = new JTextArea(10, 20);
txtUsers.setEditable(false);
JScrollPane scrollPane1 = new JScrollPane(txtUsers);
// result text area
txtResult = new JTextArea(1, 20);
txtResult.setEditable(false);
JScrollPane scrollPane2 = new JScrollPane(txtResult);
// layout
JPanel btnPanel = new JPanel(new GridLayout(4, 1));
btnPanel.add(btnAddUser);
btnPanel.add(btnDeleteUser);
btnPanel.add(btnClearUsers);
btnPanel.add(btnStartDraw);
JPanel userPanel = new JPanel(new BorderLayout());
userPanel.add(new JLabel("Users:"), BorderLayout.NORTH);
userPanel.add(scrollPane1, BorderLayout.CENTER);
JPanel resultPanel = new JPanel(new BorderLayout());
resultPanel.add(new JLabel("Result:"), BorderLayout.NORTH);
resultPanel.add(scrollPane2, BorderLayout.CENTER);
JPanel mainPanel = new JPanel(new BorderLayout());
mainPanel.add(userPanel, BorderLayout.WEST);
mainPanel.add(resultPanel, BorderLayout.EAST);
mainPanel.add(btnPanel, BorderLayout.SOUTH);
add(mainPanel);
setSize(400, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
new LuckyDraw();
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btnAddUser) {
String name = JOptionPane.showInputDialog("Enter user name:");
if (name != null && !name.isEmpty()) {
userList.add(name);
updateUsers();
}
} else if (e.getSource() == btnDeleteUser) {
if (userList.size() > 0) {
String name = JOptionPane.showInputDialog("Enter user name to delete:");
if (name != null && !name.isEmpty()) {
userList.remove(name);
updateUsers();
}
}
} else if (e.getSource() == btnClearUsers) {
userList.clear();
updateUsers();
} else if (e.getSource() == btnStartDraw) {
if (userList.size() > 0) {
Random rand = new Random();
int index = rand.nextInt(userList.size());
String winner = userList.get(index);
txtResult.setText(winner + " wins!");
} else {
txtResult.setText("No users!");
}
}
}
private void updateUsers() {
txtUsers.setText("");
for (String user : userList) {
txtUsers.append(user + "\n");
}
}
}
```
这个程序的主要界面包含一个 "Users" 的文本区域,其中会显示当前添加的用户,以及一个 "Result" 的文本区域,用于显示抽奖结果。程序还包含四个按钮,分别是 "Add User"、"Delete User"、"Clear Users" 和 "Start Draw",分别用于添加用户、删除用户、清空用户列表和开始抽奖。用户列表和抽奖结果都是通过 JTextArea 控件实现的,而按钮则通过 JButton 控件实现。程序还包含一个 userList 的 ArrayList,用于保存用户列表。在每次添加、删除和清空用户列表时,都需要更新文本区域的内容,这个操作是通过 updateUsers() 方法实现的。在开始抽奖时,程序会随机选择一个用户作为中奖者,并将其显示在 "Result" 文本区域中。
阅读全文