2011-08-09 3 views
4

내 GUI에 투명 패널을 넣고 싶습니다 (Windows 7 창 헤더와 같은 경우 더 좋을 것입니다). 내가패널의 자바 투명 패널 및 사용자 정의 효과

AWTUtilities.setWindowOpacity(frame, (float)0.90); 

com.sun.awt.AWTUtilities을 사용하고 있지만 매개 변수 JFrame 같은 창입니다 및 JPanel 사용할 수 없습니다 전에

.

또한 Windows 7 헤더 버튼과 마찬가지로 JPanel 또는 JLabel에 영향을주고 싶습니다 (예 : 발광). 다른 재미있는 효과는 나에게도 도움이된다.

enter image description here

답변

7

튜토리얼 How to Create Translucent and Shaped Windows와 * How to Create Translucent and Shaped Windows *를 참조하십시오. 우수한 예배당 링크는 @camickr입니다. 당신은 시간이 있다면 예를 들어

,

import java.awt.event.*; 
import java.awt.Color; 
import java.awt.AlphaComposite; 
import javax.swing.*; 
import javax.swing.UIManager.LookAndFeelInfo; 

public class ButtonTest { 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       new ButtonTest().createAndShowGUI(); 
      } 
     }); 
    } 

    private JFrame frame; 
    private JButton opaqueButton1; 
    private JButton opaqueButton2; 
    private SoftJButton softButton1; 
    private SoftJButton softButton2; 

    public void createAndShowGUI() { 
     opaqueButton1 = new JButton("Opaque Button"); 
     opaqueButton2 = new JButton("Opaque Button"); 
     softButton1 = new SoftJButton("Transparent Button"); 
     softButton2 = new SoftJButton("Transparent Button"); 
     opaqueButton1.setBackground(Color.GREEN); 
     softButton1.setBackground(Color.GREEN); 
     frame = new JFrame(); 
     frame.getContentPane().setLayout(new java.awt.GridLayout(2, 2, 10, 10)); 
     frame.add(opaqueButton1); 
     frame.add(softButton1); 
     frame.add(opaqueButton2); 
     frame.add(softButton2); 
     frame.setSize(567, 350); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setVisible(true); 
     Timer alphaChanger = new Timer(30, new ActionListener() { 

      private float incrementer = -.03f; 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       float newAlpha = softButton1.getAlpha() + incrementer; 
       if (newAlpha < 0) { 
        newAlpha = 0; 
        incrementer = -incrementer; 
       } else if (newAlpha > 1f) { 
        newAlpha = 1f; 
        incrementer = -incrementer; 
       } 
       softButton1.setAlpha(newAlpha); 
       softButton2.setAlpha(newAlpha); 
      } 
     }); 
     alphaChanger.start(); 
     Timer uiChanger = new Timer(3500, new ActionListener() { 

      private LookAndFeelInfo[] laf = UIManager.getInstalledLookAndFeels(); 
      private int index = 1; 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       try { 
        UIManager.setLookAndFeel(laf[index].getClassName()); 
        SwingUtilities.updateComponentTreeUI(frame); 
       } catch (Exception exc) { 
        exc.printStackTrace(); 
       } 
       index = (index + 1) % laf.length; 
      } 
     }); 
     uiChanger.start(); 
    } 

    public static class SoftJButton extends JButton { 
     private static final JButton lafDeterminer = new JButton(); 
     private static final long serialVersionUID = 1L; 
     private boolean rectangularLAF; 
     private float alpha = 1f; 

     public SoftJButton() { 
      this(null, null); 
     } 

     public SoftJButton(String text) { 
      this(text, null); 
     } 

     public SoftJButton(String text, Icon icon) { 
      super(text, icon); 

      setOpaque(false); 
      setFocusPainted(false); 
     } 

     public float getAlpha() { 
      return alpha; 
     } 

     public void setAlpha(float alpha) { 
      this.alpha = alpha; 
      repaint(); 
     } 

     @Override 
     public void paintComponent(java.awt.Graphics g) { 
      java.awt.Graphics2D g2 = (java.awt.Graphics2D) g; 
      g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha)); 
      if (rectangularLAF && isBackgroundSet()) { 
       Color c = getBackground(); 
       g2.setColor(c); 
       g.fillRect(0, 0, getWidth(), getHeight()); 
      } 
      super.paintComponent(g2); 
     } 

     @Override 
     public void updateUI() { 
      super.updateUI(); 
      lafDeterminer.updateUI(); 
      rectangularLAF = lafDeterminer.isOpaque(); 
     } 
    } 
} 
+1

+1 처음에는 스윙 질문으로 대답하십시오. –

+0

아주 좋은 효과가 있습니다. 그러나 패널이 아닌 투명하게 만드는 기술을 제안 할 수 있다면 투명 해져서 나에게 매우 좋았습니다. 이 메서드는 JPanels에 대해 테스트했지만 SoftJPanel에서는'super (text, icon); '생성자가 작동하지 않습니다. – sajad

+2

@sajad'super (text, icon);는'JButton'을위한 생성자입니다. 여기에 JLabel을 넣으십시오. 자세한 정보는 Icon/ImageIcon'super (icon);'을 참고하십시오. http://download.oracle.com/javase/ 튜토리얼/uiswing/components/label.html 및 http://download.oracle.com/javase/tutorial/uiswing/components/icon.html – mKorbel

3

난 당신이 Filty Rich Clients 통해 이동하는 것이 좋습니다. 이 책을 사용하여 Swing 및 Java 2D로 놀라운 시각적 효과와 애니메이션 효과를 만드는 방법을 배울 수 있습니다. 고급 렌더링 기술뿐만 아니라 그래픽 및 애니메이션 기본 사항을 익히십시오.

편집 : 투명 패널 그것은 자신의 배경을 그릴 수 있습니다 부모의 배경을 그림 넘길 수 있습니다

setOpaque(false) 

를 호출과 creat합니다.

화면 캡처를 수행 한 다음이를 사용하여 패널 배경을 칠할 수 있습니다.

+0

선생님! JPanels에 자바 시각 효과를위한 샘플 코드를 찾고 싶습니다. – sajad

+0

setOpaque()에 대해 알고 있습니다. 내가 질문을 첨부 한 그림을 보면, 윈도우 7 시작 메뉴에 투명도가 있다는 것을 알 수 있습니다. 고맙습니다\ – sajad

관련 문제