简体   繁体   中英

how do i make the listener notice which button is pressed

I need to create even more buttons. How do I make it so the listener knows b1 has been pressed, and will change the background to the hashmap value of salmon. This way when I create more buttons it will know which button is pressed, and change the background to the color associated with that button. I don't know what to do.

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.swing.*;




public class Collection extends JFrame {
    JPanel panel1 = new JPanel();
    public Collection(){
        Listen react = new Listen();
        Map<String, String> hashmap = new HashMap<String, String>();
    setLayout(new BorderLayout());  



        hashmap.put("Salmon","#FA8072" );

        JRadioButton b1 = new JRadioButton(hashmap.get("Salmon"));
        b1.addActionListener(react);
        panel1.add(b1);

        add(panel1, BorderLayout.CENTER);
    }





class Listen implements ActionListener{
    public void actionPerformed(ActionEvent e){

        panel1.setBackground();}}

public static void main(String[] args){
    Collection c = new Collection();
    c.setSize(300,300);
    c.setLocationRelativeTo(null);
    c.setTitle("colors");
    c.setDefaultCloseOperation(c.EXIT_ON_CLOSE);
    c.setVisible(true);
}}

In the method public void actionPerformed(ActionEvent e) you can use e.getSource() to determine the source of the event. You can use b1.setName to set the name of the button and then do something like ((JRadioButton)e.getSource()).getName() to retrieve it and determine which button was pressed. Alternatively you can use b1.putClientProperty("id", someObject) for the same purpose.

Try adding the information of color to the Listen class: create a constructor that accepts a Color or String that holds the color information per Listen instance. Then, where you call panel1.setBackground(), use the color information you have stored to specify the color there.

Perhaps something like this:

class Listen implements ActionListener {
     Color myColor;
     public Listen (Color newColor) {
           myColor = newColor;
     }
     public void actionPerformed (ActionEvent e) {
           panel1.setBackground(myColor);
     }
}

Then in your Collection class,

Listen react = new Listen();

becomes

Listen react = new Listen(hashmap.get("Salmon"));

and thus, when your button is clicked, the panel turns black. I suppose that you would have to redesign your HashMap system, which I think is great, but it will have to map Strings to Colors. You could use the Color constructor that take red, blue, and green values that can specify specific colors. Your salmon color, for example, would be FA = 250, 80 = 128, 72 = 114, so hashmap.put("Salmon", new Color(250, 128, 114));

If you have any questions, let me know!

Look into the observer pattern. You can attach an observer to an object which will change what it's attached to based on what it's observing. The idea is you can attach as many observers as you'd like, and change the state of the attachment accordingly. This works well for action listeners and button listeners especially.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM