2013-01-05 25 views
1

我有一個Java代碼,在那裏我實現了一個半透明的JPanel,並在其上用Graphics 2D繪製了一個圖像。此圖片是一個PNG,包含一個白色矩形,80%不透明,遍佈JFrame。現在我需要添加一個JTextPane來顯示數據(我將它設置爲使用應用程序包中的自定義字體),但是我無法設法讓它變成半透明:它的白色背景是不透明的(即使使用textPane.setOpaque(false);設置)和使我的JFrame的透明度有點無用......很不爽。在透明的JFrame上移除JTextPane的沒有setOpaque()的白色背景

所以我正在尋找一種方法來消除這個讓我感到困惑的白色背景。

我翻了很多谷歌搜索,但我發現的一切都是一個布爾值來設置JTextPane的不透明度。我還發現,使用圖形2D,我可以創建一個自定義的JTextPane並覆蓋它的背景,但它沒有工作......我已經嘗試了所有這些。

public class MyWindow extends JFrame { 

    private static class MyTextPane extends JTextPane { 
     public MyTextPane() { 
      super(); 

      setText("Hello World"); 
      setOpaque(false); 
      setBackground(new Color(0,0,0,0)); 
     } 

     @Override 
     protected void paintComponent(Graphics g) { 
      g.setColor(new Color(0, 0, 0, 0)); 
      g.fillRect(0, 0, getWidth(), getHeight()); 

      super.paintComponent(g); 
     } 
    } 

    public static void main(String[] args) { 

     MyTextPane textPane = new MyTextPane(); 

     JPanel panel = new JPanel() { 

      private static final long serialVersionUID = 1L; 

      @Override 
         protected void paintComponent(Graphics g) { 
           try { 
       Image img = ImageIO.read(new File("images/bg.png")); 
       g.drawImage(img, 0, 0, this.getWidth(), this.getHeight(), this); 
           } catch (IOException e) { 
       e.printStackTrace(); 
       } 

           if (g instanceof Graphics2D) { 
           final int R = 240; 
           final int G = 240; 
           final int B = 240; 

       Paint p = 
        new GradientPaint(0.0f, 0.0f, new Color(R, G, B, 0), 
         0.0f, getHeight(), new Color(R, G, B, 0), true); 
       Graphics2D g2d = (Graphics2D)g; 
       g2d.setPaint(p); 
       g2d.fillRect(0, 0, getWidth(), getHeight()); 
      } 
     } 
    }; 
    panel.add(textPane); 
    setContentPane(panel); 

    } 

(產地與Oracle的解釋JFrame中透亮,就在這裏HERE)謝謝!

+1

你添加文本窗格滾動窗格? – MadProgrammer

+0

不,我將它添加到'Jpanel panel = new JPanel();'。我要編輯我的帖子:) – guillaume

+0

什麼是MyTestPane? – MadProgrammer

回答

4

這是怎麼了,我可能接近這種想法......

enter image description here

public class OverlayTextArea { 

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

    public OverlayTextArea() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (Exception ex) { 
       } 

       JFrame frame = new JFrame("Testing"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.setContentPane(new ImagePane()); 
       frame.setLayout(new BorderLayout()); 
       frame.add(new TransparentTextArea()); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    public class TransparentTextArea extends JTextArea { 

     public TransparentTextArea() { 
      setOpaque(false); 
      setBorder(new CompoundBorder(new EmptyBorder(10, 10, 10, 10), new LineBorder(Color.LIGHT_GRAY))); 
     } 

     @Override 
     protected void paintComponent(Graphics g) { 
      g.setColor(new Color(255, 255, 255, 128)); 
      Insets insets = getInsets(); 
      int x = insets.left; 
      int y = insets.top; 
      int width = getWidth() - (insets.left + insets.right); 
      int height = getHeight() - (insets.top + insets.bottom); 
      g.fillRect(x, y, width, height); 
      super.paintComponent(g); 
     } 

    } 

    public class ImagePane extends JPanel { 

     private BufferedImage background; 

     public ImagePane() { 
      try { 
       background = ImageIO.read(new File("/path/to/background.img")); 
      } catch (IOException ex) { 
       ex.printStackTrace(); 
      } 
     } 

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

     @Override 
     protected void paintComponent(Graphics g) { 
      super.paintComponent(g); 
      if (background != null) { 
       int x = (getWidth() - background.getWidth())/2; 
       int y = (getHeight() - background.getHeight())/2; 
       g.drawImage(background, x, y, this); 
      } 
     } 

    } 

} 

反饋

  • 你真的應該叫super.paintComponent,不這樣做可能會導致這有些嚴重的麻煩,特別是當你處理透明組件時。
  • 請勿在paintXxx方法內執行任何長時間運行的任務,如加載圖像。這些方法旨在迅速返回,也可稱爲內連續多次...
+0

謝謝!它解決了這個問題:我通過TransparentTextArea替換了MyTextPane類,並且成功了。再次感謝,我一段時間以來一直在思考這個問題。 – guillaume

+0

@GuillaumeCendre很高興解決了它;) – MadProgrammer

+0

不適用於Nimbus Look&Feel。在這個L&F下,TextArea的ui被設置爲'SynthTextAreaUI'的一個實例。 'paintComponent'調用'ui.update(scratchGraphics,this);',最終執行這個代碼: 'if((subregion && style.isOpaque(state))|| (!subregion && c。isOpaque()))' 其中OR的第一部分結束爲真,因此'c'被設置爲不透明沒有任何影響 –

0

,使其與Nimbus的大號&˚F工作:

jta.setOpaque(false); 
jta.setBorder(BorderFactory.createEmptyBorder()); 
jta.setBackground(new Color(0,0,0,0));