【问题标题】:removing jPanel from another class从另一个类中删除 jPanel
【发布时间】:2015-02-20 16:55:46
【问题描述】:

这是场景: 我创建了一个可以在主面板中删除或隐藏 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
    }
}

【问题讨论】:

  • “不起作用”到底是什么意思?删除 JPanel 后是否调用 revalidate()?你的MCVE在哪里?
  • 您是从非 EDT 线程调用 hideLoginPanel() 吗?你是否在代码中吞下了Exceptions?
  • 将不起作用意味着根本没有任何更改,我仍然可以单击主面板内现有面板中的按钮。但是如果在同一个类上调用该函数,它会完成删除主面板内的面板的工作。抱歉,如果这有点令人困惑。
  • @Gábor Bakos 对不起,我不认真。但我没有收到任何 printStackTrace 错误
  • 你能提供比这更多的代码吗?例如“其他类”?

标签: java swing jpanel


【解决方案1】:

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

相反,您应该将此对象传递给另一个您调用 mehtod 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
    }
}

【讨论】:

  • 你能举个例子吗,对不起是新手
  • 事情是,从一个jpanel类,我调用logservice类,服务类从jframe调用函数。
  • 因为 MainFrame 是代码的起点。您始终可以将其对象传递给其他组件。
  • 如果还是不行,分享一下创建logservice对象的代码
【解决方案2】:
import javax.swing.*;

public class Test extends JFrame {
    private JPanel nPanel;
    private JLabel label;

    Test() {
        setSize(300,300);
        nPanel = new JPanel();
        label = new JLabel("Hello!");
        nPanel.add(label);
        add(nPanel);
    }

    public void clear(JPanel panel) {
        panel.removeAll();
        panel.repaint();
    }

    public static void main(String[] args) {
        Test t = new Test();
        t.setVisible(true);
        t.clear(t.nPanel); //if you comment this line, you will see label said "Hello" to you!
    }
}

【讨论】:

  • 这是一种将您的面板提供给该方法的方法,如果代码没问题,它应该可以工作,欢迎您,给我一分钟,我会为您测试并再次编辑代码
  • 我需要调用另一个类的函数
  • 它是一样的,没有什么不同!只需在另一个类中创建您的面板和标签,并使用 clear 方法创建一个类,然后在您的 Main 中创建类的对象并使用它,祝你好运:-)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-08-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多