有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java试图找出如何将用户从这个GUI代码中选择的内容打印到另一个GUI上

试图弄清楚如何将用户从这个GUI中选择的内容打印到另一个GUI上,并使其看起来像机票或收据。我很确定我可以设计机票GUI。我在编写代码时遇到了问题,这些代码会接受用户从下面的GUI代码中输入和选择的任何内容,并将其打印到另一个GUI


import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

public class GUI implements ActionListener {

    private static JLabel firstNamelabel, lastNamelabel, fromLabel, toLabel, adultLabel, childrenLabel, bookingDate, LDate, classLabel, passengerDetails;
    private static JTextField firstNameuserText, lastNameuserText, bookingDateText;
    private static JComboBox fromCombo, toCombo, adultCombo, childrenCombo, classCombo;
    private static JButton button;

    public static void main(String[] args) {

        JPanel panel = new JPanel();
        JFrame frame = new JFrame();
        frame.setSize(400, 400);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        frame.add(panel);

        panel.setLayout(null);


        String[] sItem1 = { "New York" };
        String[] sItem2 = { "Florida ", "California ", "Texas ", "Chicago ", "Nevada ", "Ohio ", "Washington ","Georgia " };
        String[] sItem3 = { "Economic", "Business" };
        String[] item4 = { "1", "2", "3", "4", "5", "6" };
        String[] item5 = { "0", "1", "2", "3", "4" };

        passengerDetails = new JLabel("Passenger Details");
        passengerDetails.setBounds(10, 1, 150, 25);
        passengerDetails.setForeground(Color.blue);
        panel.add(passengerDetails);

        firstNamelabel = new JLabel("First Name");
        firstNamelabel.setBounds(10, 40, 80, 25); // x,y,width,height
        panel.add(firstNamelabel);

        firstNameuserText = new JTextField(20);
        firstNameuserText.setBounds(100, 40, 165, 25);
        panel.add(firstNameuserText);

        lastNamelabel = new JLabel("Last Name");
        lastNamelabel.setBounds(10, 65, 80, 25);
        panel.add(lastNamelabel);

        lastNameuserText = new JTextField(20);
        lastNameuserText.setBounds(100, 65, 165, 25);
        panel.add(lastNameuserText);

        fromLabel = new JLabel("From");
        fromLabel.setBounds(10, 110, 80, 25);
        panel.add(fromLabel);

        fromCombo = new JComboBox(sItem1);
        fromCombo.setBounds(50, 110, 125, 25);
        panel.add(fromCombo);

        toLabel = new JLabel("To");
        toLabel.setBounds(150, 110, 80, 25);
        panel.add(toLabel);

        toCombo = new JComboBox(sItem2);
        toCombo.setBounds(175, 110, 125, 25);
        panel.add(toCombo);

        adultLabel = new JLabel("Adult(12+)");
        adultLabel.setBounds(10, 150, 80, 25);
        panel.add(adultLabel);

        adultCombo = new JComboBox(item4);
        adultCombo.setBounds(10, 175, 80, 25);
        panel.add(adultCombo);

        childrenLabel = new JLabel("Children(2-11)");
        childrenLabel.setBounds(150, 150, 100, 25);
        panel.add(childrenLabel);

        childrenCombo = new JComboBox(item5);
        childrenCombo.setBounds(150, 175, 80, 25);
        panel.add(childrenCombo);

        bookingDate = new JLabel("Booking Date");
        bookingDate.setBounds(10, 225, 100, 25);
        panel.add(bookingDate);

        bookingDateText = new JTextField(20);
        bookingDateText.setBounds(100, 225, 140, 25);
        panel.add(bookingDateText);

        LDate = new JLabel("(MM/DD/YYYY)");
        LDate.setBounds(250, 225, 300, 25);
        LDate.setForeground(Color.red);
        panel.add(LDate);

        classLabel = new JLabel("Class");
        classLabel.setBounds(10, 275, 80, 25);
        panel.add(classLabel);

        classCombo = new JComboBox(sItem3);
        classCombo.setBounds(60, 275, 125, 25);
        panel.add(classCombo);

        button = new JButton("Make Reservation");
        button.setBounds(120, 325, 175, 25);
        button.addActionListener(new GUI());
        panel.add(button);

        panel.setBackground(Color.lightGray);

        frame.setVisible(true);

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        String FirstName = firstNameuserText.getText();
        String LastName = lastNameuserText.getText();

    }

}

共 (1) 个答案

  1. # 1 楼答案

    可能您应该首先创建一个类来保存从用户输入(TicketRequest.class)获取的信息,并将数据处理到新的GUI中

    代码思想:

     button.addActionListener(e -> {
                TicketRequest tr = new TicketRequest();
                tr.setName(firstNameuserText.getText());
                // ...
                GUInew guiNew = new GUInew(tr);
                guiNew.setVisible(true);
                
            
            });
    

    GUInew类可以扩展JFrame