java编程 多自口卖票情况进度显示: (1)界面上显示两个以上的白口卖票情况, 每个盲口内容包括口口名称(WY 1 131 1本人姓名的首字母,班级号, 对应的学号后三位和自口号码), 该官口分配的票数,已卖票数(建议用进度条加数字显示) (2)所有自口分配的票数总和等于自己学号的后三位数字,每个窗口每次卖出的票数由随机数指定, (3)启动卖贝行为,由每次通过按健手工启动或通过自动按钮一键完成(建议自动),自口的每次卖票间网不少于1秒。
时间: 2024-02-17 08:04:20 浏览: 84
以下是一个可能的Java代码实现,实现了多线程卖票和进度条的显示:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TicketSeller extends JFrame {
// 窗口中的白口卖票情况数量
private static final int SELLER_COUNT = 2;
// 每个卖票情况的宽度
private static final int SELLER_WIDTH = 400;
// 每个卖票情况的高度
private static final int SELLER_HEIGHT = 100;
// 票的总数
private static final int TOTAL_TICKET_NUMBER = 131;
private JPanel[] sellers;
private JLabel[] sellerNames;
private JLabel[] sellerTicketNumbers;
private JLabel[] sellerSoldNumbers;
private JProgressBar[] sellerProgressBars;
private JButton manualButton;
private JButton autoButton;
private boolean isAuto;
public TicketSeller() {
setTitle("白口卖票情况进度显示");
// 初始化界面
initGUI();
// 初始化白口卖票情况
initSellers();
// 初始化按钮
initButtons();
// 初始化自动卖票线程
initAutoThread();
// 显示窗口
pack();
setVisible(true);
}
private void initGUI() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
setPreferredSize(new Dimension(SELLER_WIDTH * SELLER_COUNT, SELLER_HEIGHT + 100));
}
private void initSellers() {
sellers = new JPanel[SELLER_COUNT];
sellerNames = new JLabel[SELLER_COUNT];
sellerTicketNumbers = new JLabel[SELLER_COUNT];
sellerSoldNumbers = new JLabel[SELLER_COUNT];
sellerProgressBars = new JProgressBar[SELLER_COUNT];
for (int i = 0; i < SELLER_COUNT; i++) {
sellers[i] = new JPanel(new GridLayout(4, 1));
sellerNames[i] = new JLabel("WY 1 131 1" + (char) ('A' + i));
sellerTicketNumbers[i] = new JLabel("票数: " + TOTAL_TICKET_NUMBER / SELLER_COUNT);
sellerSoldNumbers[i] = new JLabel("已卖票数: 0");
sellerProgressBars[i] = new JProgressBar(0, TOTAL_TICKET_NUMBER / SELLER_COUNT);
sellers[i].add(sellerNames[i]);
sellers[i].add(sellerTicketNumbers[i]);
sellers[i].add(sellerSoldNumbers[i]);
sellers[i].add(sellerProgressBars[i]);
add(sellers[i], BorderLayout.WEST);
}
}
private void initButtons() {
manualButton = new JButton("手动卖票");
autoButton = new JButton("自动卖票");
manualButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
isAuto = false;
startSellingTickets();
}
});
autoButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
isAuto = true;
}
});
JPanel buttonPanel = new JPanel();
buttonPanel.add(manualButton);
buttonPanel.add(autoButton);
add(buttonPanel, BorderLayout.SOUTH);
}
private void initAutoThread() {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
while (true) {
if (isAuto) {
startSellingTickets();
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
thread.start();
}
private void startSellingTickets() {
for (int i = 0; i < SELLER_COUNT; i++) {
new Thread(new TicketSellingTask(i)).start();
}
}
private class TicketSellingTask implements Runnable {
private int sellerIndex;
public TicketSellingTask(int sellerIndex) {
this.sellerIndex = sellerIndex;
}
@Override
public void run() {
int ticketNumber = Math.min((int) (Math.random() * 10), TOTAL_TICKET_NUMBER / SELLER_COUNT - getSellerSoldNumber(sellerIndex));
updateSellerSoldNumber(sellerIndex, getSellerSoldNumber(sellerIndex) + ticketNumber);
updateProgressBar(sellerIndex);
}
}
private int getSellerSoldNumber(int sellerIndex) {
return Integer.parseInt(sellerSoldNumbers[sellerIndex].getText().substring(5));
}
private void updateSellerSoldNumber(int sellerIndex, int soldNumber) {
sellerSoldNumbers[sellerIndex].setText("已卖票数: " + soldNumber);
}
private void updateProgressBar(int sellerIndex) {
int soldNumber = getSellerSoldNumber(sellerIndex);
sellerProgressBars[sellerIndex].setValue(soldNumber);
sellerProgressBars[sellerIndex].setString(soldNumber + "/" + TOTAL_TICKET_NUMBER / SELLER_COUNT);
sellerProgressBars[sellerIndex].setStringPainted(true);
}
public static void main(String[] args) {
new TicketSeller();
}
}
```
代码中,使用JFrame类实现窗口界面的显示,使用JPanel类实现每个白口卖票情况的显示区域,使用JProgressBar类实现进度条的显示。在每个白口卖票情况的显示区域中,显示该口口名称、分配的票数、已卖票数和进度条。在每个卖票情况中,使用Thread类和Runnable接口实现多线程操作,模拟不同的白口卖票情况并更新进度条和已卖票数的显示。使用JButton类实现手动和自动卖票的按钮,并使用ActionListener接口监听按钮事件。在自动卖票中,使用Thread.sleep()方法实现每次卖票间隔不少于1秒的要求。最后,通过调用JFrame类的setVisible()方法显示窗口。
阅读全文