2015-04-07 229 views
0

enter image description here如何減少JFrame背景顏色的不透明度

我想創建一個這樣的框架。這是一個0.9不透明度的主框架,並擁有兩個JPanel s。其中一個面板是黑色的,另一個面板沒有任何背景色e:g setOpaque(false);

這裏是我試過,但沒有奏效:

的JFrame

public class STBG { 
    JFrame frame = new JFrame("Panel"); 
    BorderLayout bl = new BorderLayout(); 

    public STBG(){ 
     frame.setLayout(bl); 
     frame.setSize(800, 600); 
     frame.add(new LeftBar(), BorderLayout.WEST); 
     frame.add(new MainPanel(), BorderLayout.CENTER); 

     //frame.setUndecorated(true); 
     frame.setBackground(new Color(0, 255, 0, 1)); 

     frame.setLocationRelativeTo(null); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) throws IOException { 
     // TODO code application logic here 
     new STBG(); 
    } 
} 

左側面板

public class LeftBar extends JPanel{ 
    JPanel menuPanel; 
    JLabel schedule, history, settings, aboutApp; 
    public LeftBar(){ 
     setBackground(Color.BLACK); 
     setPreferredSize(new Dimension(100, 20)); 
     setLayout(new BorderLayout()); 
     menuPanel = new JPanel(new GridLayout(5,0)); 
     menuPanel.setBackground(Color.black); 


     settings = new JLabel("Settings"); 
     schedule = new JLabel("Schedule"); 
     history = new JLabel("History"); 
     menuPanel.add(settings); 
     menuPanel.add(schedule); 
     menuPanel.add(history); 

     aboutApp = new JLabel("About App"); 
     add(menuPanel, BorderLayout.CENTER); 
     add(aboutApp, BorderLayout.SOUTH); 
    } 
} 

主面板

public class MainPanel extends JPanel{ 
    public MainPanel(){ 
     setLayout(new GridLayout(0,1)); 
     add(new JLabel("TEXT")); 

     setOpaque(false); 
     setBackground(new Color(0, 0, 0, 1)); 
    } 
} 

任何想法如何使圖像類似?

+1

不使'JFrame'未裝飾,這是不可能的。此外,Swing不理解如何處理基於alpha的顏色,相反,您需要使面板透明並且「假」填充 – MadProgrammer

+0

@MadProgrammer感謝您的快速響應。如果我使'JFrame'未修飾,那麼關閉按鈕和最小化會消失。我試圖避免爲他們定製按鈕。無論如何,你可以通過僞造填充來解釋你的意思嗎?還是一個例子?謝謝 – Dan

+0

看看https://harryjoy.wordpress.com/2011/07/09/make-jframe-transparent/ – Anptk

回答

1

儘管tutorials似乎說了什麼,但似乎無論如何不使框架內容區域透明而不使框架內塗層。

擺動組件(除JFrame之外)不能很好地渲染透明背景顏色,該過程不會導致繪畫工件和其他相關問題的結束。

但是,你可以「僞造」它。您可以通過使JPanel透明(setOpaque(false))開始,然後覆蓋它的paintComponent方法和油漆什麼都填寫你想要的顏色,例如...

Fade

import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.EventQueue; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.GridLayout; 
import java.awt.LinearGradientPaint; 
import java.awt.Point; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

public class JavaApplication1048 { 

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

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

       System.out.println("Blah"); 

       JFrame frame = new JFrame("Testing"); 
       frame.setLayout(new GridLayout(1, 2)); 
       frame.setUndecorated(true); 
       frame.setBackground(new Color(0, 255, 0, 0)); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.add(new LeftPane()); 
       frame.add(new RightPane()); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    public class LeftPane extends JPanel { 

     public LeftPane() { 
      JLabel label = new JLabel("Left"); 
      label.setForeground(Color.WHITE); 
      setBackground(Color.BLACK); 
      add(label); 
     } 

     @Override 
     public Dimension getPreferredSize() { 
      return new Dimension(100, 100); 
     } 

    } 

    public class RightPane extends JPanel { 

     public RightPane() { 
      JLabel label = new JLabel("Right"); 
      label.setForeground(Color.WHITE); 
      setBackground(Color.BLACK); 
      add(label); 
      setOpaque(false); 
     } 

     @Override 
     public Dimension getPreferredSize() { 
      return new Dimension(100, 100); 
     } 

     @Override 
     protected void paintComponent(Graphics g) { 
      super.paintComponent(g); 
      Graphics2D g2d = (Graphics2D) g.create(); 
      LinearGradientPaint lgp = new LinearGradientPaint(
          new Point(0, 0), 
          new Point(getWidth(), 0), 
          new float[]{0f, 1f}, 
          new Color[]{getBackground(), applyAlpha(getBackground(), 0.5f)} 
      ); 
      g2d.setPaint(lgp); 
      g2d.fillRect(0, 0, getWidth(), getHeight()); 
      g2d.dispose(); 
     } 

     protected Color applyAlpha(Color color, float alpha) { 

      return new Color(color.getRed(), color.getGreen(), color.getBlue(), Math.round(255 * alpha)); 

     } 

    } 

} 
1

我剛修改過的例子之一,從Swing教程How to Create Translucent Windows。它似乎適用於Metal LAF,所以至少你會有按鈕。

import java.awt.*; 
import javax.swing.*; 
import static java.awt.GraphicsDevice.WindowTranslucency.*; 

public class FrameTranslucent extends JFrame { 
    public FrameTranslucent() { 
     super("Frame Translucent"); 

     setBackground(new Color(0,0,0,0)); 
     setSize(new Dimension(300,200)); 
     setLocationRelativeTo(null); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     JPanel panel = new JPanel(new BorderLayout()) { 
      @Override 
      protected void paintComponent(Graphics g) { 
       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, 255), true); 
        Graphics2D g2d = (Graphics2D)g; 
        g2d.setPaint(p); 
        g2d.fillRect(0, 0, getWidth(), getHeight()); 
       } 
      } 
     }; 
     setContentPane(panel); 

     JPanel left = new JPanel(new BorderLayout()); 
     left.setBackground(Color.BLACK); 
     left.add(new JLabel("Left Label Text"), BorderLayout.NORTH); 
     add(left, BorderLayout.WEST); 

     JPanel center = new JPanel(new BorderLayout()); 
     center.setBackground(new Color(0, 0, 0, 128)); 
     JLabel label = new JLabel("Center Label Text"); 
     label.setForeground(Color.RED); 
     center.add(label, BorderLayout.NORTH); 
     add(center, BorderLayout.CENTER); 
    } 

    public static void main(String[] args) { 
     // Determine what the GraphicsDevice can support. 
     GraphicsEnvironment ge = 
      GraphicsEnvironment.getLocalGraphicsEnvironment(); 
     GraphicsDevice gd = ge.getDefaultScreenDevice(); 
     boolean isPerPixelTranslucencySupported = 
      gd.isWindowTranslucencySupported(PERPIXEL_TRANSLUCENT); 

     //If translucent windows aren't supported, exit. 
     if (!isPerPixelTranslucencySupported) { 
      System.out.println(
       "Per-pixel translucency is not supported"); 
       System.exit(0); 
     } 

     JFrame.setDefaultLookAndFeelDecorated(true); 

     // Create the GUI on the event-dispatching thread 
     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       FrameTranslucent gtw = new FrameTranslucent(); 

       // Display the window. 
       gtw.setVisible(true); 
      } 
     }); 
    } 
} 

我沒有注意到任何問題,但我確定你到底想要什麼。

我也注意到使用透明背景時出現問題,這就是爲什麼我創建了Alpha Container以確保背景被正確繪製。如果這個例子不起作用,那麼AlphaContainer可能會有所幫助。

+0

這是一個恥辱,你不能讓本地框架裝飾工作。 – MadProgrammer