[英]Problems with array changing elements
我正在嘗試使用Jbutton將數組中的元素更改為單詞“ empty”,並且如果數組中所選位置的內容為空,則還會通過Jtextfield添加名稱。 由於某種原因,我無法使其正常工作。 這是代碼,不知道我是否缺少某些東西,或者我完全錯了
move = new JButton("Add Tennant");
window.add(move);
moveIn.addActionListener(this);
Tennant = new JTextField(FIELD_WIDTH);
nTennant.setText("Enter new name") ;
window.add(Tennant);
Tennant.addActionListener(this);
evict = new JButton("Evict");
window.add(evict);
moveIn.addActionListener(this);
不同的方法:
if(e.getSource() == move)
{
if (occupant[selectedApartment].equals("empty"))
{
occupant[selectedApartment] = Tennant.getText();
}
}
if(e.getSource() == evict)
{
if(!occupant[selectedApartment].equals("Empty"))
{
occupant[selectedApartment] = "Empty";
}
}
跳到我身上的第一件事是您使用occupant[selectedApartment] = "Empty";
將公寓設置為空,但使用if (occupant[selectedApartment].equals("empty"))
來測試公寓是否為空
"Empty" != "empty"
你可以改變
if (occupant[selectedApartment].equals("empty"))
至
if (occupant[selectedApartment].equals("Empty"))
或使用
if (occupant[selectedApartment].equalsIgnoreCase("empty"))
或改變
occupant[selectedApartment] = "Empty";
至
occupant[selectedApartment] = "empty";
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.