【发布时间】:2021-06-28 13:57:53
【问题描述】:
我正在制作一个面板来显示底部有“购买”和“收藏”按钮的房产广告。我没有多次制作相同的面板,而是创建了一个为我生成广告面板的方法,我只需使用不同的坐标多次调用该方法。
public JPanel createAdvertisement(int price, String owner, String location, int bedrooms, int bathrooms,
int stories, int area) {
JPanel adPanel = new JPanel();
adPanel.setBackground(Color.WHITE);
adPanel.setBorder(new LineBorder(new Color(0, 0, 0), 1, true));
adPanel.setBounds(50, 138, 500, 230);
this.add(adPanel);
adPanel.setLayout(null);
JLabel housePicture = new JLabel("");
housePicture.setIcon(new ImageIcon(BrowseAds.class.getResource("/images/stock house.jpg")));
housePicture.setBounds(10, 0, 194, 200);
adPanel.add(housePicture);
JLabel priceLabel = new JLabel(String.valueOf(price));
priceLabel.setFont(new Font("SansSerif", Font.BOLD, 18));
priceLabel.setIcon(new ImageIcon(BrowseAds.class.getResource("/images/price.png")));
priceLabel.setBounds(214, 11, 276, 32);
adPanel.add(priceLabel);
JLabel ownerLabel = new JLabel(owner);
ownerLabel.setIcon(new ImageIcon(BrowseAds.class.getResource("/images/owner.png")));
ownerLabel.setFont(new Font("SansSerif", Font.PLAIN, 16));
ownerLabel.setBounds(218, 50, 270, 21);
adPanel.add(ownerLabel);
JLabel locationLabel = new JLabel("<html>" + location + "</html>");
locationLabel.setIcon(new ImageIcon(BrowseAds.class.getResource("/images/location.png")));
locationLabel.setFont(new Font("SansSerif", Font.PLAIN, 12));
locationLabel.setBounds(220, 75, 270, 30);
adPanel.add(locationLabel);
JLabel bedroomLabel = new JLabel(String.valueOf(bedrooms) + " Rooms");
bedroomLabel.setIcon(new ImageIcon(BrowseAds.class.getResource("/images/bedroom.png")));
bedroomLabel.setFont(new Font("SansSerif", Font.PLAIN, 12));
bedroomLabel.setBounds(224, 118, 100, 21);
adPanel.add(bedroomLabel);
JLabel bathroomLabel = new JLabel(String.valueOf(bathrooms) + " Baths");
bathroomLabel.setIcon(new ImageIcon(BrowseAds.class.getResource("/images/bathroom.png")));
bathroomLabel.setFont(new Font("SansSerif", Font.PLAIN, 12));
bathroomLabel.setBounds(350, 118, 100, 21);
adPanel.add(bathroomLabel);
JLabel storiesLabel = new JLabel(String.valueOf(stories) + " Stories");
storiesLabel.setIcon(new ImageIcon(BrowseAds.class.getResource("/images/floors.png")));
storiesLabel.setFont(new Font("SansSerif", Font.PLAIN, 12));
storiesLabel.setBounds(224, 150, 100, 21);
adPanel.add(storiesLabel);
JLabel areaLabel = new JLabel(String.valueOf(area) + " Marla");
areaLabel.setIcon(new ImageIcon(BrowseAds.class.getResource("/images/area.png")));
areaLabel.setFont(new Font("SansSerif", Font.PLAIN, 12));
areaLabel.setBounds(350, 150, 100, 21);
adPanel.add(areaLabel);
buyButton = new JButton("Buy");
buyButton.addActionListener(this);
buyButton.setFont(new Font("SansSerif", Font.PLAIN, 10));
buyButton.setBounds(214, 196, 120, 23);
adPanel.add(buyButton);
JButton btnAddToFavorite = new JButton("Add to Favorite");
btnAddToFavorite.setFont(new Font("SansSerif", Font.PLAIN, 10));
btnAddToFavorite.setBounds(360, 196, 120, 23);
adPanel.add(btnAddToFavorite);
return adPanel;
}
我想根据其位置在同一个按钮上调用不同的方法。例如。如果用户点击屏幕上的第三个“购买”按钮,我想将该广告的数据发送到后端。你能建议我一种使用同一个按钮执行不同操作的方法吗?
制作广告的代码:
public void viewAds() {
for (int i = 0; i < 4; i++) {
int index = ((pageNumber - 1) * 4) + i;
if (index < totalCount) {
Ad curr = array.get(i);
JPanel panel = createAdvertisement(curr.price, curr.owner, curr.location, curr.bedrooms, curr.bathrooms,
curr.stories, curr.area);
if (index % 4 == 0) {
panel.setBounds(70, 138, 500, 230);
}
if (index % 4 == 1) {
panel.setBounds(700, 138, 500, 230);
}
if (index % 4 == 2) {
panel.setBounds(70, 380, 500, 230);
} else if (index % 4 == 3) {
panel.setBounds(700, 380, 500, 230);
}
} else {
break;
}
}
}
【问题讨论】:
-
1) 为了尽快获得更好的帮助,edit 添加minimal reproducible example 或Short, Self Contained, Correct Example。首先使用一两个按钮解决问题,并忽略图标,除非这会影响问题。 2) Java GUI 必须在不同的语言环境中使用不同的 PLAF 在不同的操作系统、屏幕尺寸、屏幕分辨率等上工作。因此,它们不利于像素完美布局。而是使用布局管理器,或 combinations of them 以及 white space 的布局填充和边框。
标签: java swing button actionlistener