简体   繁体   中英

How to transfer all the data from one JFrame to another JFrame through a button?

Sorry I am new to Java so pardon my lack of knowledge. I have added in text fields, button and combo boxs in MainSystem JFrame. I want to enter data in Main System JFrame, get it's data and transfer through the button to the Receipt JFrame like a receipt. But I am unsure whether the data can be transferred for the combobox. Is there also any good option for layouts? My program layout is very spread out. I also have no idea how to print out the combobox output.

This is my code below.

Main System (First 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();
       } 
   });  

Receipt(The other 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);
    }
}

Event Collector:

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;
    }
}

At the end of your action listener all of the values you are creating are forgotten/lost/ignore. You need to reference them in the action listener.

 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);
   } 
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM