可以使用java语言帮我编写下列题目吗:编写GUI界面,模拟抢红包游戏,在界面输入红包大小和抢红包人姓名(用逗号隔开),输出系统当前时间以及每个人抢的红包大小,要求每个人抢的红包最少不能少于0.01元,精确到小数点后两位。使用UTF-8编码,jdk-19
时间: 2024-04-02 08:33:25 浏览: 60
JAVA上机实验 | 图形界面和流操作——完成一个记事本程序的编写
5星 · 资源好评率100%
好的,以下是一个简单的 GUI 界面,模拟抢红包游戏的 Java 实现:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;
public class RedPacketGameGUI implements ActionListener {
private JFrame frame;
private JLabel timeLabel;
private JLabel nameLabel;
private JLabel amountLabel;
private JTextField nameField;
private JTextField amountField;
private JButton addButton;
private JButton startButton;
private JTextArea resultArea;
private JScrollPane resultScrollPane;
private List<RedPacket> redPacketList = new ArrayList<>();
public static void main(String[] args) {
new RedPacketGameGUI().run();
}
private void run() {
// 创建窗口
frame = new JFrame("抢红包游戏");
frame.setSize(500, 400);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
// 创建顶部面板
JPanel topPanel = new JPanel(new FlowLayout());
timeLabel = new JLabel();
topPanel.add(timeLabel);
// 创建中间面板
JPanel centerPanel = new JPanel(new GridLayout(2, 2, 10, 10));
nameLabel = new JLabel("姓名:");
nameField = new JTextField();
amountLabel = new JLabel("红包大小:");
amountField = new JTextField();
centerPanel.add(nameLabel);
centerPanel.add(nameField);
centerPanel.add(amountLabel);
centerPanel.add(amountField);
// 创建底部面板
JPanel bottomPanel = new JPanel(new FlowLayout());
addButton = new JButton("添加红包");
addButton.addActionListener(this);
startButton = new JButton("开始游戏");
startButton.addActionListener(this);
bottomPanel.add(addButton);
bottomPanel.add(startButton);
// 创建结果区域
resultArea = new JTextArea();
resultArea.setEditable(false);
resultScrollPane = new JScrollPane(resultArea);
// 将面板添加到窗口中
frame.add(topPanel, BorderLayout.NORTH);
frame.add(centerPanel, BorderLayout.CENTER);
frame.add(bottomPanel, BorderLayout.SOUTH);
frame.add(resultScrollPane, BorderLayout.CENTER);
// 显示窗口
frame.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == addButton) {
// 添加红包
String name = nameField.getText().trim();
String amountStr = amountField.getText().trim();
if (name.isEmpty() || amountStr.isEmpty()) {
JOptionPane.showMessageDialog(frame, "姓名和红包大小不能为空!", "错误", JOptionPane.ERROR_MESSAGE);
return;
}
double amount = Double.parseDouble(amountStr);
if (amount < 0.01) {
JOptionPane.showMessageDialog(frame, "红包大小不能小于0.01元!", "错误", JOptionPane.ERROR_MESSAGE);
return;
}
redPacketList.add(new RedPacket(name, amount));
nameField.setText("");
amountField.setText("");
resultArea.append(name + " 添加了一个红包,大小为 " + String.format("%.2f", amount) + " 元\n");
} else if (e.getSource() == startButton) {
// 开始游戏
if (redPacketList.isEmpty()) {
JOptionPane.showMessageDialog(frame, "请先添加红包!", "错误", JOptionPane.ERROR_MESSAGE);
return;
}
resultArea.append("\n开始游戏,系统当前时间为 " + getCurrentTime() + "\n");
while (!redPacketList.isEmpty()) {
int index = new Random().nextInt(redPacketList.size());
RedPacket redPacket = redPacketList.get(index);
double amount = Math.min(redPacket.amount, getTotalAmount(redPacketList) / redPacketList.size() * 2);
redPacketList.remove(index);
redPacket.amount -= amount;
resultArea.append(redPacket.name + " 抢到了 " + String.format("%.2f", amount) + " 元\n");
}
resultArea.append("红包已经被抢完,游戏结束!");
startButton.setEnabled(false);
}
}
private double getTotalAmount(List<RedPacket> redPacketList) {
double totalAmount = 0;
for (RedPacket redPacket : redPacketList) {
totalAmount += redPacket.amount;
}
return totalAmount;
}
private String getCurrentTime() {
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
}
private class RedPacket {
String name;
double amount;
RedPacket(String name, double amount) {
this.name = name;
this.amount = amount;
}
}
}
```
这个程序使用了 Swing GUI 框架,包括顶部面板、中间面板、底部面板和结果区域。用户可以在中间面板输入抢红包人的姓名和红包大小,点击“添加红包”按钮将红包添加到列表中。当用户点击“开始游戏”按钮时,程序会随机抽取红包,计算每个人抢到的红包大小,并在结果区域输出每个人抢到的红包大小。当所有红包都被抢完后,程序会输出游戏结束的信息。
阅读全文