나는 현재 버튼을 눌렀을 때 배열 요소를 값으로 업데이트 한 다음 그 요소를 포함하는 프레임을 닫는 버튼 집합을 가지고있다.많은 버튼이 비슷한 작업을 수행합니다. actionPerformed를 단순화하는 방법이 있습니까?
현재, 그들의 행동은 다음과 같이 명시되어 있습니다 :
...
svwnb.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
input[5] = svwnb.getText();
dftframe.setVisible(false);
}
});
blypb.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
input[5] = blypb.getText();
dftframe.setVisible(false);
}
});
pw91b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
input[5] = pw91b.getText();
dftframe.setVisible(false);
}
});
b97db.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
input[5] = b97db.getText();
dftframe.setVisible(false);
}
});
pbepbesolb.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
input[5] = pbepbesolb.getText();
dftframe.setVisible(false);
}
});
...
간단한 방법으로이 문제를 단순화하는 방법이 있나요? getSource()
메서드를 사용해야 할 필요가 있지만이 같은 작업 메서드를 작성하는 데 경험이 없습니다.
public void actionPerformed(ActionEvent e){
input[5] = e.getSource().getText();
dftframe.setVisible(false);
}
난 그냥이 코드를 생성하는 방법을 정확히 모르는 :
더 간결하기 위해, 나는 코드가 다음 의사처럼 보일 것이라는 생각을 가지고있다.
@AndrewThompson Yep. 못 생겼어. 질문자는 자신의 수업에서 ActionListener 유형의 (비공개) 필드를 정의 할 수 있습니다. 나는 그것을 편집 할 것이다. 고맙습니다. – MockerTim
고마워요, 이것은 완벽하게 작동했습니다. 많이 감사합니다 – user1147964
@MockerTim 명확히 해주셔서 감사합니다. 좋은 편집, 훌륭한 대답. :) –