LeadComponent() 在 getname() 方法中给出问题
LeadComponent() gives issues in getname() method
如果我设置 leadcomponent 方法,所有 btns returns 同名但我为每个按钮设置不同的名称。如果我注释掉 leadcomponent 行,它工作正常。可能发生了什么?我为所有按钮设置名称,以便我可以应用动作侦听器并根据返回的名称打开不同的表单。
我的代码:
BorderLayout gl1 = new BorderLayout();
Container middleContainer = new Container(gl1);
parentContiner.addComponent(middleContainer);
GridLayout gl2 = new GridLayout(counter / 3 + 1, 3);
gl2.setAutoFit(true);
Container gridContainer = new Container(gl2);
parentContiner.addComponent(gridContainer);
for (HashMap<String, Object> entry : homeStorage) {
if (!"".equals(entry.get("img").toString())) {
counter++;
Button homeButton = new Button();
id = entry.get("id").toString();
imageUrl = entry.get("img").toString();
title = entry.get("name").toString();
homePlaceholder = homePlaceholder.scaled(screenWidth / 3 - 17, screenWidth / 3 - 17);
encodedHomePlaceholder = EncodedImage.createFromImage(homePlaceholder, true);
Image btnIcon = URLImage.createToStorage(encodedHomePlaceholder, "home_" + title + imageUrl, allUrl.globalHomeImageUrl + imageUrl, URLImage.RESIZE_SCALE_TO_FILL);
homeButton.setIcon(btnIcon);
***//set the name of button to id***
homeButton.setName(id);
TextArea buttonTitle = new TextArea(properCase(title));
buttonTitle.setEditable(false);
buttonTitle.setGrowByContent(true);
buttonTitle.setGrowLimit(2);
buttonTitle.setScrollVisible(false);
Container containerBtnTitle = new Container(new FlowLayout(Label.RIGHT, Label.BOTTOM));
containerBtnTitle.add(buttonTitle);
gridContainer.add(LayeredLayout.encloseIn(homeButton, containerBtnTitle));
**//if i remove this line(setLeadcomponent) it works (ie returns different name as set above, otherwise all the button returns the same name (ie last id)**
gridContainer.setLeadComponent(homeButton);
gridContainer.revalidate();
}
homeButton.addActionListener((e) -> {
if (homeButton.getName().equals("3")) {
showForm("YoutubeVideos", null);
} else if (homeButton.getName().equals("17")) {
showForm("k2Gallery", null);
} else if (homeButton.getName().equals("15")) {
showForm("EmergencyCategory", null);
} else if (homeButton.getName().equals("2")) {
showForm("PartyClicks", null);
} else if (homeButton.getName().equals("1")) {
showForm("WhereTheParty", null);
} else if (homeButton.getName().equals("5")) {
showForm("CocktailRecipies", null);
}
});
}
每个容器只能有 1 个主要成分。
您正在创建几个 homeButton,但您有 1 个 gridContainer。
如果我设置 leadcomponent 方法,所有 btns returns 同名但我为每个按钮设置不同的名称。如果我注释掉 leadcomponent 行,它工作正常。可能发生了什么?我为所有按钮设置名称,以便我可以应用动作侦听器并根据返回的名称打开不同的表单。
我的代码:
BorderLayout gl1 = new BorderLayout();
Container middleContainer = new Container(gl1);
parentContiner.addComponent(middleContainer);
GridLayout gl2 = new GridLayout(counter / 3 + 1, 3);
gl2.setAutoFit(true);
Container gridContainer = new Container(gl2);
parentContiner.addComponent(gridContainer);
for (HashMap<String, Object> entry : homeStorage) {
if (!"".equals(entry.get("img").toString())) {
counter++;
Button homeButton = new Button();
id = entry.get("id").toString();
imageUrl = entry.get("img").toString();
title = entry.get("name").toString();
homePlaceholder = homePlaceholder.scaled(screenWidth / 3 - 17, screenWidth / 3 - 17);
encodedHomePlaceholder = EncodedImage.createFromImage(homePlaceholder, true);
Image btnIcon = URLImage.createToStorage(encodedHomePlaceholder, "home_" + title + imageUrl, allUrl.globalHomeImageUrl + imageUrl, URLImage.RESIZE_SCALE_TO_FILL);
homeButton.setIcon(btnIcon);
***//set the name of button to id***
homeButton.setName(id);
TextArea buttonTitle = new TextArea(properCase(title));
buttonTitle.setEditable(false);
buttonTitle.setGrowByContent(true);
buttonTitle.setGrowLimit(2);
buttonTitle.setScrollVisible(false);
Container containerBtnTitle = new Container(new FlowLayout(Label.RIGHT, Label.BOTTOM));
containerBtnTitle.add(buttonTitle);
gridContainer.add(LayeredLayout.encloseIn(homeButton, containerBtnTitle));
**//if i remove this line(setLeadcomponent) it works (ie returns different name as set above, otherwise all the button returns the same name (ie last id)**
gridContainer.setLeadComponent(homeButton);
gridContainer.revalidate();
}
homeButton.addActionListener((e) -> {
if (homeButton.getName().equals("3")) {
showForm("YoutubeVideos", null);
} else if (homeButton.getName().equals("17")) {
showForm("k2Gallery", null);
} else if (homeButton.getName().equals("15")) {
showForm("EmergencyCategory", null);
} else if (homeButton.getName().equals("2")) {
showForm("PartyClicks", null);
} else if (homeButton.getName().equals("1")) {
showForm("WhereTheParty", null);
} else if (homeButton.getName().equals("5")) {
showForm("CocktailRecipies", null);
}
});
}
每个容器只能有 1 个主要成分。 您正在创建几个 homeButton,但您有 1 个 gridContainer。