2010-04-19 71 views
4

我有一個JLayeredPane,我添加了3個JPanel。Java Swing透明JPanels問題

我使JPanels透明(沒有背景設置和setOpaque(false))。我在JPanel上繪製線條,只有最後添加的JPanel上的線條可見。其他JPanel的行通過頂層JPanel不可見(即使添加它們時添加了不同的zIndexes)。

任何人都知道這個解決方案?他們爲什麼不透明?

我創建了一個小測試程序(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(); 

    } 
} 

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 

} 

我真的希望有人能幫助我a.s.a.p.有這個問題。

回答

4

刪除線

lp.setLayout(new BorderLayout()); 

並更換的add()與

lp.add(component, layer); 

您使用的layeredPane正確調用 - 用的layeredPane,你(通常)不想設置佈局。我相信(但必須檢查),你只看到一條線的原因是,使用BorderLayout,如果你添加多個組件到同一區域(例如BorderLayout.CENTER),只有你添加的最後一個被定位;其他人從佈局中被有效地移除。

有關更多詳細信息,請參閱the Swing tutorial on Layered Panes

1

你加入你的面板相同的BorderLayout的區域。