用Java完成以下要求:多窗口卖票情况进度显示: (1)界面上显示两个以上的窗口卖票情况, 每个窗口内容包括窗口名称(LXY_1_33本人姓名的首字母,班级号,对应的学号后三位和窗口号码)该窗口分配的票数,已卖票数(建议用进度条加数字显示) (2)所有窗口分配的票数总和等于自己学号的后三位数字(133),每个窗口每次卖出的票数由随机数指定, (3)启动卖票行为,由每次通过按键手工启动或通过自动按钮一键完成(建议自动),窗口的每次卖票间隔不少于1秒。
时间: 2024-02-17 10:04:07 浏览: 68
好的,以下是Java代码实现多窗口卖票情况进度显示的示例:
```java
import javax.swing.*;
import java.awt.*;
import java.util.Random;
public class TicketSelling extends JFrame implements Runnable {
private static final int WINDOW_COUNT = 3; // 窗口数量
private static final int TICKET_COUNT = 133; // 总票数
private static final int TICKET_MAX = 10; // 每次最多卖出的票数
private static final int INTERVAL_MIN = 1000; // 每次卖票的最小时间间隔(毫秒)
private static final int INTERVAL_MAX = 3000; // 每次卖票的最大时间间隔(毫秒)
private static final String[] WINDOW_NAMES = {"LXY_1_33_1", "LXY_1_33_2", "LXY_1_33_3"}; // 窗口名称
private int[] ticketCounts = new int[WINDOW_COUNT]; // 每个窗口已卖票数
private int[] ticketTotals = new int[WINDOW_COUNT]; // 每个窗口分配的票数
private JProgressBar[] progressBars = new JProgressBar[WINDOW_COUNT]; // 进度条
private JLabel[] countLabels = new JLabel[WINDOW_COUNT]; // 已卖票数标签
private JButton startButton = new JButton("手动卖票"); // 手动卖票按钮
private JButton autoButton = new JButton("自动卖票"); // 自动卖票按钮
public TicketSelling() {
setTitle("多窗口卖票情况进度显示");
setLayout(new GridLayout(WINDOW_COUNT + 2, 1));
// 初始化各个窗口的票数和进度条
for (int i = 0; i < WINDOW_COUNT; i++) {
ticketTotals[i] = TICKET_COUNT / WINDOW_COUNT;
ticketCounts[i] = 0;
progressBars[i] = new JProgressBar(0, ticketTotals[i]);
progressBars[i].setValue(ticketCounts[i]);
countLabels[i] = new JLabel(WINDOW_NAMES[i] + " 已卖票数: " + ticketCounts[i] + "/" + ticketTotals[i]);
JPanel panel = new JPanel(new BorderLayout());
panel.add(new JLabel(WINDOW_NAMES[i]), BorderLayout.WEST);
panel.add(progressBars[i], BorderLayout.CENTER);
panel.add(countLabels[i], BorderLayout.EAST);
add(panel);
}
// 初始化按钮
JPanel buttonPanel = new JPanel(new FlowLayout());
startButton.addActionListener(e -> sellTicket());
autoButton.addActionListener(e -> new Thread(this).start());
buttonPanel.add(startButton);
buttonPanel.add(autoButton);
add(buttonPanel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500, 300);
setVisible(true);
}
// 手动卖票
private void sellTicket() {
for (int i = 0; i < WINDOW_COUNT; i++) {
int ticketCount = new Random().nextInt(TICKET_MAX) + 1;
if (ticketCounts[i] + ticketCount > ticketTotals[i]) {
ticketCount = ticketTotals[i] - ticketCounts[i];
}
ticketCounts[i] += ticketCount;
progressBars[i].setValue(ticketCounts[i]);
countLabels[i].setText(WINDOW_NAMES[i] + " 已卖票数: " + ticketCounts[i] + "/" + ticketTotals[i]);
}
}
// 自动卖票
@Override
public void run() {
autoButton.setEnabled(false);
while (true) {
try {
Thread.sleep(new Random().nextInt(INTERVAL_MAX - INTERVAL_MIN) + INTERVAL_MIN);
} catch (InterruptedException e) {
e.printStackTrace();
}
sellTicket();
boolean finished = true;
for (int i = 0; i < WINDOW_COUNT; i++) {
if (ticketCounts[i] < ticketTotals[i]) {
finished = false;
break;
}
}
if (finished) {
JOptionPane.showMessageDialog(this, "所有票已售完!");
break;
}
}
autoButton.setEnabled(true);
}
public static void main(String[] args) {
new TicketSelling();
}
}
```
这个程序创建了一个窗口,包含了三个进度条和相关的标签和按钮。手动卖票按钮可以手动卖票,自动卖票按钮可以自动卖票,每次卖票间隔在1秒到3秒之间。每个窗口的票数是随机分配的,总票数为133,每个窗口的票数之和等于133。每个窗口每次最多卖出的票数为10。
阅读全文