【问题标题】:Can JRadioButtons be stored in mapJRadioButtons可以存储在地图中吗
【发布时间】:2014-05-02 20:21:40
【问题描述】:

我想知道您是否可以将 JRadioButtons 添加到 HashMap 作为键和 Color 作为值,例如:

Map <"JRadioButton, Color"> store =  new HashMap <"JRadioButton, Color">    
store.add(new JRadioButton("FF0000"), Color.red);

然后添加一个动作监听器来改变 panel.setBackground(store.get(e));

或者只使用 ArrayList 来保存字符串并创建 [] 单选按钮会更好吗? 我无法弄清楚如何让按钮与颜色值相关联。

【问题讨论】:

  • 这不是 Java。此外,要将按钮与颜色相关联,请使用传递给按钮的侦听器中的颜色。
  • 通用括号中不应有引号。

标签: java map key-value jradiobutton


【解决方案1】:

任何对象都可以作为键或值存储在 Map 上。但是,可能 Color 会比 JRadioButton 更好,但这取决于您的问题。

【讨论】:

    【解决方案2】:

    枚举可以在这里很好地工作,如果需要的话,不需要使用 Map 或 ArrayList....

    import java.awt.Color;
    import java.awt.GridLayout;
    import java.awt.event.*;
    
    import javax.swing.*;
    
    public class ColorBtns extends JPanel {
       public ColorBtns() {
          setLayout(new GridLayout(0, 1));
          ButtonGroup btnGroup = new ButtonGroup();
          ActionListener actnListener = new ActionListener() {
    
             @Override
             public void actionPerformed(ActionEvent evt) {
                String colorStr = evt.getActionCommand().toUpperCase();
                MyColor myColor = MyColor.valueOf(colorStr);
                setBackground(myColor.getColor());
             }
          };
          for (MyColor myColor : MyColor.values()) {
             JRadioButton radioBtn = new JRadioButton(myColor.getText());
             radioBtn.setOpaque(false);
             radioBtn.setActionCommand(myColor.getText());
             radioBtn.addActionListener(actnListener);
             btnGroup.add(radioBtn);
             add(radioBtn);
          }
       }
    
       private static void createAndShowGui() {
          ColorBtns mainPanel = new ColorBtns();
    
          JFrame frame = new JFrame("ColorBtns");
          frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
          frame.getContentPane().add(mainPanel);
          frame.pack();
          frame.setLocationByPlatform(true);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                createAndShowGui();
             }
          });
       }
    }
    
    enum MyColor {
       RED(Color.red, "Red"), ORANGE(Color.orange, "Orange"), YELLOW(Color.yellow,
             "Yellow"), GREEN(Color.green, "Green"), BLUE(Color.blue, "Blue");
       private Color color;
       private String text;
    
       private MyColor(Color color, String text) {
          this.color = color;
          this.text = text;
       }
    
       public Color getColor() {
          return color;
       }
    
       public String getText() {
          return text;
       }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-03
      • 2019-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-11
      • 2011-09-22
      • 1970-01-01
      相关资源
      最近更新 更多