2013-01-07 110 views
1

我對在JPanel中顯示組件感到困惑。JPanel中的內容如何變得半透明?

假設,如果我創建一個自定義的JPanel具有0.8f的半透明如下: -

JPanel panel=new JPanel(){ 
     @Override 
     public void paint(Graphics g) 
     { 
      super.paint(g); 
      BufferedImage img=(BufferedImage)createImage(getWidth(),getHeight()); 
      Graphics2D g2=(Graphics2D) g.create(); 
      g2.setComposite(AlphaComposite.SrcOver.derive(0.8f)); 
      g2.drawImage(img,0,0,null); 
     } 
     @Override 
     public Dimension getPreferredSize() 
     { 
      return new Dimension(300,300); 
     } 
    }; 

現在,我將其設置爲框架的contentPane。

frame.setContentPane(panel); 

現在我給它添加一些按鈕。

frame.add(new JButton("Click Here")); 
    frame.add(new JButton("Click Here")); 
    frame.add(new JButton("Click Here")); 
    frame.add(new JButton("Click Here")); 

1)然後,在輸出爲何我得到半透明按鈕?作爲JPanel的是單層和予先塗上半透明圖像時我overrided其paint方法,然後加入按鈕,按鈕不能被半透明如他們應該過來。

2)同樣在這4個按鈕中只有2個是半透明的。爲什麼會有這種偏袒?

3)如果我在添加這4個按鈕之前添加一個表格,那麼一切都變得半透明。爲什麼?

Object[] names = new Object[] { 
      "Title", "Artist", "Album" 
     }; 
     String[][] data = new String[][] { 
      { "Los Angeles", "Sugarcult", "Lights Out" }, 
      { "Do It Alone", "Sugarcult", "Lights Out" }, 
      { "Made a Mistake", "Sugarcult", "Lights Out" }, 
      { "Kiss You Better", "Maximo Park", "A Certain Trigger" }, 
      { "All Over the Shop", "Maximo Park", "A Certain Trigger" }, 
      { "Going Missing", "Maximo Park", "A Certain Trigger" } 
     }; 
     JTable table = new JTable(data, names); 
     frame.add(table); 

4)如果我使用paintComponent(Graphics g)的JPanel中再沒有什麼是半透明的,我是否添加表或沒有或儘可能多的按鈕是added.Why?

(我正在詢問應用程序運行時的直接輸出。我知道當鼠標滾過這些按鈕時,或者如果表格中的任何一行被點擊,它將變得不透明,這是由於Swing的繪畫機制造成的。)

+0

1)'的JPanel面板=新JPanel(){ @Override 公共無效塗料(圖形克) { super.paint(克);'應'的JPanel面板=新JPanel() {圖片g) { super.paintComponent(g);'2)爲了更快地獲得更好的幫助,請發佈[SSCCE](http://sscce.org/)。 –

+0

另外,不要忘記'處理()'你創建的任何'Graphics'對象。 –

+0

@AndrewThompson:請參閱我更新的問題。最後一點4. –

回答

4

1)然後在輸出中爲什麼我會得到半透明的按鈕?由於JPanel是單獨的 ,所以我首先繪製了半透明圖像,當我忽略其 繪圖方法並添加了按鈕時,這些按鈕不應該是 ,因爲它們應該越過它。

實際上,你在按鈕上畫了半透明效果。 paint電話

  • paintComponent
  • paintBorder
  • paintChildren

然後你畫了什麼的已經繪(孩子)上的半透明效果。當你添加組件時,Swing將沒有什麼區別,Swing會在很多情況下繪製組件,第一次是組件第一次被實現(在屏幕上可見)和響應對髒區域(和批次其他人)...不要欺騙你的自我,你無法控制這個...

想想塗料過程作爲一種分層方法。首先,你畫的背景,那麼你畫的中間地面,最後,脫穎而出地上,然後你去了,濺在其...

enter image description here

public class TestTranslucentControls { 

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

    public TestTranslucentControls() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
       } 

       JFrame frame = new JFrame(); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.setLayout(new BorderLayout()); 
       frame.add(new TestPane()); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    public class TestPane extends JPanel { 

     private BufferedImage background; 

     public TestPane() { 
      setLayout(new GridBagLayout()); 
      try { 
       background = ImageIO.read(new File("C:/Users/shane/Dropbox/MegaTokyo/poster.jpg")); 
      } catch (IOException ex) { 
       ex.printStackTrace(); 
      } 
      add(new JButton("1")); 
      add(new JButton("2")); 
      add(new JButton("3")); 
      add(new JButton("4")); 
      add(new JButton("5")); 
     } 

     @Override 
     protected void paintComponent(Graphics g) { 
      super.paintComponent(g); 
      if (background != null) { 
       Graphics2D g2 = (Graphics2D) g.create(); 
       g2.setComposite(AlphaComposite.SrcOver.derive(0.25f)); 
       int x = (getWidth() - background.getWidth())/2; 
       int y = (getHeight() - background.getHeight())/2; 
       g2.drawImage(background, x, y, this); 
       g2.dispose(); 
      } 
     } 

     @Override 
     public Dimension getPreferredSize() { 
      return background == null ? new Dimension(300, 300) : new Dimension(background.getWidth(), background.getHeight()); 
     } 
    } 
} 

我想你可能會發現...

有用;)

1

通常,您想覆蓋paintComponent()而不是paint()。這可能是您透明度問題的原因。其次,你可能想要改變佈局。默認情況下,面板使用FlowLayout。您可能想要使用BorderLayout或GridBagLayout或兩者的組合。

+0

:佈局沒有效果,請參閱我更新的問題。我已經添加了另一個要點4。 –