java - 如何使用单个按钮在不同的面板上执行不同的操作?
问题描述
我正在制作一个面板来显示底部带有“购买”和“收藏”按钮的房地产广告。我没有多次制作相同的面板,而是创建了一个为我生成广告面板的方法,我只需使用不同的坐标多次调用该方法。
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;
}
}
}
解决方案
SExtend JButton 以包含一个字段 private 或记录它是哪个字段。所需要的只是一个ID。比这更进一步的是一种缓存形式,或者拥有一个保存信息的组件/容器可能会更好,例如屏幕上的坐标。
class SButton extends JButton {
int myID;
// Code that accepts an ActionListener and applies it to this button.
// Or use super classes' methods and constructors.
// Constructor has to set the button's command to include the ID
// The ID can be generated statically in sequence or provide by
// outside code. This is because the command is passed through
// the ActionEvent generated by the thread. So a simple way
// is to append the ID at the end of the command. This can
// be easy because the SButtons will all have the same label
// or you can provide a single base for their command for all.
}
class Ad {
Point myLocation;
// Ad code creates panel that uses an SButton, maybe providing ID.
}
class AdManager implements ActionListener {
Ad [] myAds = new Ad []; // use lists, hashmaps, etc. as appropriate.
void initAds () {
for (some kind of loop) {
make some kind of Ad;
set its ActionListener to this AdManager;
put it in myAds;
}
}
public void actionPerformed (ActionEvent e) {
String cmd = e.getActionCommand ();
int anID = cmd.subString (some index at the end);
theOneMethodThatSyedWroteThatHandlesAllTheSButtons (anID);
}
}
推荐阅读
- php - 如何使用 SOAP API 和 laravel 避免多次请求
- java - Java LocalDate 在 Spring bean 中不起作用
- sqlite - 有什么方法可以在 SQLite 中跨多个数据库实现视图?
- javascript - 提交类型表单的 onSubmit() 和 onChange() 事件返回为未定义
- r - 如何使用条形图获得可靠的刻度刻度,这些条形图将数字总结为 ggplot (R) 中的单个条形?
- javascript - 无法在 rails 表单中创建所需的 flatpickr 字段
- svg - 如何删除 SVG 周围的空格?
- apache-kafka - 为主题动态创建消息监听器
- python - 关于 Django 和 SECRET_KEY 的困惑
- python - 无法在python中动态更改变量(分数)