Java 单击JButton时更改标签文本

Java 单击JButton时更改标签文本,java,arrays,swing,jlabel,Java,Arrays,Swing,Jlabel,我试图在单击Jbutton时更改JLabel上的文本,但我不明白为什么单击按钮时会将文本变为空。我正试图从数据库中检索数据 这是我的标签 labelDisplay = new JLabel[7]; for(int z = 0; z<7; z++){ labelDisplay[z] = new JLabel("d"); labelDisplay[z].setForeground(new Color(230,230,230)); if( z%2==0)

我试图在单击Jbutton时更改JLabel上的文本,但我不明白为什么单击按钮时会将文本变为空。我正试图从数据库中检索数据

这是我的标签

labelDisplay = new JLabel[7];

for(int z = 0; z<7; z++){
    labelDisplay[z] = new JLabel("d");
    labelDisplay[z].setForeground(new Color(230,230,230));
    if( z%2==0)
        labelDisplay[z].setBounds(130,65,160,25);
    else
        labelDisplay[z].setBounds(130,30,160,25);
}
labelDisplay[1].setText(“+id”);
字符串标签[]={guestInfo.getFirstName()+“”+guestInfo.getLastName(),
“”+roomInfo.getRoomNo()、roomInfo.getRoomType()、guestInfo.getTime(),
“11:00”,“guestInfo.getDeposit(),”30“};
标签=新字符串[7];

对于(int z=0;z“using instanceof”java说extendB/searchB无法解析为类型,因为searchB不是类..searchB是类吗?searchB是什么?@HovercraftFullOfEels我应该学会耐心的美德:)在我的傲慢中,我误导:(@user1708134:请检查Lews edit to他的答案,因为它似乎包含您的解决方案。没关系,先生,我只是想澄清我是否应该为每个getter放置一个引用变量?labels数组不是有效字符串吗?您是否检查了labels[]数组中的方法是否确实返回了一些文本?删除line labels=新字符串[7];而且很可能work@rempelos这是可行的,但是你能告诉我为什么我不应该像那样初始化字符串吗?我想如果我不放那一行,我会得到一个空指针异常,请给我解释一下你的答案:)@user1708134这样做是完全有效的。您拥有的是一个初始值设定项列表。编译器可以推断数组的大小并在运行时为其分配内存。但是,当您将标签设置为新字符串[7]时,您创建了一个新内存以覆盖以前的数组。首先在数组中放入字符串,然后使用new关键字将其清空。顺便说一句,您应该接受@Lewsterin的答案;)
    public class ButtonHandler implements ActionListener
        {
            public void actionPerformed(ActionEvent e)
            {
                if(e.getSource() == extendB)
                {
                    ExtensionForm extend = new ExtensionForm();
                    extend.setVisible(true);
                }
                else if(e.getSource()== searchB)
                {
                    //get text from the textField
                    String guest = guestIDTF.getText();
                    //parse the string to integer for retrieving of date
                    int id = Integer.parseInt(guest);
                    GuestsInfo guestInfo = new GuestsInfo(id);
                    Room roomInfo = new Room(id);
                    searchB.setText(""+id);
                    System.out.println(""+guestInfo.getFirstName());
                    labelDisplay[1].setText(""+id);
                    String labels[] = {guestInfo.getFirstName()+" "+guestInfo.getLastName(),
                            ""+roomInfo.getRoomNo(),roomInfo.getRoomType(),guestInfo.getTime(),"11:00",
                            ""+guestInfo.getDeposit(),"30"};
                    labels = new String[7];
                    for(int z = 0; z<labels.length; z++){
                        labelDisplay[z].setText(labels[z]);
                    }

                }
        }
    }
ButtonHandler bh = new ButtonHandler();
searchB = new JButton("search");
searchB.setBounds(190,30,75,25);
searchB.addActionListener(bh);
 labelDisplay[1].setText(""+id);

 String labels[] = {guestInfo.getFirstName()+" "+guestInfo.getLastName(),
      ""+roomInfo.getRoomNo(),roomInfo.getRoomType(), guestInfo.getTime(),
      "11:00", ""+guestInfo.getDeposit(),"30"};

 labels = new String[7];

 for(int z = 0; z<labels.length; z++){
      labelDisplay[z].setText(labels[z]);
 }