我需要創建更多的按鈕。我如何做到這一點,讓聽衆知道b1已被按下,並將背景改爲鮭魚的hashmap值。這樣,當我創建更多的按鈕時,它將知道哪個按鈕被按下,並將背景更改爲與該按鈕相關的顏色。我不知道該怎麼辦。我如何讓聽衆注意到哪個按鈕被按下
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);
}}