请帮助即使 Chatgpt 也无法处理这件事。 我在同一个项目文件中有两个 Java 文件。首先,主页。用于使用文本字段询问用户输入。第二,收据。用于显示从 MainPage 输入的值。该值必须是 Display inside TextField。我无法解释我做了什么,因为正如我所说,Chatgpt 也无法解决它。我对这个范围的了解有限,所以真的希望它能完成。
当我在 MainPage 的名称中输入值时,问题就在这里。收据不会从 MainPage 中检索值并将其显示在 txtname
中主页.java
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Main.java to edit this template
*/
package project_f1083_f1081_f1067;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.border.TitledBorder;
import project_f1083_f1081_f1067.Receipt.receiptcontent;
public class MainPage extends JFrame {
JTextField txtname, txtphone, txtemail, txtpurp, txttotAttend, txtcheckin;
JTextField txtAmount;
JCheckBox cbCatering, cbPhoto, cbEntert, cbDecor;
JCheckBox cbTerms;
JComboBox cbHallType;
MainPage(){
JFrame frame = new JFrame("Hall Room Reservation");
frame.setLayout(new CardLayout(20,20));
frame.add(new MainPanel());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(880,710);
frame.setVisible(true);
}
public class hallPanel extends JPanel{
JLabel lblHallType, lblcheckin;
String Type[] = {"--Select Your Hall--","Grand Ballroom","Royal Hall",
"Majestic Ballroom","Regal Salon","Imperial Chamber"};
JButton btnlearn;
hallPanel(){
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 2;
gbc.gridy = 2;
gbc.anchor = GridBagConstraints.WEST;
lblHallType = new JLabel("Hall Type :");
cbHallType = new JComboBox(Type);
lblcheckin = new JLabel("Check In Date :");
txtcheckin = new JTextField(10);
btnlearn = new JButton("Learn About Our Halls");
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(3,3,3,3);
gbc.gridx = 0;
gbc.gridy = 0;
add(lblHallType,gbc);
gbc.gridx = 1;
gbc.gridy = 0;
add(cbHallType,gbc);
gbc.gridx = 0;
gbc.gridy = 1;
add(lblcheckin,gbc);
gbc.gridx = 1;
gbc.gridy = 1;
add(txtcheckin,gbc);
gbc.gridx = 1;
gbc.gridy = 2;
add(btnlearn,gbc);
setPreferredSize(new Dimension(300,200));
}
}
public class customerinfo extends JPanel {
JLabel lblname, lblphone, lblemail;
customerinfo(){
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 2;
gbc.gridy = 2;
gbc.anchor = GridBagConstraints.EAST;
lblname = new JLabel("Enter Your Name");
txtname = new JTextField(20);
lblphone = new JLabel("Enter Your Phone Number");
txtphone = new JTextField(20);
lblemail = new JLabel("Enter Your email");
txtemail = new JTextField(20);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(3,3,3,3);
gbc.gridx = 0;
gbc.gridy = 0;
add(lblname,gbc);
gbc.gridx = 1;
gbc.gridy = 0;
add(txtname,gbc);
gbc.gridx = 0;
gbc.gridy = 1;
add(lblphone,gbc);
gbc.gridx = 1;
gbc.gridy= 1;
add(txtphone,gbc);
gbc.gridx = 0;
gbc.gridy = 2;
add(lblemail,gbc);
gbc.gridx = 1;
gbc.gridy = 2;
add(txtemail,gbc);
setPreferredSize(new Dimension(100,200));
}
}
public class button extends JPanel implements ActionListener{
JButton btnconfirm,btnback;
button(){
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 2;
gbc.gridy = 2;
gbc.anchor = GridBagConstraints.EAST;
btnconfirm= new JButton("Make Reservation");
btnback = new JButton("Go Back To Home");
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(3,3,3,3);
gbc.gridx = 4;
gbc.gridy = 4;
add(btnconfirm,gbc);
gbc.gridx = 5;
gbc.gridy = 4;
add(btnback,gbc);
btnconfirm.addActionListener(this);
btnback.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==btnconfirm){
//customer info
String name = txtname.getText();
String phone = txtphone.getText();
String email = txtemail.getText();
String purp = txtpurp.getText();
String totalAttend = txttotAttend.getText();
int selectedOption = cbHallType.getSelectedIndex();
String checkin = txtcheckin.getText();
if(name.equals("") && phone.equals("") && email.equals("")){
JOptionPane.showMessageDialog(null, "Fill In Every Details in Customer Info !", "Warning",
JOptionPane.INFORMATION_MESSAGE);
}
else if(selectedOption == 0){
JOptionPane.showMessageDialog(null, "Select Your Hall Type !", "Warning",
JOptionPane.INFORMATION_MESSAGE);
}
else if(checkin.equals("")){
JOptionPane.showMessageDialog(null, "Fill In Your Check-in !", "Warning",
JOptionPane.INFORMATION_MESSAGE);
}
else if(purp.equals("")){
JOptionPane.showMessageDialog(null, "Please Fill In The Reason You Want To Reserve !", "Warning",
JOptionPane.INFORMATION_MESSAGE);
}
else if(totalAttend.equals("")){
JOptionPane.showMessageDialog(null, "Please Fill In Your Total Attendee !", "Warning",
JOptionPane.INFORMATION_MESSAGE);
}
else if(!cbTerms.isSelected()){
JOptionPane.showMessageDialog(null, "Please Accept Terms & Conditions !", "Warning",
JOptionPane.INFORMATION_MESSAGE);
}
else {
int result = JOptionPane.showConfirmDialog(null, "Are You Sure To Make Reservation With Those Value?",
"Confirmation", JOptionPane.OK_CANCEL_OPTION);
if (result == JOptionPane.CANCEL_OPTION) {
JOptionPane.getRootFrame().dispose();
}
if (result == JOptionPane.OK_OPTION) {
JFrame frame = (JFrame)SwingUtilities.getWindowAncestor(this);
Receipt receipt = new Receipt();
receipt.rc.leftPanel.txtname.setText(name);
receipt.rc.leftPanel.txtphone.setText(phone);
receipt.rc.leftPanel.txtemail.setText(email);
receipt.revalidate();
frame.dispose();
}
}
}
if(e.getSource()==btnback) {
int exit = JOptionPane.showConfirmDialog(null, "Are You Sure You Want To Exit?",
"Confirmation", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
if (exit == JOptionPane.YES_OPTION) {
JFrame frame = (JFrame) SwingUtilities.getWindowAncestor(this);
Login login = new Login();
frame.dispose();
} else {
JOptionPane.getRootFrame().dispose();
}
}
}
}
public class MainPanel extends JPanel {
MainPanel() {
setLayout(new GridBagLayout());
hallPanel panel1 = new hallPanel();
panel1.setBorder(new TitledBorder("Choose Your Hall "));
GridBagConstraints gbc1 = new GridBagConstraints();
gbc1.gridx = 0;
gbc1.gridy = 0;
gbc1.gridheight = 1;
gbc1.fill = GridBagConstraints.BOTH;
gbc1.weightx = 0.5;
gbc1.weighty = 0.5;
gbc1.insets = new Insets(5, 5, 5, 5);
add(panel1, gbc1);
addFee panel2 = new addFee();
panel2.setBorder(new TitledBorder("Additional Services"));
GridBagConstraints gbc2 = new GridBagConstraints();
gbc2.gridx = 0;
gbc2.gridy = 1;
gbc2.gridheight = 1;
gbc2.fill = GridBagConstraints.BOTH;
gbc2.weightx = 0.5;
gbc2.weighty = 0.5;
gbc2.insets = new Insets(5, 5, 5, 5);
add(panel2, gbc2);
Details panel3 = new Details();
panel3.setBorder(new TitledBorder("Your Details"));
GridBagConstraints gbc3 = new GridBagConstraints();
gbc3.gridx = 1;
gbc3.gridy = 0;
gbc3.gridheight = 2;
gbc3.fill = GridBagConstraints.BOTH;
gbc3.weightx = 1.0;
gbc3.weighty = 1.0;
gbc3.insets = new Insets(3,3,3,3);
add(panel3, gbc3);
customerinfo panel4 = new customerinfo();
panel4.setBorder(new TitledBorder("Customer Info"));
GridBagConstraints gbc4 = new GridBagConstraints();
gbc4.gridx = 0;
gbc4.gridy = 2;
gbc4.gridwidth =2;
gbc4.fill = GridBagConstraints.HORIZONTAL;
gbc4.insets = new Insets(5, 5, 5, 5);
add(panel4, gbc4);
button panel5 = new button();
GridBagConstraints gbc5 = new GridBagConstraints();
gbc5.gridx = 0;
gbc5.gridy = 3;
gbc5.gridwidth = 2;
gbc5.fill = GridBagConstraints.HORIZONTAL;
gbc5.insets = new Insets(5,5,5,5);
add(panel5,gbc5);
setPreferredSize(new Dimension(600, 400));
}
}
public static void main(String[] args) {
MainPage app = new MainPage();
}
}```
Receipt.java
/*
Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template / package project_f1083_f1081_f1067; import javax.swing.; import java.awt.*; import java.text.DateFormat; import java.util.Date; import javax.swing.border.TitledBorder;
public class Receipt extends JFrame {
receiptcontent rc;
Receipt(){
JFrame frame = new JFrame("Thank You For Your Reservation - Hall-O-Ya Hall -");
frame.setLayout(new CardLayout(20,20));
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
rc = new receiptcontent();
getContentPane().add(rc);
pack();
frame.add(new MainPanel());
frame.setSize(950,680);
frame.setVisible(true);
}
public class Title extends JPanel {
JLabel lblcompany,lbltitle,dateTimeLabel;
DateFormat dateFormat;
Title(){
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
lblcompany = new JLabel("Hall-O-Ya Corporation ");
lbltitle= new JLabel("THANK YOU FOR YOUR RESERVATION !");
dateFormat = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM);
Date currentDate = new Date();
String dateString = dateFormat.format(currentDate);
dateTimeLabel = new JLabel("You Have Made Your Reservation At :"+dateString);
gbc.fill = GridBagConstraints.CENTER;
gbc.anchor = GridBagConstraints.CENTER;
gbc.insets = new Insets(3,3,3,3);
gbc.gridx = 0;
gbc.gridy = 1;
add(lblcompany,gbc);
gbc.gridx = 0;
gbc.gridy = 2;
add(lbltitle,gbc);
gbc.gridx = 0;
gbc.gridy = 3;
add(dateTimeLabel,gbc);
setPreferredSize(new Dimension(300,200));
}
}
类 receiptcontent 扩展 JPanel {
public LeftPanel leftPanel;
receiptcontent(){
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
leftPanel = rc.leftPanel;
// create left panel and add to receiptcontent
//LeftPanel leftPanel = new LeftPanel();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.CENTER;
gbc.fill = GridBagConstraints.BOTH;
gbc.insets =new Insets(20,10,5,5);
gbc.weightx = 0.5;
gbc.weighty = 0;
add(leftPanel, gbc);
// create right panel and add to receiptcontent
RightPanel rightPanel = new RightPanel();
rightPanel.setBorder(new TitledBorder(""));
gbc.gridx = 1;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.WEST;
gbc.fill = GridBagConstraints.BOTH;
gbc.insets =new Insets(20,5,5,20);
gbc.weightx = 0.5;
gbc.weighty = 1.0;
add(rightPanel, gbc);
BottomPanel bottomPanel = new BottomPanel();
gbc.gridx = 0;
gbc.gridy = 1;
gbc.anchor = GridBagConstraints.CENTER;
gbc.fill = GridBagConstraints.BOTH;
gbc.insets= new Insets(10,10,10,10);
gbc.gridwidth=2;
gbc.weightx =1.0;
gbc.weighty = 0;
add(bottomPanel,gbc);
setPreferredSize(new Dimension(800,600));
}
// separate class for left panel
public class LeftPanel extends JPanel {
JLabel lblname,lblphonenum, lblemail,lblpurp,lbltotAttend,lbladdreq;
JTextField txtname,txtphone,txtemail,txtpurp,txttotAttend;
JTextArea taaddreq;
LeftPanel(){
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
lblname = new JLabel("Your Name :");
txtname = new JTextField(20);
lblphonenum = new JLabel("Your Phone Number :");
txtphone = new JTextField(20);
lblemail = new JLabel("Your Email :");
txtemail = new JTextField(20);
lblpurp = new JLabel("The Purpose Of Reserve :");
txtpurp = new JTextField(20);
lbltotAttend = new JLabel("Total That Will Attending");
txttotAttend = new JTextField(20);
lbladdreq = new JLabel("Additional Requirements");
taaddreq = new JTextArea(5,20);
gbc.gridx = 0;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.WEST;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets =new Insets(0,20,2,20);
gbc.weightx = 0.5;
gbc.weighty = 0;
add(lblname,gbc);
gbc.gridx = 1;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.EAST;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets =new Insets(0,20,2,20);
gbc.weightx = 0;
gbc.weighty = 0;
txtname.setEditable(true);
add(txtname,gbc);
gbc.gridx = 0;
gbc.gridy = 1;
gbc.anchor = GridBagConstraints.WEST;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets =new Insets(10,20,2,20);
gbc.weightx = 0.5;
gbc.weighty = 0;
add(lblphonenum,gbc);
gbc.gridx = 1;
gbc.gridy = 1;
gbc.anchor = GridBagConstraints.EAST;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets =new Insets(10,20,2,20);
gbc.weightx = 0.5;
gbc.weighty = 0;
txtphone.setEditable(false);
add(txtphone,gbc);
gbc.gridx = 0;
gbc.gridy = 2;
gbc.anchor = GridBagConstraints.WEST;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets =new Insets(10,20,2,20);
gbc.weightx = 0.5;
gbc.weighty = 0;
add(lblemail,gbc);
gbc.gridx = 1;
gbc.gridy = 2;
gbc.anchor = GridBagConstraints.EAST;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets =new Insets(10,20,2,20);
gbc.weightx = 0.5;
gbc.weighty = 0;
txtemail.setEditable(false);
add(txtemail,gbc);
gbc.gridx = 0;
gbc.gridy = 3;
gbc.anchor = GridBagConstraints.WEST;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets =new Insets(10,20,2,20);
gbc.weightx = 0.5;
gbc.weighty = 0;
add(lblpurp,gbc);
gbc.gridx = 1;
gbc.gridy = 3;
gbc.anchor = GridBagConstraints.EAST;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets =new Insets(10,20,2,20);
gbc.weightx = 0.5;
gbc.weighty = 0;
txtpurp.setEditable(false);
add(txtpurp,gbc);
gbc.gridx = 0;
gbc.gridy = 4;
gbc.anchor = GridBagConstraints.WEST;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets =new Insets(10,20,2,20);
gbc.weightx = 0.5;
gbc.weighty = 0;
add(lbltotAttend,gbc);
gbc.gridx = 1;
gbc.gridy = 4;
gbc.anchor = GridBagConstraints.EAST;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets =new Insets(10,20,2,20);
gbc.weightx = 0.5;
gbc.weighty = 0;
txttotAttend.setEditable(false);
add(txttotAttend,gbc);
gbc.gridx = 0;
gbc.gridy = 5;
gbc.anchor = GridBagConstraints.WEST;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets =new Insets(10,20,2,20);
gbc.weightx = 0.5;
gbc.weighty = 0;
add(lbladdreq,gbc);
gbc.gridx = 1;
gbc.gridy = 5;
gbc.anchor = GridBagConstraints.EAST;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets =new Insets(10,20,2,20);
gbc.weightx = 0.5;
gbc.weighty = 0;
taaddreq.setEditable(false);
add(taaddreq,gbc);
}
}
公共类 MainPanel 扩展 JPanel {
MainPanel() {
setLayout(new GridBagLayout());
Title panel1 = new Title();
GridBagConstraints gbc1 = new GridBagConstraints();
gbc1.gridx = 0;
gbc1.gridy = 0;
gbc1.fill = GridBagConstraints.BOTH;
gbc1.insets = new Insets(5, 5, 5, 5);
add(panel1, gbc1);
receiptcontent panel2 = new receiptcontent();
panel2.setBorder(new TitledBorder(""));
GridBagConstraints gbc2 = new GridBagConstraints();
gbc2.gridx = 0;
gbc2.gridy = 1;
gbc2.fill = GridBagConstraints.BOTH;
gbc2.weightx = 2.5;
gbc2.weighty = 2.5;
gbc2.insets = new Insets(5, 5, 5, 5);
add(panel2, gbc2);
info panel4 = new info();
GridBagConstraints gbc4 = new GridBagConstraints();
gbc4.fill = GridBagConstraints.LINE_START;
gbc4.anchor = GridBagConstraints.WEST;
gbc4.gridx = 0;
gbc4.gridy = 2;
gbc4.gridwidth =1;
gbc4.weightx = 0.5;
gbc4.weighty = 0;
add(panel4, gbc4);
setPreferredSize(new Dimension(600, 400));
}
}
public static void main(String[] args) {
Receipt app = new Receipt();
}
}```