简体   繁体   中英

how to set semi-transparent jframe when “submit” button is clicked?

loadingLab=new JLabel("The name is being saved..");
loadPanel.add(loadingLab);
submitBttn=new JButton("Submit");
submitBttn.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent e) {  
        System.out.println("Submit Button Clicked!!");
        try {
            //something is wrong in here as it throws an exception
            //what is wrong?
            frame.setUndecorated(false);
            frame.setOpacity(0.55f);

            //when above both lines are commented, the code works fine
            //but doesnt have transparency  
            frame.add(loadPanel,BorderLayout.SOUTH);
            frame.setVisible(true);
        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }
});

I am trying to display transparent JFrame when "submit" button is clicked which displays panel with a JLabel ... I have tried using setOpacity(0.55f) , but it throws exception.. what am i doing wrong?

Unfortunately I think there's no way to keep the system window decoration, you will probably have to go with the default one. Since I'm not 100% sure if you want to toggle the opacity of the whole frame or just the frame's background, I've included both functions in my example. (mKorbels answer help you more if you don't want to have a decoration)

Code:

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JToggleButton;

public class TransparentExample extends JFrame {

    public TransparentExample() {

        super("TransparentExample");
        Color defaultBackground = getBackground();
        float defaultOpacity = getOpacity();

        JToggleButton button1 = new JToggleButton("Toggle background transparency");
        button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (button1.isSelected()) {
                    setBackground(new Color(defaultBackground.getRed(), defaultBackground.getGreen(),
                            defaultBackground.getBlue(), 150));
                } else {
                    setBackground(defaultBackground);
                }
            }
        });

        JToggleButton button2 = new JToggleButton("Toggle opacity of whole frame");
        button2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();
                if (button2.isSelected()) {
                    setOpacity(0.55f);
                } else {
                    setOpacity(defaultOpacity);
                }
                setVisible(true);
            }
        });

        getContentPane().setLayout(new FlowLayout());
        getContentPane().add(button1);
        getContentPane().add(button2);
        setSize(800, 600);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame.setDefaultLookAndFeelDecorated(true);
                TransparentExample frame = new TransparentExample();
                frame.setVisible(true);
            }
        });
    }

}

Picture of frame with no togglebutton selected: 在此输入图像描述

Picture of frame with the first togglebutton selected: 在此输入图像描述

Picture of frame with the second togglebutton selected: 在此输入图像描述

@Programmer007 wrote - the exception is " java.awt.IllegalComponentStateException: The frame is displayable."

  • please where I can't see any, for more info about the possible exceptions to read ,

  • as mentioned no idea, everything is about your effort, transformed to the SSCCE / MCVE, short, runnable, compilable

.

import java.awt.Color;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JDialog;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

public class GenericForm extends JDialog {

    private static final long serialVersionUID = 1L;
    private Timer timer;
    private JDialog dialog = new JDialog();
    private int count = 0;

    public GenericForm() {
        dialog.setSize(400, 300);
        dialog.setUndecorated(true);
        dialog.setOpacity(0.5f);
        dialog.setName("Toggling with opacity");
        dialog.getContentPane().setBackground(Color.RED);
        dialog.setLocation(150, 150);
        dialog.setVisible(true);
        timer = new javax.swing.Timer(1500, updateCol());
        timer.setRepeats(true);
        timer.start();
    }

    private Action updateCol() {
        return new AbstractAction("Hello World") {
            private static final long serialVersionUID = 1L;

            @Override
            public void actionPerformed(ActionEvent e) {
                boolean bol = dialog.getOpacity() < 0.55f;
                count += 1;
                if (count < 10) {
                    if (bol) {
                        dialog.setOpacity(1.0f);
                        dialog.getContentPane().setBackground(Color.WHITE);
                    } else {
                        dialog.setOpacity(0.5f);
                        dialog.getContentPane().setBackground(Color.RED);
                    }
                } else {
                    System.exit(0);
                }
            }
        };
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new GenericForm();
            }
        });
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM