帮我用java编写一个图形界面程序,完成简单的火车票购买。假设火车站有:南宁、柳州、玉林、百色、北海。用户指定火车票的乘客名字、起点、终点、日期时间。要求起点、终点用组合框实现,在一个日历窗口上选择日期和时刻。当用户输入完毕,用一个弹出窗口显示完整的输入信息。
时间: 2023-07-10 19:19:49 浏览: 41
好的,我可以帮你完成这个任务。下面是代码实现:
```java
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JButton;
import com.toedter.calendar.JCalendar;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.awt.event.ActionEvent;
public class TrainTicket extends JFrame {
private JPanel contentPane;
private JComboBox<String> startComboBox;
private JComboBox<String> endComboBox;
private JCalendar calendar;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
TrainTicket frame = new TrainTicket();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public TrainTicket() {
setTitle("火车票购买");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
startComboBox = new JComboBox<String>();
startComboBox.setBounds(10, 50, 100, 21);
startComboBox.addItem("南宁");
startComboBox.addItem("柳州");
startComboBox.addItem("玉林");
startComboBox.addItem("百色");
startComboBox.addItem("北海");
contentPane.add(startComboBox);
JLabel labelFrom = new JLabel("起点:");
labelFrom.setBounds(10, 30, 54, 15);
contentPane.add(labelFrom);
endComboBox = new JComboBox<String>();
endComboBox.setBounds(10, 110, 100, 21);
endComboBox.addItem("南宁");
endComboBox.addItem("柳州");
endComboBox.addItem("玉林");
endComboBox.addItem("百色");
endComboBox.addItem("北海");
contentPane.add(endComboBox);
JLabel labelTo = new JLabel("终点:");
labelTo.setBounds(10, 90, 54, 15);
contentPane.add(labelTo);
calendar = new JCalendar();
calendar.setBounds(150, 50, 200, 150);
contentPane.add(calendar);
JLabel labelTime = new JLabel("日期和时刻:");
labelTime.setBounds(150, 30, 100, 15);
contentPane.add(labelTime);
JButton button = new JButton("购买");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = calendar.getDate();
String