找不到方法但找到另一种方法

时间:2013-07-09 19:11:29

标签: java user-interface methods

我正在为一个课程项目编写代码。我有许多单独的类,它们共同工作以显示一系列GUI,这些GUI具有可以购买的服务菜单。第二个GUI获取客户的信息(姓名,信用卡等),第三个GUI显示收据。 每个GUI都有自己的类。为了显示收据,我的第三个GUI(GUIBill)必须从我的第二个GUI(GUICheckout)调用方法。所以我在GUICheckout的底部有几个get方法。由于某种原因,getName方法完美地运行。但是,其余的方法(getStreet,getCity等)在我的GUIBill中不起作用。我不断发现这些方法无法找到的错误,我正试图找出原因。现在,我只发布GUICheckout和GUIBill类,但如果由于某种原因需要其他类,请告诉我。

GUICheckout的前75%可能无关紧要,但我发布了整个代码,以防问题与我创建JLabel时有关。

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

这是GUIBill类,它应该从GUICheckout调用方法。

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

1 个答案:

答案 0 :(得分:2)

您声明了GUI对象如下:

  

private static JFrame theGUI = GUIShopping.getCheckoutMainFrame();

所以它是JFrame类型,它不知道你在 GUICheckout -class中声明的方法。而只是像下面那样声明GUI,你应该能够调用方法:

  
    

private static GUICheckout theGUI = GUIShopping.getCheckoutMainFrame();

  

我希望这可以帮到你!