Java 如何在JFrame中的另一个JPanel之上显示/隐藏JPanel?
我有一个重新组合一些JPanel的主框架。我的JFrame已经完全填满了 我希望能够在左侧JFrame中的另一个JPanel上显示/隐藏一个小JPanel。此JPanel是用户的配置区域 所以这里是我的问题,在我的JFrame中,在一个小区域中显示JPanel的最佳方式是什么 我试过了,但没有按预期工作(这是单击设置图标时执行的代码):Java 如何在JFrame中的另一个JPanel之上显示/隐藏JPanel?,java,swing,jframe,jpanel,Java,Swing,Jframe,Jpanel,我有一个重新组合一些JPanel的主框架。我的JFrame已经完全填满了 我希望能够在左侧JFrame中的另一个JPanel上显示/隐藏一个小JPanel。此JPanel是用户的配置区域 所以这里是我的问题,在我的JFrame中,在一个小区域中显示JPanel的最佳方式是什么 我试过了,但没有按预期工作(这是单击设置图标时执行的代码): 谢谢 您可以用玻璃窗格覆盖框架内容 我用它来显示JFrame内部的效果或标记,这要感谢我提出了一个解决方案,它的工作原理与预期一致: public void
谢谢 您可以用玻璃窗格覆盖框架内容 我用它来显示
JFrame
内部的效果或标记,这要感谢我提出了一个解决方案,它的工作原理与预期一致:
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);
}
}
您只需将所有要隐藏/显示在
分层窗格中的组件
放入中
您是否尝试过使用分层窗格@brainiac080195?哦,我不知道,我会尝试一下!坦率地说,glassPane
会更简单JFrame
已经有了JLayeredPane
,其中glassPane
是其中的一部分,虽然它可能“起作用”,但我认为它是可行的解决方案,但这是您需要解决的问题,不是我的,您可能需要在正在使用的容器上使用重新验证
和重新喷漆
changing@HJerem将FSettings添加到glasspane后,尝试调用setSize()和setLocation()。
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);
}
}
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);
}
}