Java Swing透明JPanels问题 [英] Java Swing transparent JPanels problem

查看:118
本文介绍了Java Swing透明JPanels问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JLayeredPane,其中添加了3个JPanels.

I'm having A JLayeredPane where I add 3 JPanels.

我使JPanels透明(未设置背景并且setOpaque(false)).我在JPanels上绘制线条,只有最后添加的JPanel上的线条可见.通过该顶部JPanel看不到其他JPanels的行(即使我在添加它们时添加了不同的zIndexes).

I make the JPanels transparent (no background is set and setOpaque(false)). I draw lines on the JPanels and only the line on the last JPanel which has been added is visible. The lines of the other JPanels are not visible through that top JPanel (even if I have added different zIndexes when adding them).

有人知道解决方案吗?为什么它们不透明?

Anybody who knows a solution for this? Why aren't they transparent?

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

I have created a little test program (3 classes). (TestJPanel and TestJPanel1 draw a line but on a different position but I only see the line of the last added JPanel. I don't see 2 lines, because it isn't transparent :( )

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

}

我真的希望有人可以帮我一个忙.这个问题.

I really hope somebody could help me out a.s.a.p. with this problem.

推荐答案

删除行

lp.setLayout(new BorderLayout());

并将您的add()调用替换为

and replace your add() calls with

lp.add(component, layer);

您错误地使用了LayeredPane-使用LayeredPane,您(通常)不想设置布局.我相信(但必须检查)您只看到一行的原因是,使用BorderLayout,如果将多个组件添加到同一区域(例如BorderLayout.CENTER),则只会添加最后添加的组件;其他有效地从布局中删除.

You're using LayeredPane incorrectly - with LayeredPane, you (typically) don't want to set a layout. I believe (but would have to check) that the reason you're only seeing the one line is that, with BorderLayout, if you add multiple components to the same region (e.g. BorderLayout.CENTER), only the last one you add gets positioned; the others are effectively removed from the layout.

有关更多详细信息,请参见有关分层窗格的Swing教程

For more detail, see the Swing tutorial on Layered Panes

这篇关于Java Swing透明JPanels问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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