java抽奖程序界面设计多用户输入
时间: 2023-12-10 21:41:45 浏览: 123
用java写的抽奖程序
4星 · 用户满意度95%
以下是一个简单的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 LotteryProgram extends JFrame implements ActionListener {
private JLabel titleLabel, nameLabel, prizeLabel, winnerLabel;
private JTextField nameField;
private JButton addButton, startButton;
private JTextArea resultArea;
private ArrayList<String> nameList;
private ArrayList<String> prizeList;
private Random random;
public LotteryProgram() {
setTitle("抽奖程序");
setSize(500, 500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 创建标题
titleLabel = new JLabel("欢迎使用抽奖程序", JLabel.CENTER);
titleLabel.setFont(new Font("宋体", Font.BOLD, 24));
add(titleLabel, BorderLayout.NORTH);
// 创建输入框和按钮
nameLabel = new JLabel("请输入用户姓名:", JLabel.CENTER);
nameLabel.setFont(new Font("宋体", Font.PLAIN, 16));
nameField = new JTextField();
addButton = new JButton("添加用户");
addButton.addActionListener(this);
startButton = new JButton("开始抽奖");
startButton.addActionListener(this);
JPanel inputPanel = new JPanel();
inputPanel.setLayout(new GridLayout(1, 3));
inputPanel.add(nameLabel);
inputPanel.add(nameField);
inputPanel.add(addButton);
add(inputPanel, BorderLayout.CENTER);
// 创建结果框和抽奖按钮
resultArea = new JTextArea();
resultArea.setEditable(false);
resultArea.setFont(new Font("宋体", Font.PLAIN, 16));
JScrollPane scrollPane = new JScrollPane(resultArea);
add(scrollPane, BorderLayout.SOUTH);
prizeLabel = new JLabel("中奖名单:", JLabel.CENTER);
prizeLabel.setFont(new Font("宋体", Font.PLAIN, 16));
winnerLabel = new JLabel("", JLabel.CENTER);
winnerLabel.setFont(new Font("宋体", Font.BOLD, 20));
JPanel prizePanel = new JPanel();
prizePanel.setLayout(new GridLayout(2, 1));
prizePanel.add(prizeLabel);
prizePanel.add(winnerLabel);
add(prizePanel, BorderLayout.EAST);
random = new Random();
nameList = new ArrayList<>();
prizeList = new ArrayList<>();
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == addButton) {
String name = nameField.getText();
if (name.equals("")) {
JOptionPane.showMessageDialog(this, "请输入用户姓名!", "错误", JOptionPane.ERROR_MESSAGE);
} else {
nameList.add(name);
resultArea.append(name + "已添加\n");
nameField.setText("");
}
} else if (e.getSource() == startButton) {
if (nameList.size() == 0) {
JOptionPane.showMessageDialog(this, "请先添加用户!", "错误", JOptionPane.ERROR_MESSAGE);
} else {
int index = random.nextInt(nameList.size());
String winner = nameList.get(index);
prizeList.add(winner);
winnerLabel.setText(winner);
nameList.remove(index);
resultArea.append(winner + "获得了奖品!\n");
}
}
}
public static void main(String[] args) {
LotteryProgram lotteryProgram = new LotteryProgram();
lotteryProgram.setVisible(true);
}
}
```
这个程序界面分为三部分:
- 输入框和添加用户按钮,用于输入多个用户姓名。
- 结果框和开始抽奖按钮,用于显示中奖名单和进行抽奖操作。
- 中奖名单区域,用于显示当前中奖者的姓名。
在实现过程中,我们使用了`ArrayList`来存储多个用户姓名和中奖者姓名。在点击添加用户按钮时,程序会将输入框中的姓名添加到`nameList`中,并在结果框中显示添加成功的信息。在点击开始抽奖按钮时,程序会从`nameList`中随机选出一个中奖者,并将其从`nameList`中移除,并将其姓名显示在中奖名单区域中,并在结果框中显示中奖者的姓名。
阅读全文