[英]How i can access method from jframe to jpanel?
In my btcFrame
class I have the method在我的
btcFrame
类中,我有方法
private void closeButtonMouseClicked(MouseEvent evt){
this.dispose();
}
In my BtcTitleBarPanel
I have in button for that I tend to close the frame.在我的
BtcTitleBarPanel
我有一个按钮,我倾向于关闭框架。 In the constructor I am adding my listener to closeButton
like below.在构造函数中,我将我的侦听器添加到
closeButton
如下所示。
closeButton.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent evt){
closeButtonMouseClicked(evt);
}
});
Problem is I cannot reach closeButtonMouseClicked
method within the panel.问题是我无法在面板中访问
closeButtonMouseClicked
方法。 How should I be doing to solve this?我该怎么做才能解决这个问题?
Images in case.图片以防万一。 Methods from jframe Methods in jpanel
来自 jframe 的方法jpanel 中的方法
I would add a getter within the Panel
for the closeButton
.我会在
Panel
为closeButton
添加一个吸气剂。 After initializing the Panel
in the frame
I would add the listener to button within the Frame.
在
frame
初始化Panel
后,我会将侦听器添加到Frame.
按钮Frame.
Like, btcPanel.getCloseButton().addActionListener(new ...);
比如,
btcPanel.getCloseButton().addActionListener(new ...);
and within this action listener you could close the frame
, like btcFrame.this.dispose()
Though I am not sure if that is a good practise, I almost always create new classes for listening events, but in this case of yours maybe helpful.在这个动作监听器中,你可以关闭
frame
,比如btcFrame.this.dispose()
虽然我不确定这是否是一个好的做法,我几乎总是为监听事件创建新的类,但在你的这种情况下可能会有帮助。
PS(Out of topic): You should see how to name classes and methods, your naming style is wrong.(ie classes starts with Capital etc.)
PS(主题外):你应该看看如何命名类和方法,你的命名风格是错误的。(即类以大写等开头)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.