在ActionListener中引用JRadioButtons

时间:2013-05-07 17:58:26

标签: hashmap actionlistener

我正在尝试编写一个显示十六进制值的GUI,并根据选择的JRadioButton显示相关颜色。我的ActionListener查找一个hashMap条目,该条目将各个单选按钮存储为对象键,并且它是关联的十六进制值(作为字符串)。

我可以使用hashMap.get片段获取十六进制值,但是如何让动作侦听器引用任何JRadioButton而不仅仅是'jrbBlue'或任何硬编码的?

Eclipse不喜欢它,如果我把JRadioButton.addActionListener(错误 - “无法对类型为AbstractButton的非静态方法addActionListener(ActionListener)进行静态引用”)或jpRadioButton.addActionListener,我的JPanel为按钮(它想要将addActionListener更改为addComponentListener和一堆其他addWhatevers,其中没有一个工作)。

我意识到还有其他方法可以写出这整篇文章,但我很想和我所拥有的东西一起工作而且我还在学习。

提前致谢。

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.border.*;

public class Colors extends JFrame  {

static Map<Object, String> hashMap = new HashMap<Object, String>();

String hex = "Hex Value";

//----  The JLabel message (hex value of the color selected)
private JLabel jlblMessage = new JLabel(hex, JLabel.CENTER);

//----  Create the radio buttons
private static JRadioButton jrbBlue = new JRadioButton("Blue");
private static JRadioButton jrbPurplish = new JRadioButton("Purplish");
private static JRadioButton jrbRed = new JRadioButton("Red");
private static JRadioButton jrbYellow = new JRadioButton("Yellow");
private static JRadioButton jrbGreen = new JRadioButton("Green");
private static JRadioButton jrbOrange = new JRadioButton("Orange");
private static JRadioButton jrbCyan = new JRadioButton("Cyan");
private static JRadioButton jrbCoral = new JRadioButton("Coral");
private static JRadioButton jrbFuscia = new JRadioButton("Fuscia");
private static JRadioButton jrbViolet = new JRadioButton("Violet");
private static JRadioButton jrbDodgerBlue = new JRadioButton("Dodger Blue");
private static JRadioButton jrbGrey = new JRadioButton("Grey");
private static JRadioButton jrbWhite = new JRadioButton("White");
private static JRadioButton jrbCrimson = new JRadioButton("Crimson");
private static JRadioButton jrbDarkOrchid = new JRadioButton("Dark Orchid");
private static JRadioButton jrbFirebrick = new JRadioButton("Firebrick");
private static JRadioButton jrbHotPink = new JRadioButton("Hot Pink");
private static JRadioButton jrbMaroon = new JRadioButton("Maroon");
private static JRadioButton jrbDarkBlue = new JRadioButton("Dark Blue");
private static JRadioButton jrbTurquoise = new JRadioButton("Turquoise");

public Colors() {

    //----  JLabel placement
    jlblMessage.setBorder(new LineBorder(Color.BLACK, 2));
    add(jlblMessage, BorderLayout.CENTER);

    //----  Add the radio buttons to the JPanel
    JPanel jpRadioButtons = new JPanel();
    jpRadioButtons.setLayout(new GridLayout(3, 1));
    jpRadioButtons.add(jrbBlue);
    jpRadioButtons.add(jrbPurplish);
    jpRadioButtons.add(jrbRed);
    jpRadioButtons.add(jrbYellow);
    jpRadioButtons.add(jrbGreen);
    jpRadioButtons.add(jrbOrange);
    jpRadioButtons.add(jrbCyan);
    jpRadioButtons.add(jrbCoral);
    jpRadioButtons.add(jrbFuscia);
    jpRadioButtons.add(jrbViolet);
    jpRadioButtons.add(jrbDodgerBlue);
    jpRadioButtons.add(jrbGrey);
    jpRadioButtons.add(jrbWhite);
    jpRadioButtons.add(jrbCrimson);
    jpRadioButtons.add(jrbDarkOrchid);
    jpRadioButtons.add(jrbFirebrick);
    jpRadioButtons.add(jrbHotPink);
    jpRadioButtons.add(jrbMaroon);
    jpRadioButtons.add(jrbDarkBlue);
    jpRadioButtons.add(jrbTurquoise);

    add(jpRadioButtons, BorderLayout.WEST);

    //----  Add all the buttons to the same group
    ButtonGroup group = new ButtonGroup();
    group.add(jrbBlue);
    group.add(jrbPurplish);
    group.add(jrbRed);
    group.add(jrbYellow);
    group.add(jrbGreen);
    group.add(jrbOrange);
    group.add(jrbCyan);
    group.add(jrbCoral);
    group.add(jrbFuscia);
    group.add(jrbViolet);
    group.add(jrbDodgerBlue);
    group.add(jrbGrey);
    group.add(jrbWhite);
    group.add(jrbCrimson);
    group.add(jrbDarkOrchid);
    group.add(jrbFirebrick);
    group.add(jrbHotPink);
    group.add(jrbMaroon);
    group.add(jrbDarkBlue);
    group.add(jrbTurquoise);

    //jrbBlue.setSelected(true);
    //jlblMessage.setForeground(Color.decode("#0000FF"));

    //----  Action Listener
    jrbBlue.addActionListener(new ActionListener()  {   //<---- How to Reference whichever button is selected?
        @Override
        public void actionPerformed(ActionEvent e)  {
            jlblMessage.setForeground(Color.decode("#000000"));
            jlblMessage.setText(hashMap.get((JRadioButton)e.getSource()));
            getContentPane().setBackground(Color.decode(hashMap.get((JRadioButton)e.getSource())));
        }
    });

}

public static void main(String[] args)  {
    Colors frame = new Colors();
    frame.pack();
    frame.setTitle("Colors");
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true); 
    frame.setSize(900,300);

    //----  Color library map
    hashMap.put(jrbBlue, "#0000FF");
    hashMap.put(jrbPurplish, "#DF01D7");
    hashMap.put(jrbRed, "#FF0000");
    hashMap.put(jrbYellow, "#FFFF00");
    hashMap.put(jrbGreen, "#00FF00");
    hashMap.put(jrbOrange, "#FF8C00");
    hashMap.put(jrbCyan, "#00FFFF");
    hashMap.put(jrbCoral, "#FF7F50");
    hashMap.put(jrbFuscia, "#FF00FF");
    hashMap.put(jrbViolet, "#00FF00");
    hashMap.put(jrbDodgerBlue, "#1E90FF");
    hashMap.put(jrbGrey, "#C0C0C0");
    hashMap.put(jrbWhite, "#FFFFFF");
    hashMap.put(jrbCrimson, "#DC143C");
    hashMap.put(jrbDarkOrchid, "#9932CC");
    hashMap.put(jrbFirebrick, "#B22222");
    hashMap.put(jrbHotPink, "#FF69B4");
    hashMap.put(jrbDarkBlue, "#00008B");
    hashMap.put(jrbMaroon, "#800000");
    hashMap.put(jrbTurquoise, "#48D1CC");
}
}

1 个答案:

答案 0 :(得分:0)

编写单个动作侦听器并将其添加到所有必需的单选按钮。然后在actionPerformed(ActionEvent e)里面,你可以调用e.getSource()。这将返回相应的JRadioButton,因此您可以将其强制转换并使用它。无需在动作侦听器中对变量进行硬编码。