单选按钮上的动作侦听器

问题描述 投票:9回答:5

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

java swing applet actionlistener jradiobutton
5个回答
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);

        }
    });

4
投票

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

2
投票

试试这个:

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

1
投票

这个问题的另一个答案。修改zalpha314的答案中的一些代码。

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

首先,定义两个动作命令

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()是一个非常长的字符串时,动作命令可能更好。


0
投票

试试这个:

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");
        }
    });
}
© www.soinside.com 2019 - 2024. All rights reserved.