获取从java中的单选按钮中选择的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19449365/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get value that been selected from radio button in java
提问by Adriel
import javax.swing.*;
import java.awt.*;
public class RadioButtonTest extends JFrame {
private JTextField jtfAnswer = new JTextField(10);
private JRadioButton jrbMale = new JRadioButton("Male");
private JRadioButton jrbFemale = new JRadioButton("Female");
private JButton jbSubmit = new JButton("Submit");
public RadioButtonTest(){
setLayout(new GridLayout(5,1));
ButtonGroup group = new ButtonGroup();
group.add(jrbMale);
group.add(jrbFemale);
add(new Label("Select gender:"));
add(jrbMale);
add(jrbFemale);
add(jtfAnswer);
add(jbSubmit);
setTitle("Radio Button");
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocation(200, 200);
setSize(150, 150);
setVisible(true);
}
public static void main(String[] args) {
new RadioButtonTest();
}
}
I know should add an actionlistener
to obtain the selected values , but what is the content I should code in the actionlistener
?
我知道应该添加一个actionlistener
来获取选定的值,但是我应该在其中编码的内容是actionlistener
什么?
回答by jzd
You have to call addActionListener()
on the item you want to listen to, in this case it looks like you want to call it on your submit button. The action listener that you pass as a parameter then has the code that you want to execute. Check out the tutorial:
您必须调用addActionListener()
要收听的项目,在这种情况下,您似乎想在提交按钮上调用它。您作为参数传递的操作侦听器具有您要执行的代码。查看教程:
http://docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html
http://docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html
For each form item you will have to look in the API to understand what method to call to get the correct value. For example: getText()
or isSelected()
.
对于每个表单项,您必须查看 API 以了解调用什么方法来获取正确的值。例如:getText()
或isSelected()
。
回答by dic19
I know should add an
actionlistener
to obtain the selected values , but what is the content I should code in theactionlistener
?
我知道应该添加一个
actionlistener
来获取选定的值,但是我应该在其中编码的内容是actionlistener
什么?
Inside your ActionListener
you can ask who's the source of the action event and then set the text field's text as needed:
在您的内部,您ActionListener
可以询问谁是操作事件的来源,然后根据需要设置文本字段的文本:
ActionListener actionListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() instanceof JRadioButton){
JRadioButton radioButton = (JRadioButton) e.getSource();
if(radioButton.isSelected()){
jtfAnswer.setText(radioButton.getText());
}
}
}
};
jrbMale.addActionListener(actionListener);
jrbFemale.addActionListener(actionListener);
Notesuggested reading EventObject.getSource()
注意建议阅读EventObject.getSource()
回答by Luke
create your own action listner like this:
像这样创建你自己的动作监听器:
class CustomActionListener implements ActionListener{
private JTextField textField;
private JRadioButton btn;
public CustomActionListener( JRadioButton btn, JTextField field){
this.btn = btn;
this.textField = field;
}
@Override
public void actionPerformed(ActionEvent arg0) {
this.textField.setText( this.btn.getText() );
}
}
And then add it to your radio buttons:
然后将其添加到您的单选按钮中:
jrbMale.addActionListener( new CustomActionListener( jrbMale, jtfAnswer ) );
jrbFemale.addActionListener( new CustomActionListener( jrbFemale, jtfAnswer ) );