D
D
DR_Demons2014-05-02 00:51:41
Java
DR_Demons, 2014-05-02 00:51:41

What is the secret of transparency in Java Swing?

Good Time! I am learning java and now swing in particular. I stumbled upon this on the internet

example
import javax.swing.JFrame;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;

import com.sun.awt.AWTUtilities;
   
  public class TransparentFrame extends JFrame {
       public TransparentFrame() {
           setTitle("Transparent Frame");
           setSize(400,400);
           setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
           JSlider slider = new JSlider(JSlider.HORIZONTAL, 10, 100, 50);
           
           add(slider);
           setVisible(true);
       }
       public static void main(String[] args) {
      	 
          new TransparentFrame();
            
           
       }
       
       public void stateChanged(ChangeEvent e) {
   	      JSlider slider = (JSlider) e.getSource();
   	      if(!slider.getValueIsAdjusting()){
   	    	 AWTUtilities.setWindowOpacity(TransparentFrame.this, slider.getValue()/100f);
   	      }
   	}
  }

According to the author's idea, it should turn out like this
So
tr1.png


but in fact it turns out
So
h_1398981500_6711975_2341fabcc5.png


Ie transparency does not work. Tell me what is wrong and how to implement it correctly?
Thanks in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Vershinin, 2014-05-02
@WolfdalE

Try moving the code from the stateChanged method to the slider listener. That is, before
add(slider)write

slider.addActionListener(new ActionListener()
{
    @Override
    public void ActionPerformed(ActionEvent e)
    {
//Сюда
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question