用Java代码完成以下任务 多窗口卖票情况进度显示: (1)界面上显示两个窗口卖票情况,第一个窗口分配的票数是107,窗口名称是LK _2_215_1。第二个窗口分配的票数是108,窗口名称是LK _2_215_2. 每个窗口内容包括 1.窗口名称,2.该窗口分配的票数,3.已卖票数(用进度条加数字显示) (2)每个窗口每次卖出的票数由随机数指定, (3)启动卖票行为,由每次通过按键手工启动
时间: 2024-02-17 20:05:19 浏览: 55
以下是用Java代码实现多窗口卖票情况进度显示的示例代码:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
public class TicketSellingSystem extends JFrame implements ActionListener {
private JLabel window1Name;
private JLabel window2Name;
private JLabel window1TicketCount;
private JLabel window2TicketCount;
private JLabel window1SoldCount;
private JLabel window2SoldCount;
private JProgressBar window1ProgressBar;
private JProgressBar window2ProgressBar;
private JButton sellButton;
private int window1TicketTotal = 107;
private int window2TicketTotal = 108;
private int window1TicketSold = 0;
private int window2TicketSold = 0;
private Random random = new Random();
public TicketSellingSystem() {
setTitle("票务销售系统");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 300);
setLocationRelativeTo(null);
setLayout(new GridLayout(3, 2));
window1Name = new JLabel("LK_2_215_1");
add(window1Name);
window2Name = new JLabel("LK_2_215_2");
add(window2Name);
window1TicketCount = new JLabel("票数:" + window1TicketTotal);
add(window1TicketCount);
window2TicketCount = new JLabel("票数:" + window2TicketTotal);
add(window2TicketCount);
window1ProgressBar = new JProgressBar(0, window1TicketTotal);
window1ProgressBar.setStringPainted(true);
add(window1ProgressBar);
window2ProgressBar = new JProgressBar(0, window2TicketTotal);
window2ProgressBar.setStringPainted(true);
add(window2ProgressBar);
window1SoldCount = new JLabel("已售:" + window1TicketSold);
add(window1SoldCount);
window2SoldCount = new JLabel("已售:" + window2TicketSold);
add(window2SoldCount);
sellButton = new JButton("卖票");
sellButton.addActionListener(this);
add(sellButton);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == sellButton) {
if (window1TicketSold < window1TicketTotal && window2TicketSold < window2TicketTotal) {
int window1SellNum = random.nextInt(5) + 1; // 随机卖出1-5张票
int window2SellNum = random.nextInt(5) + 1;
if (window1TicketSold + window1SellNum > window1TicketTotal) {
window1SellNum = window1TicketTotal - window1TicketSold;
}
if (window2TicketSold + window2SellNum > window2TicketTotal) {
window2SellNum = window2TicketTotal - window2TicketSold;
}
window1TicketSold += window1SellNum;
window2TicketSold += window2SellNum;
window1TicketCount.setText("票数:" + window1TicketTotal);
window2TicketCount.setText("票数:" + window2TicketTotal);
window1SoldCount.setText("已售:" + window1TicketSold);
window2SoldCount.setText("已售:" + window2TicketSold);
window1ProgressBar.setValue(window1TicketSold);
window2ProgressBar.setValue(window2TicketSold);
} else {
JOptionPane.showMessageDialog(null, "票已售完!");
}
}
}
public static void main(String[] args) {
new TicketSellingSystem();
}
}
```
运行该程序可以看到一个包含两个窗口卖票情况的界面,每次点击“卖票”按钮会随机卖出1-5张票,并更新窗口显示的已卖票数和进度条。当票已售完时会弹出提示框。
注意:该程序只是一个示例,实际应用中还需要考虑线程安全、用户界面交互等方面的问题。
阅读全文