Digg StumbleUpon LinkedIn YouTube Flickr Facebook Twitter RSS Reset

Java: Créer un JPanel Translucide

 set, swing, awt, JPanel, JFrame
{filelink=10253}

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTabbedPane;
import javax.swing.SwingUtilities;       
import java.awt.AlphaComposite;
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
 
public class JPanelTransparent extends JPanel {
 
public static void main(String []args) {
 
    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        JFrame frame = new JFrame();
 
        frame.setContentPane(new JPanelTransparent());
        frame.getContentPane().setLayout(new BorderLayout());
        JTabbedPane table=new JTabbedPane();
        table.add(new JPanel(),"Onglet 1");
        table.add(new JPanel(),"Onglet 2");
        frame.add(table);
 
        JPanel p = new JPanel();
        p.add(new JButton("Play"));
        p.add(new JButton("Pause"));
        p.add(new JButton("Stop"));
        frame.add(p, BorderLayout.SOUTH);
 
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle("Exemple JPanel Translucide");
        frame.setSize(400, 300);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
      }
    });
  } 
 
   BufferedImage mainImage = null;
  public void paint(Graphics g) {
    if (mainImage == null || mainImage.getWidth() != getWidth() || mainImage.getHeight() != getHeight()) {
 
      mainImage = (BufferedImage) createImage(getWidth(), getHeight());
    }
 
    Graphics2D g2 = mainImage.createGraphics();
    g2.setClip(g.getClip());
    super.paint(g2);
    g2.dispose();
 
    g2 = (Graphics2D) g.create();
    g2.setComposite(AlphaComposite.SrcOver.derive(0.2f));
    g2.drawImage(mainImage, 0, 0, null);
  }
}

A Voir sur le même Sujet:

  • Barre de progression
  • jcalendar
  • jpanel java
  • jpanel jbutton exemple
  • jframe dans un jpanel exemple
  • methode vider jpanel
  • jpanel java exemple
  • charger dynamiquement de jpanel dans jframe java
  • iframe src in jpanel
  • barre outlook