我正在尝试编写一个显示十六进制值的 GUI,并根据选择的 JRadioButton 关联颜色。我的 ActionListener 查找一个 hashMap 条目,该条目将单个单选按钮存储为对象键,并且它是关联的十六进制值(作为字符串)。
我可以使用 hashMap.get 片段获取十六进制值,但是如何让动作侦听器引用任何 JRadioButton 而不仅仅是“jrbBlue”或任何硬编码?
如果我把 JRadioButton.addActionListener(Error - "Cannot make a static reference to the non-static method addActionListener(ActionListener) from the type AbstractButton") 或 jpRadioButton.addActionListener, my JPanel for the button (它想要将 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");
}
}