我正在为一个班级项目编写代码。我有许多单独的类,它们一起工作以显示一系列 GUI,这些 GUI 具有可以购买的服务菜单。第二个 GUI 获取客户的信息(姓名、信用卡等),第三个 GUI 显示收据。每个 GUI 都有自己的类。为了显示收据,我的第三个 GUI (GUIBill) 必须从我的第二个 GUI (GUICheckout) 调用方法。所以我的 GUICheckout 底部有几个 get 方法。出于某种原因,getName 方法可以完美运行。但是,其余方法(getStreet、getCity 等)在我的 GUIBill 中不起作用。我不断收到无法找到这些方法的错误消息,我绞尽脑汁试图找出原因。现在,我只会发布 GUICheckout 和 GUIBill 类,
GUICheckout 的前 75% 可能无关紧要,但我发布了整个代码,以防问题与我创建 JLabels 时有关。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class GUICheckout extends JFrame{
private static GUIShopping theGUI = new GUIShopping();
private CompleteSale sale = new CompleteSale();
/*Widgets for second window, where customer enters their information*/
private JLabel customerName = new JLabel("Name", JLabel.CENTER);
private JLabel customerStreet = new JLabel("Street Address", JLabel.CENTER);
private JLabel customerCity = new JLabel("City", JLabel.CENTER);
private JLabel customerState = new JLabel("State", JLabel.CENTER);
private JLabel customerZip = new JLabel("Zip", JLabel.CENTER);
private JLabel creditCard = new JLabel("Credit Card Type", JLabel.CENTER);
private JLabel creditCardNumber = new JLabel("Credit Card Number", JLabel.CENTER);
private JLabel creditExpiration = new JLabel("Expiration Date", JLabel.CENTER);
private JTextField customerNameField = new JTextField("");
private JTextField customerStreetField = new JTextField("");
private JTextField customerCityField = new JTextField("");
private JTextField customerStateField = new JTextField("");
private JTextField customerZipField = new JTextField("");
private JTextField creditCardNumberField = new JTextField("");
private JButton proceed = new JButton("Pay and View Bill");
private JButton goBack = new JButton("Return to Menu");
private String[] creditCardTypes = { "Visa", "MasterCard", "American Express" };
private String[] monthList = { "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12" };
private String[] yearList = { "13", "14", "15", "16", "17", "18", "19", "20" };
private JComboBox creditCardList = new JComboBox(creditCardTypes);
private JComboBox expirationMonth = new JComboBox(monthList);
private JComboBox expirationYear = new JComboBox(yearList);
public GUICheckout(){
JPanel dataPanel = new JPanel(new GridLayout(5, 4, 5, 20));
dataPanel.add(customerName);
dataPanel.add(customerNameField);
dataPanel.add(customerState);
dataPanel.add(customerStateField);
dataPanel.add(customerStreet);
dataPanel.add(customerStreetField);
dataPanel.add(customerZip);
dataPanel.add(customerZipField);
dataPanel.add(customerCity);
dataPanel.add(customerCityField);
dataPanel.add(creditCard);
dataPanel.add(creditCardList);
dataPanel.add(creditCardNumber);
dataPanel.add(creditCardNumberField);
dataPanel.add(new JLabel(""));
dataPanel.add(new JLabel(""));
dataPanel.add(creditExpiration);
dataPanel.add(expirationMonth);
dataPanel.add(expirationYear);
JPanel buttonPanel = new JPanel(new GridLayout(1,3,12,6));
buttonPanel.add(goBack);
buttonPanel.add(proceed);
JPanel topPanel = new JPanel();
JPanel leftPanel = new JPanel();
JPanel rightPanel = new JPanel();
Container container = getContentPane();
container.add(dataPanel, BorderLayout.CENTER);
container.add(topPanel, BorderLayout.NORTH);
container.add(buttonPanel, BorderLayout.SOUTH);
container.add(rightPanel, BorderLayout.EAST);
container.add(leftPanel, BorderLayout.WEST);
goBack.addActionListener(new GoBackListener());
proceed.addActionListener(new ProceedListener());
}
private class GoBackListener implements ActionListener{
public void actionPerformed(ActionEvent e){
JFrame theFirstGUI = GUIShopping.getCheckoutMainFrame();
theFirstGUI.dispose();
theGUI.setTitle("Iowa Computer Service and Repair");
theGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
theGUI.setSize(600,300);
theGUI.setVisible(true);
}
}
private class ProceedListener implements ActionListener{
public void actionPerformed(ActionEvent e){
sale.setName(customerNameField.getText());
sale.setAddress(customerStreetField.getText(), customerCityField.getText(), customerStateField.getText(), customerZipField.getText());
String cardType = (String)creditCardList.getSelectedItem();
String expMonth = (String)expirationMonth.getSelectedItem();
String expYear = (String)expirationYear.getSelectedItem();
sale.setCardInfo(cardType, creditCardNumberField.getText(), expMonth, expYear);
JFrame theFirstGUI = GUIShopping.getCheckoutMainFrame();
theFirstGUI.dispose();
GUIBill theBillGUI = new GUIBill();
theBillGUI.setTitle("Iowa Computer Service and Repair");
theBillGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
theBillGUI.setSize(600,300);
theBillGUI.setVisible(true);
}
}
public static JFrame getShoppingMainFrame(){
return theGUI;
}
public String getName(){ //Here are all of the get Methods. the getName works fine but the rest do not
String name = customerNameField.getText();
return name;
}
public String getStreet(){
String street = customerStreetField.getText();
return street;
}
public String getCity(){
String city = customerCityField.getText();
return city;
}
public String getCustState(){
String state = customerStateField.getText();
return state;
}
public String getZip(){
String zip = (String)customerZipField.getText();
return zip;
}
public String getCardType(){
String type = (String)creditCardList.getSelectedItem();
return type;
}
public String getNumber(){
String number = (String)creditCardNumberField.getText();
return number;
}
public String getExpMonth(){
String expMonth = (String)expirationMonth.getSelectedItem();
return expMonth;
}
public String getExpYear(){
String expYear = (String)expirationYear.getSelectedItem();
return expYear;
}
}
这是应该从 GUICheckout 调用方法的 GUIBill 类。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class GUIBill extends JFrame{
//private static GUICheckout theGUI = new GUICheckout();
private static JFrame theGUI = GUIShopping.getCheckoutMainFrame();
private JLabel thankYou = new JLabel("Thank you for choosing Iowa Computer Service and Repair!", JLabel.CENTER);
private JLabel name = new JLabel(theGUI.getName()); //this is the getName method that works fine
//private JLabel street = new JLabel(theGUI.getStreet()); /*all JLabels which are commented out are the ones that have methods
//private JLabel city = new JLabel(theGUI.getCity()); which mysteriously cannot be found.*/
//private JLabel state = new JLabel(theGUI.getCustState());
//private JLabel zip = new JLabel(theGUI.getZip());
private JLabel itemsBoughtLabel = new JLabel("Services Purchased", JLabel.CENTER);
private JLabel itemPricesLabel = new JLabel("Price", JLabel.CENTER);
private JLabel tax = new JLabel();
private JLabel totalPrice = new JLabel();
public GUIBill(){
JPanel thankYouPanel = new JPanel();
thankYouPanel.add(thankYou);
JPanel namePanel = new JPanel();
namePanel.add(name);
/*JPanel addressPanel = new JPanel(new GridLayout(4,1,6,12));
addressPanel.add(streetLabel);
addressPanel.add(cityLabel);
addressPanel.add(stateLabel);
addressPanel.add(zipLabel);*/
JPanel dataPanel = new JPanel(new GridLayout(4,1,6,12));
dataPanel.add(itemsBoughtLabel);
dataPanel.add(itemPricesLabel);
/*JPanel cardPanel = new JPanel(new GridLayout(4,1,6,12));
cardPanel.add(typeLabel);
cardPanel.add(numberLabel);
cardPanel.add(expMonthLabel);
cardPanel.add(expYearLabel);*/
Container container = getContentPane();
container.add(thankYouPanel, BorderLayout.NORTH);
container.add(namePanel, BorderLayout.WEST);
//container.add(addressPanel, BorderLayout.EAST);
container.add(dataPanel, BorderLayout.CENTER);
//container.add(cardPanel, BorderLayout.SOUTH);
}
}
如果我尝试使用未注释掉的错误方法之一来编译 GUIBill,则会出现以下错误:
GUIBill.java:12: error: cannot find symbol
private JLabel street = new JLabel(theGUI.getStreet()); ^
symbol: method getStreet()
location: variable theGUI of type JFrame
1 error