我是一个新手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()方法存在,但是它打印出来:
“目前登录为:”
答案 0 :(得分:0)
您展示的代码看起来很好,所以问题出在其他地方,或者此代码并不代表您所拥有的代码。此外,您正在成功访问currentClient,除非您获得NullPointerException或在某处捕获它,然后.getUsername()调用IS正在解析。所以问题实际上是.getUsername(),当你调用.getUsername()时,某种程度上用户名是未初始化的。
作为测试,您可以在调用.getUsername()之前调用currentClient上的.setUsername()吗?这应该有助于我们缩小问题范围,将其隔离为类访问或正在初始化的变量。
另外,您知道如何使用断点进行调试吗?你提到你是新人,所以你可能不知道这是可能的。如果您正在使用Eclipse(或其他好的IDE),您可以设置断点并在DEBUG构建中运行程序,然后程序将在它到达您设置断点的行时冻结,并且您可以在程序移动时观察逐行,并查看程序更新的变量。谷歌java调试教程。 :)
答案 1 :(得分:0)
我同意(不确定我是否应该添加,因为其他人已经回答了,礼仪是什么?)。我运行得很好,它向我展示了一个面板,用户在BookRental构造函数中设置为Me登录:
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
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 String username;
private void setUsername(String u){
username = u;
}
public String getUsername(){
return username;
}
}
public class ClientInfoPanel extends JPanel{ //Panel showing main menu.
//At this point currentClient will contain values
public ClientInfoPanel(){
initComponents();
}
private void initComponents(){
infoPanelUserName = new JLabel();
infoPanelUserName.setText("Currently logged in as: " + currentClient.getUsername());
this.add(infoPanelUserName);
}
private JLabel infoPanelUserName;
}
public ClientInfoPanel clientInfoPanel;
//Constructor
public BookRental(){
currentClient.setUsername("Me");
initComponents();
}
private void initComponents(){
clientInfoPanel = new ClientInfoPanel();
this.add(clientInfoPanel);
clientInfoPanel.setVisible(true);
}
public static void main(String args[]){
new BookRental().setVisible(true);
}
}