1
클릭 할 때 JPanel의 모양을 토글 할 JMenuItem (Show History)을 추가하고 있습니다. 그러나 이렇게하고 나면, 그 메뉴 항목의 제목을 변경하여 반대 동작 (상태 기록 숨기기)을 표시하려고합니다. 해당 메뉴 항목의 텍스트 만 변경하거나 이전 JMenuItem을 제거하고 새 항목을 추가해야합니까?클릭시 JMenuItem의 제목 텍스트를 변경 하시겠습니까?
JMenuItem history = new JMenuItem("Show History");
history.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//code here to show the history
//history.changeText("Hide History") OR viewMenu.remove(history) and create/add new one
}
});
viewMenu.add(history);
고마워요!
history.setText("Hide History");
를 그리고 역사 final
합니다
"history"변수를 final로 지정할 필요가 없습니다. getSource() 메소드를 사용하여 ActionEvent에서 메뉴 항목을 가져온다. – camickr
완벽하게 작동했습니다. 실제로 그것을 최종적으로 만들어야했습니다. 나는 changeText와 가까웠다. ;) 감사! – Joey