Tôi muốn đặt tùy chọn có thể chỉnh sửa của hộp văn bản dựa trên việc chọn nút radio? Làm thế nào để mã người nghe hành động trên nút radio?Trình nghe hành động trên nút radio
Trả lời
Hãy thử điều này:
JRadioButton myRadioButton = new JRadioButton("");
myRadioButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
// Do something here...
}
});
My Java là một chút gỉ, nhưng điều này nên được những gì bạn đang tìm kiếm.
Đây là người biết lắng nghe của bạn:
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);
}
}
}
Và đây là đoạn code mà đặt nó lên.
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);
Đây là giải pháp mà tôi sẽ sử dụng trong trường hợp này.
//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);
}
});
Một câu trả lời khác cho câu hỏi này. Sửa đổi một đoạn mã nhỏ từ câu trả lời của zalpha314.
Bạn có thể biết nút radio nào được chọn bằng văn bản của nút này và bạn cũng có thể biết điều đó bằng Lệnh tác vụ. Trong mã demo nút radio của oracle http://docs.oracle.com/javase/tutorial/uiswing/examples/components/RadioButtonDemoProject/src/components/RadioButtonDemo.java, tôi đã học cách sử dụng lệnh hành động.
Thứ nhất, xác định hai lệnh hành động
final static String ON = "on"
final static String OFF = "off"
Sau đó thêm lệnh hành động để nút
JRadioButton enableButton = new JRadioButton("Enable");
enableButton.setActionCommand(ON);
JRadioButton disableButton = new JRadioButton("Disable");
disableButton.setActionCommand(OFF);
Vì vậy, trong actionPerformed, bạn có thể nhận lệnh hành động.
public void actionPerformed(ActionEvent e){
String ac = e.getActionCommand();
if (ac.equals(ON)){
textField.setEditable(true);
}else{
textField.setEditable(false);
}
}
Lệnh tác vụ có thể tốt hơn khi button.getText()
là một chuỗi rất dài.
Xem Hướng dẫn java: http://docs.oracle.com/javase/tutorial/uiswing/components/button.html và http://docs.oracle.com/javase/tutorial/uiswing/events/actionlistener. html? – DNA
Câu hỏi này là điển hình từ việc thiếu nghiên cứu –
'JCheckbox' dường như nhiều apropos hơn. – trashgod