从另一个类中删除jPanel(removing jPanel from another class)

编程入门 行业动态 更新时间:2024-10-28 00:14:35
从另一个类中删除jPanel(removing jPanel from another class)

这是场景:我创建了一个可以删除或隐藏主面板内的JPanel的函数,但是如果从另一个类调用它,它将无法工作。

功能是:

public void hideLoginPanel(){ mainPanel.removeAll(); mainPanel.repaint(); }

我可以在这里设置任何类似设置变量值的东西,但是删除mainPanel中的JPanel将不起作用。

这是我正在调用函数的另一个类的代码:

public class logService{ private MainFrame frame = new MainFrame(); private static logService dataService; public static logService getService() { if (dataService == null) { dataService = new logService(); } return dataService } public void removePanel(){ frame.hideLoginPanel // here's where I called the function } }

Here's the scenario: I created a function that can remove or hide the JPanel inside the main panel but if calling it from another class, it won't work.

The function was:

public void hideLoginPanel(){ mainPanel.removeAll(); mainPanel.repaint(); }

I can set anything like setting variable values here but removing the JPanel inside the mainPanel won't work.

Here's the code from another class where I'm calling the function:

public class logService{ private MainFrame frame = new MainFrame(); private static logService dataService; public static logService getService() { if (dataService == null) { dataService = new logService(); } return dataService } public void removePanel(){ frame.hideLoginPanel // here's where I called the function } }

最满意答案

通过查看您的代码,我假设您已经使用代码private MainFrame mainFrame = new MainFrame();创建了MainFrame的新对象private MainFrame mainFrame = new MainFrame();

相反,您应该将此对象传递给您调用hideLoginPanel()另一个类。 对象可以在构造函数中传递。 看看这两个类:

class MainFrame extends JFrame { public MainFrame() { this.setLayout(new BorderLayout()); this.setBounds(50, 50, 200, 300); this.setVisible(true); JPanel jp = new JPanel(); jp.setBackground(Color.BLUE); this.add(jp, BorderLayout.CENTER); LogService logService = new LogService(this); // This object should be used in your control where you want clear panel //logService.removePanel(); // Comment and uncomment this line to see the difference. Change in blue color } public void hideLoginPanel() { this.removeAll(); this.repaint(); } public static void main(String args[]) { new MainFrame(); } } class LogService { private MainFrame mainFrame; public LogService(MainFrame mainFrame) { this.mainFrame=mainFrame; } public void removePanel(){ mainFrame.hideLoginPanel(); // here's where I called the function } }

在此处输入图像描述

By looking at your code i assume you have created a new object of MainFrame by using code private MainFrame mainFrame = new MainFrame();

Instead you should pass this object to another class from where you are calling mehtod hideLoginPanel(). Object can be passed in constructor. Look at these two classes:

class MainFrame extends JFrame { public MainFrame() { this.setLayout(new BorderLayout()); this.setBounds(50, 50, 200, 300); this.setVisible(true); JPanel jp = new JPanel(); jp.setBackground(Color.BLUE); this.add(jp, BorderLayout.CENTER); LogService logService = new LogService(this); // This object should be used in your control where you want clear panel //logService.removePanel(); // Comment and uncomment this line to see the difference. Change in blue color } public void hideLoginPanel() { this.removeAll(); this.repaint(); } public static void main(String args[]) { new MainFrame(); } } class LogService { private MainFrame mainFrame; public LogService(MainFrame mainFrame) { this.mainFrame=mainFrame; } public void removePanel(){ mainFrame.hideLoginPanel(); // here's where I called the function } }

enter image description here

更多推荐

本文发布于:2023-08-07 19:35:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465659.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:类中   jPanel   class   removing

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!