You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java Swing如何将GUI用户输入选择传递至另一界面生成机票/回执

Java Swing跨GUI界面传递预订数据实现方案

实现步骤

1. 先创建预订信息实体类,统一封装所有要传递的数据

// BookingInfo.java 实体类,用来存储所有用户填写的预订信息
public class BookingInfo {
    private String firstName;
    private String lastName;
    private String fromCity;
    private String toCity;
    private String adultCount;
    private String childrenCount;
    private String bookingDate;
    private String seatClass;

    // 全参构造方法
    public BookingInfo(String firstName, String lastName, String fromCity, String toCity, String adultCount, String childrenCount, String bookingDate, String seatClass) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.fromCity = fromCity;
        this.toCity = toCity;
        this.adultCount = adultCount;
        this.childrenCount = childrenCount;
        this.bookingDate = bookingDate;
        this.seatClass = seatClass;
    }

    // 所有属性的getter方法,回执界面用来取数据
    public String getFirstName() { return firstName; }
    public String getLastName() { return lastName; }
    public String getFromCity() { return fromCity; }
    public String getToCity() { return toCity; }
    public String getAdultCount() { return adultCount; }
    public String getChildrenCount() { return childrenCount; }
    public String getBookingDate() { return bookingDate; }
    public String getSeatClass() { return seatClass; }
}

2. 修改原GUI类的点击事件,收集所有数据并跳转回执界面

你原来的actionPerformed方法只获取了姓名,补充所有控件值的获取逻辑,代码修改如下:

@Override
public void actionPerformed(ActionEvent e) {
    // 1. 收集所有用户输入、选择的内容
    String firstName = firstNameuserText.getText();
    String lastName = lastNameuserText.getText();
    String fromCity = (String) fromCombo.getSelectedItem();
    String toCity = (String) toCombo.getSelectedItem();
    String adultCount = (String) adultCombo.getSelectedItem();
    String childrenCount = (String) childrenCombo.getSelectedItem();
    String bookingDate = bookingDateText.getText();
    String seatClass = (String) classCombo.getSelectedItem();

    // 2. 封装到实体类对象
    BookingInfo bookingInfo = new BookingInfo(firstName, lastName, fromCity, toCity, adultCount, childrenCount, bookingDate, seatClass);

    // 3. 打开回执界面,把封装好的对象传过去
    new TicketReceiptGUI(bookingInfo).setVisible(true);
    // 如果需要点完按钮关闭原预订界面,先把main方法里的JFrame frame改为类的静态成员变量,再取消注释下面这行
    // frame.dispose();
}

注意:如果要调用关闭原窗口的方法,需要把原main方法里的JFrame frame提到类的静态成员变量位置,和其他控件定义放在一起即可。

3. 回执GUI示例(你可以自行修改样式为机票样式)

import javax.swing.*;
import java.awt.*;

public class TicketReceiptGUI extends JFrame {
    public TicketReceiptGUI(BookingInfo bookingInfo) {
        // 基础窗口设置
        setTitle("机票预订回执");
        setSize(400, 500);
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        JPanel panel = new JPanel();
        panel.setLayout(null);
        panel.setBackground(Color.white);
        add(panel);

        // 渲染回执内容,直接从传入的bookingInfo对象取数据
        JLabel titleLabel = new JLabel("预订成功回执");
        titleLabel.setBounds(150, 20, 120, 30);
        titleLabel.setFont(new Font("黑体", Font.BOLD, 16));
        panel.add(titleLabel);

        JLabel nameLabel = new JLabel("乘客姓名:" + bookingInfo.getLastName() + bookingInfo.getFirstName());
        nameLabel.setBounds(30, 70, 300, 25);
        panel.add(nameLabel);

        JLabel routeLabel = new JLabel("航线:" + bookingInfo.getFromCity() + " -> " + bookingInfo.getToCity());
        routeLabel.setBounds(30, 100, 300, 25);
        panel.add(routeLabel);

        JLabel dateLabel = new JLabel("出行日期:" + bookingInfo.getBookingDate());
        dateLabel.setBounds(30, 130, 300, 25);
        panel.add(dateLabel);

        JLabel countLabel = new JLabel("人数:成人" + bookingInfo.getAdultCount() + "人 儿童" + bookingInfo.getChildrenCount() + "人");
        countLabel.setBounds(30, 160, 300, 25);
        panel.add(countLabel);

        JLabel classLabel = new JLabel("舱位等级:" + bookingInfo.getSeatClass());
        classLabel.setBounds(30, 190, 300, 25);
        panel.add(classLabel);

        // 可继续添加其他机票需要展示的内容、调整样式
    }
}

内容的提问来源于stack exchange,提问作者Junior

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.23 15:21:01