java编写一个基于图形用户界面的火车票购票模拟系统。该系统主界面包括三个售票窗口区域,以及两个按钮: “开始售票” 和“退出系统”。点击“开始售票”按钮,三个窗口同时开始售票(启动三个线程同时售票),三个售票窗口动态的显示正在售票及剩余票量信息。若票全部售完(总票量在代码中自行指定),将弹出消息框,提示“车票已售完”,点击“退出系统”按钮时,系统退出。
时间: 2024-02-27 18:59:01 浏览: 107
好的,以下是一个示例Java代码,使用Java Swing库来实现该系统:
```java
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.concurrent.atomic.AtomicInteger;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class TicketSystem {
private JFrame frame;
private JLabel label1;
private JLabel label2;
private JLabel label3;
private JButton startButton;
private JButton quitButton;
private AtomicInteger ticketsSold;
private int totalTickets;
private boolean isSelling;
public TicketSystem(int totalTickets) {
this.totalTickets = totalTickets;
this.ticketsSold = new AtomicInteger(0);
this.isSelling = false;
createUI();
}
private void createUI() {
frame = new JFrame("火车票购票模拟系统");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
label1 = new JLabel("售票窗口1: 0/" + totalTickets);
panel.add(label1);
label2 = new JLabel("售票窗口2: 0/" + totalTickets);
panel.add(label2);
label3 = new JLabel("售票窗口3: 0/" + totalTickets);
panel.add(label3);
startButton = new JButton("开始售票");
startButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
startSelling();
}
});
panel.add(startButton);
quitButton = new JButton("退出系统");
quitButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
quit();
}
});
panel.add(quitButton);
frame.getContentPane().add(panel, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
private void startSelling() {
if (!isSelling) {
isSelling = true;
Thread t1 = new Thread(new TicketSeller(1));
Thread t2 = new Thread(new TicketSeller(2));
Thread t3 = new Thread(new TicketSeller(3));
t1.start();
t2.start();
t3.start();
}
}
private void updateUI(int window) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
if (window == 1) {
label1.setText("售票窗口1: " + ticketsSold + "/" + totalTickets);
} else if (window == 2) {
label2.setText("售票窗口2: " + ticketsSold + "/" + totalTickets);
} else {
label3.setText("售票窗口3: " + ticketsSold + "/" + totalTickets);
}
}
});
}
private void quit() {
int result = JOptionPane.showConfirmDialog(frame, "确定退出系统吗?", "退出", JOptionPane.YES_NO_OPTION);
if (result == JOptionPane.YES_OPTION) {
System.exit(0);
}
}
private class TicketSeller implements Runnable {
private int window;
public TicketSeller(int window) {
this.window = window;
}
@Override
public void run() {
while (ticketsSold.get() < totalTickets) {
// 模拟售票过程
// ...
// 更新票数
ticketsSold.incrementAndGet();
// 更新界面
updateUI(window);
}
JOptionPane.showMessageDialog(frame, "车票已售完");
isSelling = false;
}
}
public static void main(String[] args) {
TicketSystem system = new TicketSystem(100);
}
}
```
上述代码中的 `TicketSeller` 类实现了售票线程,通过 `AtomicInteger` 类来实现线程安全的票数计数。在售票过程中,通过 `SwingUtilities.invokeLater` 方法来更新界面。在点击“退出系统”按钮时,弹出确认对话框,确认后退出程序。
阅读全文