Java Swing 透明 JPanel 的问题

4
我有一个 JLayeredPane,里面添加了 3 个 JPanel。
我将这些 JPanel 设为透明(没有设置背景且 setOpaque(false))。我在这些 JPanel 上画线,只有最后添加的 JPanel 上的线是可见的。其他 JPanel 上的线不会通过顶部 JPanel 显示出来(即使我在添加时设置了不同的 zIndexes)。
有人知道解决方法吗?为什么它们不透明呢?
我创建了一个小测试程序(3 个类)。TestJPanel 和 TestJPanel1 分别绘制一条线,但位置不同,但我只能看到最后添加的 JPanel 的线。我看不到两条线,因为它不是透明的 :(
Main.Java:
import java.awt.BorderLayout;
import java.awt.Color;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;

public class Main extends JFrame {
  public Main() {
    setSize(400, 350);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    JLayeredPane lp = getLayeredPane();
    lp.setLayout(new BorderLayout());
    TestJPanel top = new TestJPanel();
    top.setOpaque(false);
    TestJPanel middle = new TestJPanel();
    middle.setOpaque(false);
    TestJPanel1 bottom = new TestJPanel1();
    bottom.setOpaque(false);

    lp.add(middle, BorderLayout.CENTER, new Integer(4));
    lp.add(top, BorderLayout.CENTER, new Integer(3));
    lp.add(bottom, BorderLayout.CENTER, new Integer(2));
    // the last one I have added (bottom) is visible and can't see the others through it

    setVisible(true);
  }

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

  }
}

TestJPanel.java

import java.awt.Graphics;


public class TestJPanel extends javax.swing.JPanel {

    /** Creates new form TestJPanel */
    public TestJPanel() {
        initComponents();
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        g.drawLine(25, 0, 25, 50);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>


    // Variables declaration - do not modify
    // End of variables declaration

}

TestJPanel1.java

import java.awt.Graphics;


public class TestJPanel1 extends javax.swing.JPanel {

    /** Creates new form TestJPanel */
    public TestJPanel1() {
        initComponents();
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        g.drawLine(50, 0, 50, 50);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>


    // Variables declaration - do not modify
    // End of variables declaration

}

我真的希望有人能尽快帮我解决这个问题,这个问题与IT技术有关。

2个回答

4

删除这行

lp.setLayout(new BorderLayout());

并将您的add()调用替换为
lp.add(component, layer);

您正在错误地使用LayeredPane - 通常情况下,您不希望为LayeredPane设置布局。我相信(但需要检查),您只看到一行的原因是,使用BorderLayout时,如果将多个组件添加到同一区域(例如BorderLayout.CENTER),则只有您添加的最后一个组件会被定位;其他组件实际上被从布局中删除了。
要了解更多详细信息,请参见 Swing教程中关于分层窗格的内容

1

你正在将你的面板添加到同一个边界布局区域。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接