Answer the question
In order to leave comments, you need to log in
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
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);
}
}
}
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question