将透明JPanel放在另一个不工作的JPanel之上 [英] placing a transparent JPanel on top of another JPanel not working

查看:177
本文介绍了将透明JPanel放在另一个不工作的JPanel之上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将JPanel放在另一个包含JTextArea和按钮的JPanel之上,我希望上层apnel是透明的。我通过制作上面板的setOpaque(false)来尝试它。但它不起作用。任何人都可以帮我解决这个问题吗?提前致谢!

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!

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();
}
}


推荐答案

确实,告诉你为什么要把面板放在另一个面板上是有用的。

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();
      }
    });
  }
}

如果完全透明,那就像它不是'那里!当不透明时,它只覆盖了一些GUI,但不会阻止鼠标点击。例如。

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.

这篇关于将透明JPanel放在另一个不工作的JPanel之上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆