当前位置: 首页 > 知识库问答 >
问题:

如何在JFrame中的另一个JPanel之上显示/隐藏JPanel?

杜河
2023-03-14

我有一个重新组合一些JPanel的主框架。我的JFrame已经完全填满了。

我希望能够在左侧JFrame中的另一个JPanel上显示/隐藏一个小JPanel。此JPanel是用户的配置区域。

所以这里是我的问题,在我的JFrame中,在一个小区域中显示JPanel的最好方式是什么?

我试过了,但没有按预期工作(这是单击设置图标时执行的代码):

private void jLabelSettingsMouseClicked(java.awt.event.MouseEvent evt) {                                            
    settingsActive = !this.jLabelEmargement.isVisible();        
    if(!settingsActive){
        FSettings.setSize(222, 380);
        FSettings.setLocation(0, 150);
        FSettings.setVisible(true);
        FSettings.setBackground(new Color(226,236,241));
        this.add(FSettings,BorderLayout.CENTER);
        this.frameLearners.setVisible(false);
        this.jLabelEmargement.setVisible(false);
        this.jLabelFinalEval.setVisible(false);
        this.jLabelLeaners.setVisible(false);
    }
    else{
        FSettings.setVisible(false);
        this.frameLearners.setVisible(true);
        this.jLabelEmargement.setVisible(true);
        this.jLabelFinalEval.setVisible(true);
        this.jLabelLeaners.setVisible(true);
    }
}

谢谢

共有2个答案

长孙作人
2023-03-14

多亏了brainiac,我想出了一个解决方案,它的工作正如预期的那样:

public void toggleSettings(){
    if(this.jLabelEmargement.isVisible()){
        // Set size of JPanel
        FSettings.setSize(222, 380);
        // Set location of JPanel
        FSettings.setLocation(0, 150);
        // Show JPanel
        FSettings.setVisible(true);
        FSettings.setBackground(new Color(226,236,241));
        // Add JPanel to LayeredPane
        jLayeredPaneSettings.add(FSettings, new Integer(5));
        this.frameLearners.setVisible(false);
        this.jLabelEmargement.setVisible(false);
        this.jLabelFinalEval.setVisible(false);
        this.jLabelLeaners.setVisible(false);
        ImageIcon icon = new ImageIcon(getClass().getResource("/com/images/cog_00997d_28.png"));
        jLabelSettings.setIcon(icon);
    }
    else{
        // Hide JPanel
        FSettings.setVisible(false);
        // Remove from LayeredPane
        jLayeredPaneSettings.remove(FSettings);
        this.frameLearners.setVisible(true);
        this.jLabelEmargement.setVisible(true);
        this.jLabelFinalEval.setVisible(true);
        this.jLabelLeaners.setVisible(true);
        ImageIcon icon = new ImageIcon(getClass().getResource("/com/images/cog_000000_28.png"));
        jLabelSettings.setIcon(icon);
    }
}

你只需要把你想要隐藏/显示的所有组件放在一个LayeredPane中。

张伯寅
2023-03-14

您可以覆盖您的框架内容与玻璃。

http://www.java2s.com/Code/Java/Swing-JFC/DemonstrateuseofGlassPane.htm

JFrame myFrame = ...

JComponent glassPane = new JPanel(null);
myFrame.setGlassPane(glassPane);

private void jLabelSettingsMouseClicked(java.awt.event.MouseEvent evt) {                                            
    settingsActive = !this.jLabelEmargement.isVisible();        
    if(!settingsActive){
        FSettings.setSize(222, 380);
        FSettings.setLocation(0, 150);
        FSettings.setBackground(new Color(226,236,241));
        glassPane.add(FSettings);
        this.frameLearners.setVisible(false);
        this.jLabelEmargement.setVisible(false);
        this.jLabelFinalEval.setVisible(false);
        this.jLabelLeaners.setVisible(false);
    }
    else{
        glassPane.remove(FSettings);
        this.frameLearners.setVisible(true);
        this.jLabelEmargement.setVisible(true);
        this.jLabelFinalEval.setVisible(true);
        this.jLabelLeaners.setVisible(true);
    }
}

我用它来显示JFrame内的效果或标记。

 类似资料:
  • 所以我想有2个JPanels。向上JPanel(内嵌面板)和向下JPanel(主面板)。我想在JFrame中添加keyListener,所以当我按下任何键时,面板隐藏,这样我们就可以看到向下的面板。代码应该如何工作? 此代码不隐藏内部面板。发生了什么?

  • 在我的Swing应用程序中,我有2个JFrameA和B。当我单击JFrameA上的按钮时,它会打开JFrameB并隐藏它自己(我设法完成了这部分) 在JFrame B上,我在JTabbedPane上放置了4个JPanels。每个JPanel有2个JButtons。 我该怎么做? //JPanel类 公共类AddItemPanel扩展javax.swing.jPanel{

  • 一个jFrame:HomeView、一个jPanel:TopicListView、另一个jPanel:ReplyListView。 在HomeView中,我有一个菜单项,可以单击它来显示TopicListView。在TopicListView中,我希望有一个可以单击以显示ReplyListView的按钮。单击按钮时,它将调用openReplyListView()方法。该方法将创建一个新的JPane

  • 问题内容: 我是的新手。在我的应用程序中,我想在或中显示。 谁能帮我吗? 问题答案:

  • 问题内容: 我有两个要素。第一个包含国家(例如美国,加拿大,英国,俄罗斯,波兰…),第二个国家被隐藏, 仅 包含美国和加拿大(例如纽约,洛杉矶,芝加哥…或渥太华,温哥华)的城市,萨里…) 如果用户从第一个中选择“加拿大”或“美国” ,则第二个应显示并允许他选择城市。 有可能吗?我使用Ajax和JQuery Validation表单访问了许多网站,但没有找到类似的来源。 谢谢。 问题答案: 侦听选择

  • 我知道同样的问题已经被问过很多次了,但是我似乎真的没有在我的代码中发现阻碍JPanel类型的对象显示在JFrame中的错误。下面是扩展JFrame的类的构造函数: 当我运行main方法(这里没有显示)时,它只显示框架和按钮。如果有人能在这方面给点提示,我会非常感谢的。