2013-05-07 34 views
0

我正在嘗試編寫一個顯示十六進制值的GUI,它的關聯顏色基於選擇的任何一個JRadioButton。我的ActionListener查找一個hashMap條目,它將單個單選按鈕存儲爲一個對象鍵,並且它是相關的十六進制值(作爲字符串)。在ActionListener中引用JRadioButtons

我可以用hashMap.get件獲取十六進制值,但是如何讓動作偵聽器引用任何JRadioButton,而不僅僅是'jrbBlue'或任何硬編碼?如果我把JRadioButton.addActionListener(錯誤 - 「無法從類型AbstractButton」)或jpRadioButton.addActionListener的非靜態方法addActionListener(ActionListener)靜態引用,我的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"); 
} 
} 

回答

0

寫一個動作監聽器,並把它添加到所有需要的單選按鈕。然後在actionPerformed(ActionEvent e)裏面,你可以調用e.getSource()。這會返回相應的JRadioButton,所以你可以投它並使用它。不需要在動作偵聽器中硬編碼變量。

相關問題