我試圖實現一個簡單的窗口,其中包含兩個按鈕Yes
和No
。如何從ActionEvent獲取所有JButton組件的源代碼?
當Yes
點擊我要禁用No
按鈕和No
按下時,我想禁用Yes
按鈕。
我實現:
JButton btnYes = new JButton("Yes");
contentPane.add(btnYes);
btnYes.setActionCommand("Yes");
btnYes.addActionListener(this);
...同爲No
按鈕...
現在我趕上在這個方法的事件:
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("Yes"))
{
//I know how to get the button that caused the event
//but I don't know how to disable the OTHER button.
JButton source = (JButton)e.getSource();
//Handle the source button...
}
}
在上述方法中,我可以訪問導致事件的按鈕,但不能訪問其他按鈕。
獲取按鈕的最佳方法是什麼?
使用'JCheckBox'或'JToggleButon'。 –