[填空題] 下面ChangeTitle()中對b1和b2按鈕構(gòu)造監(jiān)聽器,實現(xiàn)當單擊b1按鈕時標題變?yōu)?students,當單擊b2按鈕時標題變?yōu)閠eachers
[填空題] 下面ChangeTitle()中對b1和b2按鈕構(gòu)造監(jiān)聽器,實現(xiàn)當單擊b1按鈕時標題變?yōu)?students,當單擊b2按鈕時標題變?yōu)閠eachers。請將程序補充完整。
public ChangeTitle()
super("Title Bar");
b1.a(chǎn)ddActionListener(this);
______
Jpanel pane=new Jpanel();
Pane.a(chǎn)dd(b1);
Pane.a(chǎn)dd(b2);
SetContentPane(pane);
public void actionPerformed(ActionEvent evt)
Object sourve=evt.getSource();
if (sourve==b1)
setTitle("Students");
else if (source==b2)
setTitle("Teachers");
repaint();
正確答案:b2.a(chǎn)ddActionListener(this);
參考解析:在ChangeTitle()中將接收器添加到 JButton對象,但從程序段中可知程序只給b1添加了接收器,沒有給b2添加接收器。actionPerformed (ActionEvent evt)中對來自兩個JButton對象的動作事件做出響應,evt對象的getSource()方法決定了事件的來源。如果它等于b1按鈕,則標題設(shè)置為 Students;如果它等于b2,則標題設(shè)置為Teachers。需要調(diào)用repaint(),這樣在方法中可能出現(xiàn)的標題改動之后可以重新繪制。
詞條內(nèi)容僅供參考,如果您需要解決具體問題
(尤其在法律、醫(yī)學等領(lǐng)域),建議您咨詢相關(guān)領(lǐng)域?qū)I(yè)人士。