简体   繁体   English

单选按钮上的动作侦听器

[英]Action Listener on a radio button

I would like to set editable option of a text box based on the selection of a radio button? 我想根据选择的单选按钮设置文本框的可编辑选项? How to code the action listener on the radio button? 如何在单选按钮上编写动作侦听器?

This is the solution that I would use in this case. 这是我在这种情况下使用的解决方案。

    //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);

        }
    });

My Java is a little rusty, but this should be what you're looking for. 我的Java有点生疏,但这应该是你想要的。

Here is your listener: 这是你的倾听者:

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);
        }
    }
}  

And here is the code that sets it up. 以下是设置它的代码。

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);

Try this: 试试这个:

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

Another answer for this question. 这个问题的另一个答案。 Modify a little code from zalpha314 's answer. 修改zalpha314的答案中的一些代码。

You could know which radio button is selected by the text of this button, and you could also know it by Action Command. 您可以通过此按钮的文本知道选择了哪个单选按钮,您也可以通过Action Command了解它。 In the oracle's radio button demo code http://docs.oracle.com/javase/tutorial/uiswing/examples/components/RadioButtonDemoProject/src/components/RadioButtonDemo.java , I learnt how to use action command. 在oracle的单选按钮演示代码http://docs.oracle.com/javase/tutorial/uiswing/examples/components/RadioButtonDemoProject/src/components/RadioButtonDemo.java中 ,我学习了如何使用action命令。

First, define two action command 首先,定义两个动作命令

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

Then add action command to buttons 然后向按钮添加动作命令

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

So in actionPerformed, you could get the action command. 所以在actionPerformed中,你可以得到动作命令。

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

Action command maybe better when the button.getText() is a very long string. button.getText()是一个非常长的字符串时,动作命令可能会更好。

Try this: 试试这个:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class NewStudent {
    public static void main(String[] args){
        NewStudent st=new NewStudent();
    }

    public NewStudent(){
        JFrame frame=new JFrame("STUDENT REGISTRATION FORM");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(800,600);
        frame.setVisible(true);

        JPanel p1=new JPanel();
        p1.setLayout(null);
        p1.setBackground(Color.CYAN);

        frame.add(p1);
        ButtonGroup buttonGroup=new ButtonGroup();
        JRadioButton male=new JRadioButton("MALE");
        male.setBounds(100,170,100,20);
        buttonGroup.add(male);
        p1.add(male);
        JRadioButton female=new JRadioButton("FEMALE");
        female.setBounds(250,170,100,20);
        buttonGroup.add(female);
        p1.add(female);
        JLabel sex =new JLabel("SEX:");
        sex.setBounds(10,200,100,20);
        p1.add(sex);
        final JTextField gender= new JTextField();
        gender.setBounds(100,200,300,20);
        p1.add(gender);

        male.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent ie){
                gender.setText("MALE");
            }
        });

        female.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent ie){
            gender.setText("FEMALE");
        }
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM