2012-07-03 31 views
5

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

+0

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

+1

Câu hỏi này là điển hình từ việc thiếu nghiên cứu –

+2

'JCheckbox' dường như nhiều apropos hơn. – trashgod

Trả lời

2

Hãy thử điều này:

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

Sẽ thật tuyệt khi cho biết cách chúng tôi phát hiện nút radio nào đang hoạt động trong nhóm nút. – mins

+0

Sử dụng ItemListener thay vì ActionListener cho các thành phần ItemSelectable như nút radio – Cybermonk

5

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

Không có 'ButtonGroup'? – trashgod

+0

Ồ, đúng vậy. Giống như tôi đã nói, rỉ sét ở Java. – zalpha314

6

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

     } 
    }); 
0

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.

Các vấn đề liên quan