クラスプロジェクトのコーディングを書いています。購入できるサービスのメニューを持つ一連の GUI を表示するためにすべてが連携して動作する多くの個別のクラスがあります。2 番目の GUI は顧客の情報 (名前、クレジット カードなど) を取得し、3 番目の GUI はレシートを表示します。各 GUI には独自のクラスがあります。レシートを表示するには、3 番目の GUI (GUIBill) が 2 番目の 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);
}
}
コメントアウトされていない問題のあるメソッドの 1 つを使用して 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