首页 > 解决方案 > 如何使用单个按钮在不同的面板上执行不同的操作?

问题描述

我正在制作一个面板来显示底部带有“购买”和“收藏”按钮的房地产广告。我没有多次制作相同的面板,而是创建了一个为我生成广告面板的方法,我只需使用不同的坐标多次调用该方法。

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;
        }
    }
}

标签: javaswingbuttonactionlistener

解决方案


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);
    }
}
    
    

推荐阅读