简体   繁体   English

我如何从 jframe 访问方法到 jpanel?

[英]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 .我会在PanelcloseButton添加一个吸气剂。 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM