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.
- 1. Cách thêm trình nghe hành động nghe nhiều nút
- 2. Trình nghe hành động Java
- 3. Đặt Trình nghe hành động theo lập trình trong IOS
- 4. cách đính kèm một người nghe vào nút radio
- 5. Các nút radio trên Rails
- 6. Trình nghe hành động đối với JDialog cho nút được nhấp
- 7. Tại sao các Nút có trình nghe và lệnh hành động không có trong LWUIT?
- 8. Nhóm nút radio và radio
- 9. trình nghe nhấp chuột trên nút tiện ích con android
- 10. séc javascript nút radio tự động
- 11. Nút radio MFC - Hành vi DDX_Radio và DDX_Control
- 12. nút radio chọn trên nhãn nhấp vào
- 13. Nhiều nhóm nút radio trên hộp thoại
- 14. Trình nghe Onlclick không hoạt động đúng ..?
- 15. Nút radio Angularjs
- 16. jquery javascript: thực hiện hành động khi nút radio được chọn
- 17. nhận giá trị được lựa chọn của nút radio trong trường hợp hành động
- 18. Nút radio khởi động Twitter không hoạt động
- 19. Hành động nút UIAlertView?
- 20. Nút radio Yii
- 21. Tắt trình nghe cử chỉ trên DrawerLayout
- 22. Sử dụng JQuery, cách tốt nhất để đặt trình nghe sự kiện onclick cho nút radio là gì?
- 23. Hình nền nút radio
- 24. Cách chọn nút radio?
- 25. Chức năng bấm/thay đổi jQuery trên bộ nút radio
- 26. VBA Tương tác với các nút radio trên trang web
- 27. Nút radio Bootstrap Bỏ chọn
- 28. Nút radio gắn kết WinForm
- 29. CakePHP tạo các nút radio
- 30. Cách thêm nút radio trong nhóm nút?
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