Java Swing透明JPanels问题

Java Swing透明JPanels问题,java,swing,jpanel,transparent,Java,Swing,Jpanel,Transparent,我有一个JLayeredPane,在其中我添加了3个JPanel 我使JPanels透明(没有设置背景,设置不透明(false))。我在JPanel上画线,只有最后一个JPanel上添加的线是可见的。其他JPanel的行通过顶部的JPanel是不可见的(即使我在添加它们时添加了不同的zindex) 有谁知道这个问题的解决方案吗?为什么它们不透明 我创建了一个小测试程序(3个类)。 (TestJPanel和TestJPanel1画了一条线,但位置不同,但我只看到最后添加的JPanel的线。我看不到

我有一个JLayeredPane,在其中我添加了3个JPanel

我使JPanels透明(没有设置背景,设置不透明(false))。我在JPanel上画线,只有最后一个JPanel上添加的线是可见的。其他JPanel的行通过顶部的JPanel是不可见的(即使我在添加它们时添加了不同的zindex)

有谁知道这个问题的解决方案吗?为什么它们不透明

我创建了一个小测试程序(3个类)。 (TestJPanel和TestJPanel1画了一条线,但位置不同,但我只看到最后添加的JPanel的线。我看不到2条线,因为它不透明:()

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

  }
}
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

}
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

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

  }
}
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

}
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

}
导入java.awt.Graphics;
公共类TestJPanel扩展了javax.swing.JPanel{
/**创建新表单TestJPanel*/
公共测试面板(){
初始化组件();
}
@凌驾
公共组件(图形g){
超级组件(g);
g、 抽绳(25,0,25,50);
}
/**此方法从构造函数中调用,以
*初始化表单。
*警告:请勿修改此代码。此方法的内容为
*始终由表单编辑器重新生成。
*/
@抑制警告(“未选中”)
// 
私有组件(){
javax.swing.GroupLayout=newjavax.swing.GroupLayout(this);
这个.setLayout(布局);
layout.setHorizontalGroup(
createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0,400,短。最大值)
);
layout.setVerticalGroup(
createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0,300,短。最大值)
);
}// 
//变量声明-不修改
//变量结束声明
}
TestJPanel1.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();

  }
}
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

}
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

}
导入java.awt.Graphics;
公共类TestJPanel1扩展了javax.swing.JPanel{
/**创建新表单TestJPanel*/
公共测试jpanel1(){
初始化组件();
}
@凌驾
公共组件(图形g){
超级组件(g);
g、 抽绳(50,0,50,50);
}
/**此方法从构造函数中调用,以
*初始化表单。
*警告:请勿修改此代码。此方法的内容为
*始终由表单编辑器重新生成。
*/
@抑制警告(“未选中”)
// 
私有组件(){
javax.swing.GroupLayout=newjavax.swing.GroupLayout(this);
这个.setLayout(布局);
layout.setHorizontalGroup(
createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0,400,短。最大值)
);
layout.setVerticalGroup(
createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0,300,短。最大值)
);
}// 
//变量声明-不修改
//变量结束声明
}
我真的希望有人能帮我解决这个问题。

把电话线拔掉

lp.setLayout(new BorderLayout());
并将add()调用替换为

lp.add(component, layer);
您不正确地使用了LayeredPane-对于LayeredPane,您(通常)不想设置布局。我相信(但必须检查)您只看到一条线的原因是,对于BorderLayout,如果您将多个组件添加到同一区域(例如BorderLayout.CENTER),则会看到多个组件,只有最后添加的一个将被定位;其他的将从布局中有效删除


有关详细信息,请参见将面板添加到同一BorderLayout区域