这是场景:我创建了一个可以删除或隐藏主面板内的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 } }更多推荐
发布评论