ฉันตั้งค่า textArea setGrowByContent จริงและ setGrowLimit เป็น 2 แต่จะมีเพียงแถวเดียวเสมอ ดังนั้นฉันจึงบิดโค้ดและเพิ่มความสูงของ textArea สองเท่าหากขนาดของ textArea มากกว่าขนาดของปุ่ม ด้านล่างเป็นรหัส ปัญหาของฉันอยู่ท้ายคำถาม:
Button homeButtonn = new Button(btnIcon){
@Override
protected Dimension calcPreferredSize() {
System.out.println("Button size: " + super.calcPreferredSize());
return super.calcPreferredSize();
}
@Override
public void setUIID(String id) {
super.setUIID("homeButtonn");
}
};
TextArea buttonTitle = new TextArea(title){
@Override
protected Dimension calcPreferredSize() {
System.out.println("textArea title: " + getText());
System.out.println("Textarea size: " +super.calcPreferredSize());
System.out.println("");
return super.calcPreferredSize();
}
};
buttonTitle.setUIID("smallLabel");
buttonTitle.getAllStyles().setAlignment(Label.LEFT);
zeroPaddingMargin(buttonTitle);
if (buttonTitle.getPreferredW() -10 > homeButtonn.getPreferredW()) {
buttonTitle.setPreferredH(buttonTitle.getPreferredH() * 2);
}
buttonTitle.setPreferredW(homeButtonn.getPreferredW() - 10);
buttonTitle.getAllStyles().setMargin(3, 3, 3, 3);
buttonTitle.setEditable(false);
buttonTitle.setGrowByContent(true);
buttonTitle.setGrowLimit(2);
buttonTitle.setScrollVisible(false);
gridContainer.add(LayeredLayout.encloseIn(homeButtonn, FlowLayout.encloseRightBottom(buttonTitle)));
เอาท์พุท:
Button size: width = 146 height = 140
textArea title: DJ and Sound
Textarea size: width = 194 height = 25
ที่นี่ขนาด textArea มากกว่าขนาดปุ่ม แต่ถ้าฉันตั้งค่าขนาดของ textArea ด้วยขนาดที่แน่นอนของปุ่ม มันจะพอดี ดังนั้นขนาด textArea จะใหญ่กว่าขนาดปุ่มได้อย่างไรและข้อความทั้งหมดของ textArea จะพอดีกับปุ่มภายในได้อย่างไร ปัญหาที่ฉันได้รับคือเนื่องจากขนาด textArea มากกว่าขนาดปุ่ม ความสูงของ textArea จึงถูกคูณด้วยความสูงสองเท่าของตัวมันเอง แต่ textArea พอดีในบรรทัดเดียวและปล่อยให้มีบรรทัด/แถวเพิ่มเติมอยู่ด้านล่าง
ป.ล. ดูภาพหน้าจอ ขอบคุณ
ไม่มีการคำนวณขนาดที่ต้องการหรือขนาดที่ต้องการ:
หากลบ calcPreferredSize จะใช้ความกว้างหน้าจอทั้งหมดแม้ว่าจะอยู่ในตารางกริดที่มี 3 คอลัมน์ก็ตาม
อัปเดต: รหัสล่าสุดที่ไม่มี calcPreferredSize & textArea ซ้อนอยู่ในคอนเทนเนอร์ด้วย
GridLayout gl2 = new GridLayout(counter / 3 + 1, 3);
gl2.setAutoFit(true);
Container gridContainer = new Container(gl2);
gridContainer.setScrollableY(true);
f.addComponent(gridContainer);
imageUrl = entry.get("img").toString();
title = entry.get("name").toString();
homePlaceholder = homePlaceholder.scaled(screenWidth / 3 - 20, screenWidth / 3 - 26);
encodedHomePlaceholder = EncodedImage.createFromImage(homePlaceholder, true);
Image btnIcon = URLImage.createToStorage(encodedHomePlaceholder, "home_" + title + imageUrl, allUrl.globalHomeImageUrl + imageUrl, URLImage.RESIZE_SCALE_TO_FILL);
homeButton.setIcon(btnIcon);
TextArea buttonTitle = new TextArea(title);
buttonTitle.getAllStyles().setAlignment(Label.LEFT);
buttonTitle.getAllStyles().setMargin(3, 3, 3, 3);
buttonTitle.setEditable(false);
buttonTitle.setGrowByContent(true);
buttonTitle.setGrowLimit(2);
buttonTitle.setScrollVisible(false);
Container btnTitleContainer = new Container();
btnTitleContainer.addComponent(buttonTitle);
gridContainer.add(LayeredLayout.encloseIn(homeButton, FlowLayout.encloseRightBottom(btnTitleContainer)));
f.revalidate();