【问题标题】:How to show/hide JPanel on top of another JPanel in a JFrame?如何在 JFrame 中的另一个 JPanel 之上显示/隐藏 JPanel?
【发布时间】:2016-01-14 12:36:07
【问题描述】:

我有一个重新组合一些 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);
    }
}

谢谢!

【问题讨论】:

  • 您是否尝试过使用分层窗格link
  • @brainiac080195 嗯,不知道,我试试看!

标签: java swing jframe jpanel


【解决方案1】:

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

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 内的效果或标记。

【讨论】:

  • 您可能需要在要更改的容器上使用revalidaterepaint
  • @HJerem 在您将 FSettings 添加到玻璃窗格后尝试调用 setSize() 和 setLocation()。
【解决方案2】:

感谢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

【讨论】:

  • 坦率地说,glassPane 会更简单
  • JFrame 已经有JLayeredPane,其中glassPane 是其中的一部分,虽然它可能“工作”,但我认为这是可行的解决方案,但这是你要处理的问题,不是我的
猜你喜欢
  • 2011-05-16
  • 2019-06-08
  • 2013-02-09
  • 2021-01-29
  • 2017-04-18
  • 1970-01-01
  • 1970-01-01
  • 2011-05-24
  • 1970-01-01
相关资源
最近更新 更多