Как перенести все данные из одного JFrame в другой JFrame через кнопку? - PullRequest
0 голосов
/ 23 января 2020

Извините, я новичок в Java, так что извините за недостаток знаний. Я добавил в текстовые поля, кнопки и поля со списком в MainSystem JFrame. Я хочу ввести данные в основную систему JFrame, получить их и перенести через кнопку в квитанцию ​​JFrame, как квитанцию. Но я не уверен, могут ли данные быть переданы для выпадающего списка. Есть ли хороший вариант для макетов? Макет моей программы очень распространен. Я также не знаю, как распечатать вывод комбинированного списка.

Это мой код ниже.

Основная система (первый JFrame)

  Receiptbtn.addActionListener(new ActionListener(){
       public void actionPerformed(ActionEvent e){
            String name = namef.getText();
            String passport = passportf.getText();
            String contact = contactf.getText();
            String email = emailf.getText();
            String tourist = touristnumf.getText();
            String season = (String)SeasonBx.getSelectedItem();
            String region = (String)RegionBx.getSelectedItem();
            String meal = (String)MealBx.getSelectedItem();
            new Receipt();
       } 
   });  

Квитанция (Другое JFrame):

public class Receipt extends JFrame
{
    //label names
    private JLabel namelbl;
    private JLabel passportlbl;
    private JLabel contactlbl;
    private JLabel emaillbl;
    private JLabel touristnumlbl;
    private JLabel seasonlbl;
    private JLabel regionlbl;

    //user input
    private JLabel rnamelbl;
    private JLabel rpassportlbl;
    private JLabel rcontactlbl;
    private JLabel remaillbl;
    private JLabel rtouristnumlbl;
    private JLabel rseasonlbl;
    private JLabel rregionlbl;
    private JLabel rmeallbl;

    //text fields name
    private JTextField namef;
    private JTextField passportf;
    private JTextField contactf;
    private JTextField emailf;
    private JTextField touristnumf;

    public Receipt()
    {
        //creating the labels
        namelbl = new JLabel("Full Name: ");
        passportlbl = new JLabel("Passport: ");
        contactlbl = new JLabel("Contact No: ");
        emaillbl = new JLabel("Email: ");
        touristnumlbl = new JLabel("Tourist No: ");
        seasonlbl = new JLabel("Season: "); 
        regionlbl = new JLabel("Region: ");

        //get data from text field after clicking button
        String name = namef.getText();
        String passport = passportf.getText();
        String contact = contactf.getText();
        String email = emailf.getText();
        String tourist = touristnumf.getText();
        Eventcollector ec = new Eventcollector(this, name, passport, contact, 
                                                email, tourist, season, region, meal);

        rnamelbl = new JLabel(name);
        rpassportlbl = new JLabel(passport);
        rcontactlbl = new JLabel(contact);
        remaillbl = new JLabel(email);
        rtouristnumlbl = new JLabel(tourist);

        setSize(700,600);
        //JFrame visibility and function to close on pressing the x 
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

Сборщик событий:

public class Eventcollector extends EventObject
{
    private String name;
    private String passport;
    private String contact;
    private String email;
    private String touristnum;

    private String season;
    private String region;
    private String meal;

    //Accept info from source of event
    public Eventcollector(Object source) {
        super(source);
    }

    public Eventcollector(Object source, String name, String passport, String contact, 
            String email, String touristnum, String season, String region, String meal)
    {
        super(source);

        this.name = name;
        this.passport = passport;
        this.contact = contact;
        this.email = email;
        this.touristnum = touristnum;
        this.season = season;
        this.region = region;
        this.meal = meal;
    }

    public String getName(){
        return name;
    }

    public void getName(String name){
        this.name = name;
    }

    public String getpassport(){
        return passport;
    }

    public void getpassport(String passport){
        this.passport = passport;
    }

    public String getcontact(){
        return contact;
    }

    public void getcontact(String contact){
        this.contact = contact;
    }

    public String getemail(){
        return email;
    }

    public void getemail(String email){
        this.email = email;
    }

    public String gettouristnum(){
        return touristnum;
    }

    public void gettouristnum(String touristnum){
        this.touristnum = touristnum;
    }

    public String getseason(){
        return season;
    }

    public void getseason(String season){
        this.season = season;
    }

    public String getregion(){
        return region;
    }

    public void getregion(String region){
        this.region = region;
    }

    public String getmeal(){
        return meal;
    }

    public void getmeal(String meal){
        this.meal = meal;
    }
}

1 Ответ

1 голос
/ 23 января 2020

В конце вашего слушателя действия все создаваемые вами значения забываются / теряются / игнорируются. Вы должны ссылаться на них в слушателе действия.

 Receiptbtn.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e){
        String name = namef.getText();
        String passport = passportf.getText();
        String contact = contactf.getText();
        String email = emailf.getText();
        String tourist = touristnumf.getText();
        String season = (String)SeasonBx.getSelectedItem();
        String region = (String)RegionBx.getSelectedItem();
        String meal = (String)MealBx.getSelectedItem();
        Eventcollector ec = new Eventcollector(this, name, passport, contact, email, 
                                               tourist, season, region, meal);
   } 
});
...