java 将透明 JPanel 放在另一个 JPanel 上不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14068956/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 14:57:29  来源:igfitidea点击:

placing a transparent JPanel on top of another JPanel not working

javaswingjpanelglasspanejlayer

提问by Nikhil

I am trying to place a JPanel on top of another JPanel which contains a JTextArea and a button and i want to the upper apnel to be transparent. I have tried it by making the setOpaque(false) of the upper panel. but it is not working. Can anyone help me to get through this? Thanks in advance!

我试图将一个 JPanel 放在另一个包含 JTextArea 和一个按钮的 JPanel 之上,我希望上部 apnel 是透明的。我已经通过制作上面板的 setOpaque(false) 进行了尝试。但它不起作用。谁能帮我解决这个问题?提前致谢!

public class JpanelTest extends JPanel
{
    public JpanelTest()
    {
    super();
    onInit();
}
private void onInit()
{
    setLayout(new BorderLayout());

    JPanel panel = new JPanel();
    panel.setLayout(new BorderLayout());
    panel.add(new JTextArea(100,100),BorderLayout.CENTER);
    panel.add(new JButton("submit"),BorderLayout.SOUTH);

    JPanel glass = new JPanel();
    glass.setOpaque(false);

    add(panel,BorderLayout.CENTER);
    add(glass,BorderLayout.CENTER);
    setVisible(true);
}

public static void main(String args[])
{
    new JpanelTest();
}
}

采纳答案by PhiLho

Indeed, it would be useful to tell the reason why you want panels one over another.

确实,说出您想要一个又一个面板的原因会很有用。

Starting with your code, and changing it a lot, I got it to work, but it might not do what you expect...

从您的代码开始,并对其进行了大量更改,我让它可以工作,但它可能无法达到您的预期......

import java.awt.*;
import javax.swing.*;

public class Test extends JFrame
{
  public Test()
  {
    super();

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(500, 200);

    onInit();

    setVisible(true);
  }
  private void onInit()
  {
    JLayeredPane lp = getLayeredPane();

    JPanel panel = new JPanel();
    panel.setLayout(new BorderLayout());
    panel.add(new JTextArea(), BorderLayout.CENTER);
    panel.add(new JButton("Submit"), BorderLayout.SOUTH);
    panel.setSize(300, 150); // Size is needed here, as there is no layout in lp

    JPanel glass = new JPanel();
    glass.setOpaque(false); // Set to true to see it
    glass.setBackground(Color.GREEN);
    glass.setSize(300, 150);
    glass.setLocation(10, 10);

    lp.add(panel, Integer.valueOf(1));
    lp.add(glass, Integer.valueOf(2));
  }

  public static void main(String args[])
  {
    // Schedule a job for the event-dispatching thread:
    // creating and showing this application's GUI.
    javax.swing.SwingUtilities.invokeLater(new Runnable()
    {
      public void run()
      {
        new Test();
      }
    });
  }
}

If totally transparent, well, it is like it isn't here! When opaque, it just covers some of the GUI, but doesn't prevent mouse clicks, for example.

如果完全透明,好吧,就像它不在这里一样!例如,当不透明时,它只会覆盖一些 GUI,但不会阻止鼠标点击。

回答by akf

Check out this tutorial on using Swing Root Panes.

查看有关使用Swing Root Panes 的教程。

The glass pane is useful when you want to be able to catch events or paint over an area that already contains one or more components. For example, you can deactivate mouse events for a multi-component region by having the glass pane intercept the events. Or you can display an image over multiple components using the glass pane.

当您希望能够捕捉事件或在已经包含一个或多个组件的区域上绘制时,玻璃窗格非常有用。例如,您可以通过让玻璃窗格拦截事件来停用多组件区域的鼠标事件。或者,您可以使用玻璃窗格在多个组件上显示图像。

回答by mKorbel

1) there are a few ways, there no issue to put JPanel, with covering full JFrames/JPanel areaor only part of Rectangle / Dimensionthat returns JFrames/JPanel

1)有几种方法,放置JPanel没有问题,覆盖全部JFrames/JPanel area或仅部分Rectangle / Dimension返回JFrames/JPanel

  • use JLayer(Java7)based on JXLayer (Java6)

  • use GlassPane

  • use JViewport

  • use OverlayLayout

  • use transucent JDialog / JWindow

  • 使用JLayer(Java7)基于JXLayer (Java6)

  • 利用 GlassPane

  • 利用 JViewport

  • 利用 OverlayLayout

  • 使用半透明 JDialog / JWindow

2) everything depends of if you want to protect against mouseand key eventsfrom the top layerto bottom, or not (to avoiding redispatchevents from - toand vice versa)

2) 一切都取决于您是否要防止mouse和密钥eventstop layerto bottom,或不(避免redispatch事件from - to,反之亦然)