2012-07-03 30 views
5

我想根据单选按钮的选择来设置文本框的可编辑选项吗?如何在单选按钮上编写动作监听器?单选按钮上的动作监听器

+0

请参阅java教程:http://docs.oracle.com/javase/tutorial/uiswing/components/button.html和http://docs.oracle.com/javase/tutorial/uiswing/events/actionlistener。 html? – DNA

+1

这个问题是典型的从缺乏研究 –

+2

'JCheckbox'似乎更apropos。 – trashgod

回答

2

试试这个:

JRadioButton myRadioButton = new JRadioButton(""); 
myRadioButton.addActionListener(new ActionListener(){ 
    public void actionPerformed(ActionEvent e) { 
     // Do something here... 
    } 
}); 
+0

很高兴展示我们如何检测按钮组中哪个单选按钮处于活动状态。 – mins

+0

使用ItemListener而不是ActionListener作为ItemSelectable组件,如单选按钮 – Cybermonk

5

我的Java是有些生疏,但是这应该是你在找什么。

这里是你的听众:

private RadioListener implements ActionListener{ 

    private JTextField textField; 

    public RadioListener(JTextField textField){ 
     this.textField = textField; 
    } 

    public void actionPerformed(ActionEvent e){ 
     JRadioButton button = (JRadioButton) e.getSource(); 

     // Set enabled based on button text (you can use whatever text you prefer) 
     if (button.getText().equals("Enable")){ 
      textField.setEditable(true); 
     }else{ 
      textField.setEditable(false); 
     } 
    } 
} 

这里是设置它的代码。

JRadioButton enableButton = new JRadioButton("Enable"); 
JRadioButton disableButton = new JRadioButton("Disable"); 

JTextField field = new JTextField(); 

RadioListener listener = new RadioListener(field); 

enableButton.addActionListener(listener); 
disableButton.addActionListener(listener); 
+1

否'ButtonGroup'? – trashgod

+0

哦,对。就像我说的,在Java中生锈。 – zalpha314

6

这是我在这种情况下使用的解决方案。

//The text field 
    JTextField textField = new JTextField(); 

    //The buttons 
    JRadioButton rdbtnAllowEdit = new JRadioButton(); 
    JRadioButton rdbtnDisallowEdit = new JRadioButton(); 

    //The Group, make sure only one button is selected at a time in the group 
    ButtonGroup editableGroup = new ButtonGroup(); 
    editableGroup.add(rdbtnAllowEdit); 
    editableGroup.add(rdbtnDisallowEdit); 

    //add allow listener 
    rdbtnAllowEdit.addActionListener(new ActionListener() { 
     @Override 
     public void actionPerformed(ActionEvent e) { 
      textField.setEditable(true); 

     } 
    }); 

    //add disallow listener 
    rdbtnDisallowEdit.addActionListener(new ActionListener() { 
     @Override 
     public void actionPerformed(ActionEvent e) { 
      textField.setEditable(false); 

     } 
    }); 
0

此问题的另一个答案。从zalpha314的答案修改一些代码。

您可以知道该按钮的文本选择了哪个单选按钮,您也可以通过操作命令了解它。在oracle的单选按钮演示代码http://docs.oracle.com/javase/tutorial/uiswing/examples/components/RadioButtonDemoProject/src/components/RadioButtonDemo.java中,我学会了如何使用动作命令。

首先,定义了两个动作命令

final static String ON = "on" 
final static String OFF = "off" 

然后加入行动命令按钮

JRadioButton enableButton = new JRadioButton("Enable"); 
enableButton.setActionCommand(ON); 
JRadioButton disableButton = new JRadioButton("Disable"); 
disableButton.setActionCommand(OFF); 

所以在的actionPerformed,你可以得到的动作命令。

public void actionPerformed(ActionEvent e){ 
    String ac = e.getActionCommand(); 
    if (ac.equals(ON)){ 
     textField.setEditable(true); 
    }else{ 
     textField.setEditable(false); 
    } 
} 

行动命令也许当button.getText()是一个很长的字符串更好。