2013-05-07 1 views
0

16 진수 값을 표시하는 GUI를 작성하려고합니다. JRadioButton이 선택되는 것에 기반하여 연관된 색상을 사용하고 있습니다. 내 ActionListener는 개별 라디오 버튼을 객체 키로 저장하고 16 진수 값 (문자열)과 연결된 hashMap 항목을 조회합니다.ActionListener에서 JRadioButtons를 참조하는 것

hashMap.get 조각을 사용하여 16 진수 값을 얻을 수 있지만 'jrbBlue'또는 하드 코딩 된 것보다 JRadioButton을 참조하도록 작업 수신기를 얻으려면 어떻게해야합니까?

JRadioButton.addActionListener를 넣으면 Eclipse가 좋아하지 않습니다 (오류 - "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"); 
} 
} 

답변

0

단일 작업 수신기를 작성하고 필요한 모든 단일 선택 단추에 추가하십시오. 그런 다음 actionPerformed (ActionEvent e) 내부에서 e.getSource()를 호출 할 수 있습니다. 해당 JRadioButton을 반환하므로 캐스트하고 사용할 수 있습니다. 액션 리스너 내부의 변수를 하드 코딩 할 필요가 없습니다.

관련 문제