抱歉,我是 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;
}
}
最佳答案
在操作监听器结束时,您创建的所有值都将被遗忘/丢失/忽略。您需要在操作监听器中引用它们。
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);
}
});
关于java - 如何通过按钮将一个JFrame的所有数据传输到另一个JFrame?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59874101/