我試圖隱藏當前幀,當用戶單擊按鈕但它會生成錯誤我試圖創建一個方法并在 actionlistener 中調用它,但它仍然不行JButton btnAdd = new JButton("Add");btnAdd.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { AddBook add1=new AddBook(); add1.show(); frame.setVisible(false); }});btnAdd.setBounds(135, 113, 101, 49);contentPane.add(btnAdd);框架無法解析
1 回答

月關寶盒
TA貢獻1772條經驗 獲得超5個贊
因為在您定義的frame
范圍內無法訪問該變量。ActionListener
任何一個:
使用
frame
字段或簡單地在(eg: )ActionListener
之后但在.frame
JFrame frame = new JFrame(..); btn.addActionListener(...);
setVisible(true)
嘗試找到
Window
按鈕所在的位置,SwingUtilities::windowForComponent可能會有所幫助,或者如果您想要聚焦窗口,請結合使用KeyboardFocusManager::getCurrentKeyboardFocusManager、KeyboardFocusManager::getFocusOwner和SwingUtilities::windowForComponent。
添加回答
舉報
0/150
提交
取消