用java实现下列功能: 多窗口卖票情况进度显示: (1)界面上显示两个以上的窗口卖票情况,每个窗口内容包括窗口名称(NJL_2_213_A本人姓名的首字母,班级号,对应学号后三位和窗口号码),该窗口分配的票数,已卖票数(建议用进度条加数字显示); (2)所有窗口分配的票数总和等于自己学号的后三位数字,每个窗口每次卖出的票数由随机数指定; (3)启动卖票行为,由每次通过按键手工启动或通过自动按钮一键完成(建议自动),窗口的每次卖票间隔不少于1秒。
时间: 2024-02-17 16:05:30 浏览: 67
以下是用 Java 实现上述功能的代码:
```java
import javax.swing.*;
import java.awt.*;
import java.util.Random;
public class TicketSellingSystem extends JFrame {
private static final int FRAME_WIDTH = 800;
private static final int FRAME_HEIGHT = 600;
private static final int NUM_WINDOWS = 3;
private static final int MAX_TICKETS_PER_WINDOW = 50;
private Window[] windows;
private JButton manualBtn;
private JButton autoBtn;
private JProgressBar[] progressBars;
public TicketSellingSystem() {
setTitle("多窗口卖票情况进度显示");
setSize(FRAME_WIDTH, FRAME_HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create window objects
windows = new Window[NUM_WINDOWS];
int totalTicketCount = Integer.parseInt(getStudentId().substring(5));
Random rand = new Random();
for (int i = 0; i < NUM_WINDOWS; i++) {
int windowNum = i + 1;
int maxTickets = rand.nextInt(MAX_TICKETS_PER_WINDOW) + 1;
windows[i] = new Window("NJL_2_213_A-W" + windowNum, maxTickets, totalTicketCount);
}
// Create progress bar components
progressBars = new JProgressBar[NUM_WINDOWS];
for (int i = 0; i < NUM_WINDOWS; i++) {
progressBars[i] = new JProgressBar(0, windows[i].getMaxTickets());
progressBars[i].setValue(windows[i].getSoldTickets());
progressBars[i].setStringPainted(true);
}
// Create manual and auto buttons
manualBtn = new JButton("手动卖票");
autoBtn = new JButton("自动卖票");
// Add components to frame
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(NUM_WINDOWS + 2, 1));
for (int i = 0; i < NUM_WINDOWS; i++) {
panel.add(windows[i].getLabel());
panel.add(progressBars[i]);
}
panel.add(manualBtn);
panel.add(autoBtn);
add(panel);
// Add action listeners to buttons
manualBtn.addActionListener(event -> sellTicketsManually());
autoBtn.addActionListener(event -> sellTicketsAutomatically());
}
private void sellTicketsManually() {
for (int i = 0; i < NUM_WINDOWS; i++) {
int numSold = Integer.parseInt(JOptionPane.showInputDialog(this,
"请输入窗口 " + (i + 1) + " 卖出的票数:"));
windows[i].sellTickets(numSold);
progressBars[i].setValue(windows[i].getSoldTickets());
}
}
private void sellTicketsAutomatically() {
for (int i = 0; i < NUM_WINDOWS; i++) {
int numSold = new Random().nextInt(windows[i].getMaxTickets() - windows[i].getSoldTickets() + 1);
windows[i].sellTickets(numSold);
progressBars[i].setValue(windows[i].getSoldTickets());
}
}
private String getStudentId() {
return "201912345678";
}
public static void main(String[] args) {
TicketSellingSystem system = new TicketSellingSystem();
system.setVisible(true);
}
private class Window {
private String name;
private int maxTickets;
private int soldTickets;
public Window(String name, int maxTickets, int totalTicketCount) {
this.name = name;
this.maxTickets = maxTickets;
this.soldTickets = 0;
if (maxTickets > totalTicketCount) {
this.maxTickets = totalTicketCount;
}
}
public String getName() {
return name;
}
public int getMaxTickets() {
return maxTickets;
}
public int getSoldTickets() {
return soldTickets;
}
public JLabel getLabel() {
return new JLabel(name + "(已售 " + soldTickets + " 张,共 " + maxTickets + " 张)");
}
public void sellTickets(int numSold) {
if (soldTickets + numSold <= maxTickets) {
soldTickets += numSold;
}
}
}
}
```
代码中创建了一个 `TicketSellingSystem` 类,用于显示多个窗口的卖票情况和实现手动或自动卖票。`Window` 类用于表示每个窗口的状态。卖票数量是通过随机数生成的,手动卖票时通过对话框输入。进度条和数字显示用 `JProgressBar` 组件实现。
阅读全文