我是一个Java新手,所以我可能完全错误地做这整件事。我必须为软件工程课做这个巨大的项目。代码长约 2,000 行,所以这是骨架代码
public class BookRental extends JFrame{
public Client currentClient = new Client(); // creating client object
//rest of declared variables.
public class Client{ //Client class containing all get/set methods for each variable
private username;
private void setUsername(String u){
username = u;
}
public String getUsername(){
return username;
}
public class LoginPanel extends JPanel{} //Panel to show and receive login info.
public class RegisterPanel extends JPanel{} //Panel to register.
public class MenuPanel extends JPanel{ //Panel showing main menu.
//At this point currentClient will contain values
public ClientInfoPanel(){
initComponents();
}
private void initComponents(){
infoPanelUserName = new JLabel();
infoPanelFullName.setText("Currently logged in as: " + currentClient.getUsername());
}
private JLabel infoPanelUserName;
}
public class ClientInfoPanel extends JPanel{} //Panel to print currentClient info to screen using JLabel objects
private void ViewClientInfoButtonActionPerformed(event){ // Using button in Menu Panel to setVisibility of clientInfoPanel to (true)
//At this point there will be a value for currentClient
clientInfoPanel = new ClientInfoPanel();
this.add(clientInfoPanel);
menuPanel.setVisible(false);
clientInfoPanel.setVisible(true);
}
public BookRental(){initComponents();} //Constructor
private void initComponents(){} // Creates all panels and sets visibility off, besides login
public static void main(String args[]){
new BookRental().setVisible(true);
}
}
我已经很确定我这样做完全错了,但是我的问题是为什么我不能在 ClientInfoPanel 中访问 currentClient?说这个 JLabel:
infoPanelUserName.setText("Currently logged in as: " + currentClient.getUsername());
ClientInfoPanel 识别 currentClient 存在并且 getUsername() 方法存在,但是它打印:
“当前登录为:”